


// JavaScript Document
/* 基于ehm要求的头部导航
//-----------------------------------------------------------------------------
//	topNav 1.0  |  create it for EHM WCM 4-5 2007-12-11
//-----------------------------------------------------------------------------
//	Copyright (c) 2007-12-11 ehm zhangchao

//-----------------------------------------------------------------------------
*/

function $$(id)
{
	if(document.getElementById(id))
	{return document.getElementById(id);}
	else{return null}
}

function nav_ON_min(cid,pcid,topic,links,images)//初级封装数据
{
this.cid = cid;
this.pcid = pcid;
this.topic = topic;
this.links = links;
this.images = images;
this.son = [];
}

function databuilder(pcid)
{
var arrs = channelStr.split(";");
var temparr = [] ;
var arrson = [] ;

	for(var arrs_index=0;arrs_index<arrs.length;arrs_index++)
	{
	temparr = arrs[arrs_index].split(",");
	if(temparr[1]==pcid)
	{
	arrson[arrson.length] = new nav_ON_min(temparr[0],temparr[1],temparr[2],temparr[3],temparr[4]);
	}

	}
	arrs = null
	temparr=null
	return arrson;

}

function topnavOn(pcid,cid,clink,ctopic,css1,css2,x)
{
this.pcid = pcid;
this.X_zone = x;
this.cnode = [];
this.cnode = new cnodeOn(cid,clink,ctopic,css1,css2)
}
function cnodeOn(cid,clink,ctopic,css1,css2)
{
this.cid=cid;
this.clink=clink;
this.ctopic=ctopic;
this.css1=css1;
this.css2=css2;

}
function webrootbean(id,rootname)
{this.id=id;
this.rootname =rootname;}
function topNav()
{	
	this.nowindex = -1;
	this.navlist=[];
	this.Dargflag = false;
	this.timeID = "";
	this.upflag=false;
	this.downflag=false;
	this.Y_index = -1;
	this.X_index = 6;
	this.webroot={id:0,rootname:""}
	this.windowScrollTop = document.body.scrollTop;
	this.windowScrollLeft =document.body.scrollLeft ;
	this.useDrag = function(tag){this.Dargflag = tag;}
	this.setwebroot = function (){
		
		id= arguments[0];
		var portnum=(document.URL.replace("http://"+document.domain,"").split("/")[0]);
		var domainstr="http://"+document.domain+portnum;
		var rootname=location.href;
		rootname = rootname.replace("http://","");
		rootname = rootname.split("/")[1];
		
		var alink=document.getElementById("topnav_"+id).childNodes[0];
        if(arguments[1]){if(!(rootname==arguments[1])){rootname=""}}
		
		alink.href=(alink.href.indexOf(rootname)>=0)?alink.href:"/"+rootname+alink.href.replace(domainstr,"");

		this.webroot=new webrootbean(id,rootname);
		}
	this.add = function (pcid,cid,clink,ctopic,css1,css2)
	{	
	
	var clink=(parseInt(pcid)==parseInt(this.webroot.id) && !(clink.indexOf(this.webroot.rootname)>=0))?(this.webroot.rootname+clink):clink;
   
   clink =(clink.indexOf("/")==0)?clink:("/"+clink)
	
	ctopic = "<a href='"+clink+"'>"+ctopic+"</a>"
	var widthStr1=(arguments[6])?" width:"+arguments[6]+"px;":""
	var widthStr=widthStr1+"float:left;"
	var tempdiv = "";
	var tempdiv2 ="";
		this.nowindex = this.getIndex(pcid)

		var StyleStr = (this.Dargflag)?"display:block; OVERFLOW: hidden; height:0px;"+widthStr:"display:none;"+widthStr;
		tempdiv2 = "<div class='"+css1+"' onmouseout='this.className=\""+css1+"\"' onmouseover='this.className=\""+css2+"\"'  style='float:left'>"+ctopic+"</div>"
		if(this.nowindex>=0)//本组第二次加载
		{
			this.navlist[this.nowindex].cnode[this.navlist[this.nowindex].cnode.length] = new cnodeOn(cid,clink,ctopic,css1,css2);
			if($$("ch_"+pcid)){$$("ch_"+pcid).innerHTML =  $$("ch_"+pcid).innerHTML+tempdiv2;}
		}else{
			this.navlist[this.navlist.length] = new topnavOn(pcid,cid,clink,ctopic,css1,css2,this.X_index);
			//外面的下拉木快
			tempdiv = "<div id='ch_"+pcid+"' style='position:absolute;z-index:1; bottom:5px; left:0px; "+StyleStr+" ' ></div>"
			
			var clientY_div="<div style='left:0px; margin-top:-15px;*margin-top:-11px;"+widthStr1+"' ></div>";
			if($$("topnav_"+pcid)){
				$$("topnav_"+pcid).innerHTML =  $$("topnav_"+pcid).innerHTML+tempdiv;				
				}
			if($$("ch_"+pcid)){
				$$("ch_"+pcid).innerHTML =clientY_div+tempdiv2;
				}
			
		}
		
		//alert($$("topnav_"+pcid).innerHTML)
		if($$("topnav_"+pcid)){
			
		$$("topnav_"+pcid).onmouseover =  this.BtnMouseOver;
		$$("topnav_"+pcid).onmouseout =  this.BtnMouseOut;
		}
	
	}

	this.getIndex = function(pcid)
	{
		for(var Cindex=0;Cindex<this.navlist.length;Cindex++)
		{
		if (this.navlist[Cindex].pcid == pcid){return Cindex;}
		}
		return -1;
	}

	
		this.BtnMouseOver= function()
	{
		var pcid;
	if (event.srcElement.tagName != "A") 
	{ pcid = event.srcElement.id;}
	else{
		var alink = event.srcElement;
		 pcid = event.srcElement.parentElement.id
		}
		
		if(topNavlist.getTrueId(pcid))
		{
			//alert(document.body.scrollTop)
		pcid=topNavlist.getTrueId(pcid)
		if($$("ch_"+pcid).style.top<10)
		{
			
			
		$$("ch_"+pcid).style.left=event.clientX-event.offsetX-parseInt(topNavlist.navlist[topNavlist.getIndex(pcid)].X_zone) +document.body.scrollLeft ;

	 	$$("ch_"+pcid).style.top=event.clientY-event.offsetY+$$("topnav_"+pcid).clientHeight+topNavlist.Y_index+document.documentElement.scrollTop;
		   }

		topNavlist.showDiv("ch_"+pcid);		
		}
		//alert(document.getElementById("navtd").innerHTML)
	}
	
	
		this.BtnMouseOut= function()
	{	var pcid;
		if(event.srcElement.id==""){
		if(event.srcElement.parentElement.id==""){
		pcid = event.srcElement.parentElement.parentElement.id
		}else{
		pcid = event.srcElement.parentElement.id
		}
		 
		}else{
		pcid = event.srcElement.id;
		}
		pcid=topNavlist.getTrueId(pcid)
		try{
		var ishidden=$$("topnav_"+pcid).contains(document.elementFromPoint(event.clientX,event.clientY))
		}catch(e){}
		if(ishidden)   
		{}else{
		this.upflag=false;
		this.downflag=true;
		topNavlist.hiddenDiv("ch_"+pcid)
		}
	}
	
	this.hiddenDiv = function(obj_id)
	{	var obj=$$(obj_id)
		if(this.Dargflag)
		{
		var y=0,offset=3;
		y = parseInt(obj.style.height.replace("px",""))-offset;
		y = (y<0)?0:y;
		 obj.style.height=y+"px" 
		 if(y>0)
		 {
		 	this.upflag=false;
			this.downflag=true;
		 }else{
		 this.upflag=false;
		this.downflag=false;
		}
		   if(this.downflag) {this.timeID=window.setTimeout("topNavlist.hiddenDiv('"+obj_id+"')",120); }
		   else{window.clearTimeout(this.timeID)
			return;
		   }
		
		}else{
		obj.style.display="none";			
		}

	}
	
	this.showDiv = function(obj_id)
	{
		var obj=$$(obj_id)
		if(this.Dargflag)
		{	var y=0,offset=3;
		y = parseInt(obj.style.height.replace("px",""))+offset;
		 obj.style.height=y+"px" 
		 if(y<=obj.scrollHeight){
		 	this.upflag=true;
			this.downflag=false;
		 }else{this.upflag=false;
			this.downflag=false;}
	   if(this.upflag) this.timeID=window.setTimeout("topNavlist.showDiv('"+obj_id+"')",120)
	   else{try{window.clearTimeout(this.timeID)}catch(e){}}
	   
	  	}else{
			/*if(obj.childNodes.length>0)
			{ var div_width_str=obj.childNodes[0].offsetWidth;
				//var div_width=parseInt(div_width_str.replace("px",''))+2
				//obj.style.width=(div_width*obj.childNodes.length)+'px'
				alert(div_width_str)
			}
			*/
			//alert(obj.innerHTML)
		obj.style.display="block";			
		}
	}
	
	this.getTrueId = function (nowid)
	{
	if(nowid.indexOf("_")>0){return nowid.split("_")[1]}else{return null;}
	}
}

function getnavlist()
{
this.arr = [];
topNavlist = new topNav();
topNavlist.useDrag(false)
this.setXzone = function(x){topNavlist.X_index=x}
this.setYzone = function(y){topNavlist.Y_index=y}
this.setwebroot =  function(id,rootname){topNavlist.setwebroot(id,rootname)}
this.setnavId= function(id,css1,css2)
{

this.arr=databuilder(id)

for(var arr_index=0;arr_index<this.arr.length;arr_index++)
{
if(arguments[3])
{
topNavlist.add(id,this.arr[arr_index].cid,this.arr[arr_index].links,this.arr[arr_index].topic,css1,css2,arguments[3]);
}else{
topNavlist.add(id,this.arr[arr_index].cid,this.arr[arr_index].links,this.arr[arr_index].topic,css1,css2);
}

}
}
}