

// JavaScript Document
/*
//-----------------------------------------------------------------------------
//	EhmTree 1.0  |  create it for EHM WCM 4-5 2007-08-21
//-----------------------------------------------------------------------------
//	Copyright (c) 2007-12-07 ehm zhangchao
//
//	This script can be used  for EHM WCM 4-5 anysite
//	Process

		var dtf = new dataFactory();//创建数据			必填
		<!--EHM_SystemNav?pcid=0&getsuball=1-->			
		var channelStr = "<!--EHM_NavList-->";//		必填
		<!--/EHM_SystemNav-->
		dtf.useEspDot();//运用特殊点                    可选
		dtf.setchannelId(ChannelID);//本频道id(小频道)	必填
		//输出频道串
		dtf.initializeData(channelStr);//				必填
		dtf.addClass('频道一级关闭时样式,频道一级打开时样式,频道一级下划线样式;频道二级关闭时样式,频道二级打开时样式,频道二级下划线样式;频道三级关闭时样式,频道三级打开时样式,频道三级下划线样式');//添加样式 不同栏目用';'分割,相同栏目用','分割,第二个参数为下划线样式						必填
		dtf.insertChannelName("ChannelName1");//在id为"ChannelName1"的标签内显示主频道名称			可选
		dtf.insertChannelPic("asdasd");//在id为"ChannelName1"的标签内显示主频道名称		可选
		dtf.writeNav();//输出频道						必填
//-----------------------------------------------------------------------------
*/
function Node(id,pid,url,topic,classname,target,title,selectedflag,parentpicstr)
{
	this.id			= id;
	this.pid		= pid;
	this.topic		= topic;
	this.url		= url;
	this.classname	= classname;
	this.target		= target;
	this.title		= title;
	this.selectedflag	= selectedflag;
	this.parentpicstr	= parentpicstr;
	this.classLine = "";
}
function CssClass(class_close,class_open,class_line)
{
	
this.class_close = (class_close==null)?"":class_close;
this.class_open = (class_open==null)?"":class_open; ;
this.class_line =  (class_line==null)?"":class_line;;
}
function ehmTree(objectName,divid)
{
	this.arrNodes		= [];
	this.thisid			= 0;
	this.strOutput		= '';
	this.arrClass= [];
	this.instanceName	=objectName;
	this.selectedNode	= null;
	this.useCookies		=true;
	this.rootNode		=0;
	this.divid			=divid;
	this.ppcid 			="";
	this.thispid		="";	
	this.espdots		=false;
	var re=/\//;
	var lcstr=location.href.replace("http://","");
	var firstindex=lcstr.search(re); 
	var firststr  =lcstr.substring(0, firstindex);
	this.locationUrl= lcstr.replace(firststr,"");
	this.insertThis = function (id){this.thisid=id;}
	this.setPpcid = function (){
		this.ppcid = this.getGradid();
		}//获取最祖父级id


	this.add = function(id,pid,url,topic,classname,target,title,selectedflag,parentpicstr)
	{	
	this.setCookie("co_"+id,false);	
	this.arrNodes[this.arrNodes.length] = new Node(id,pid,url,topic,classname,target,title,selectedflag,parentpicstr);
	}
	
	this.addClasses = function (classStr){	
	var temparrcalss = classStr.split(";");
		for(var c_index = 0 ;c_index<temparrcalss.length ;c_index++){
		var tmpclass = temparrcalss[c_index].split(",");
		this.arrClass[this.arrClass.length] =new CssClass(tmpclass[0],tmpclass[1],tmpclass[2]);
		}
	}
	
	this.useEspDots = function(){this.espdots = true;}
	
	//初始化样式到对象
	this.initializeClass = function(){	
		var re,levelindex;
		for (var nn=0; nn<this.arrNodes.length; nn++)
		{	
		levelindex = this.Nodelevel(this.arrNodes[nn].id)-1
		if(levelindex>=this.arrClass.length){levelindex=this.arrClass.length-1}
		this.arrNodes[nn].classLine=this.arrClass[levelindex].class_line;
			if(this.getCookie("co_"+this.arrNodes[nn].id)=="true"){
				
			if (this.hasChildren(this.arrNodes[nn])){
			this.arrNodes[nn].className=this.arrClass[levelindex].class_close;	
				}else{
			re	= location.href;
			if(re.indexOf( this.arrNodes[nn].url)>0){
			this.arrNodes[nn].className=this.arrClass[levelindex].class_close;	
				}else{
			this.arrNodes[nn].className=this.arrClass[levelindex].class_open;		
			}		
			}
			
			
			}else{
			this.arrNodes[nn].className=this.arrClass[levelindex].class_open;
			}
			
			if(this.arrNodes[nn].pid==this.ppcid ){this.arrNodes[nn].className=this.arrClass[levelindex].class_close;	}
						
			if(this.espdots&&(this.arrNodes[nn].pid==this.ppcid )){
				if(!this.hasChildren(this.arrNodes[nn])){
				this.arrNodes[nn].className=this.arrClass[levelindex].class_open;	
					}
				}
		}
	}	
	
	
	this.draw = function()
	{	
		this.thispid=this.getParentid(this.thisid);	//获取此频道的上一层
		this.initializeCookie(this.thisid);	//初始化cookie
		this.initializeNavCookie();	//初始化cookie
		this.setCookie("co_"+this.thisid,true);		
		this.initializeClass();
//				
		if (document.getElementById)
		{
			this.addNode();
			document.getElementById(this.divid).innerHTML=this.strOutput;
		}
		else
		{document.writeln('Browser not supported.');}
	}
//		// Recursive function that creates the tree structure
	this.addNode = function()
	{
	var display="";
	var onclick = "";
	//alert(this.arrNodes.length)
		for (var n=0; n<this.arrNodes.length; n++)
		{
			if (this.arrNodes[n].pid==0){
			
				this.strOutput+='<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr align="left" valign="middle"><td class="border_t border_b title03"><img src="'+this.arrNodes[n].parentpicstr+'" /></td></tr></table>';
				
				this.setCookie("co_"+this.arrNodes[n].pid,"true");
				this.setCookie("co_"+this.arrNodes[n].id,"true");
				}
			else{
			if (this.getCookie("co_"+this.arrNodes[n].id)=="true"){display=""}else{display="none"}
			if(this.hasChildren (this.arrNodes[n]))
				{onclick =' onclick="'+this.instanceName+'.divAction(\''+this.arrNodes[n].id+'\')"'} 
				else
				{onclick = 'onclick="'+this.instanceName+'.ch(\''+this.arrNodes[n].id+'\')"';}
				
				this.strOutput+='<div class="'+this.arrNodes[n].className+'" id="dtree_'+this.arrNodes[n].id+'"  style="display:\''+display+'\';cursor:pointer;" '+onclick+' >'+this.arrNodes[n].topic+'</div>';
			
				if(this.arrNodes[n].pid!=this.ppcid)this.strOutput+='<div id="line_'+this.arrNodes[n].id+'" class="'+this.arrNodes[n].classLine+'" style="display:\''+display+'\';cursor:pointer;" ></div>';
				}
		}	
	}
		
		//初始化cookie
	this.initializeCookie= function (id)
	{ var ppid=this.getParentid(id) ;//

			for (var cc=0; cc<this.arrNodes.length; cc++)
			{		//同一级的要显示啊	
			if (this.arrNodes[cc].pid==ppid){this.setCookie("co_"+this.arrNodes[cc].id,true);}
			}	

		if(ppid&&(ppid!=this.ppcid)){this.initializeCookie(ppid);}
			
	}
	this.initializeNavCookie = function ()
	{for (var cc=0; cc<this.arrNodes.length; cc++)
	{if (this.arrNodes[cc].pid==this.ppcid){this.setCookie("co_"+this.arrNodes[cc].id,true);}}	
	}
	
	//获取父id
	this.getParentid = function (id)
	{
		for (var n=0; n<this.arrNodes.length; n++)
		{if (this.arrNodes[n].id==id)
		{return this.arrNodes[n].pid;
		break;}
		}
		return null;
	}
	
	//获取祖id
	this.getGradid = function ()
	{	var temparrs= [];
		for (var n=0; n<this.arrNodes.length; n++)
		{temparrs[n] = this.arrNodes[n].pid;}
		temparrs.sort()
		return temparrs[0];	
	}
	
	this.Nodelevel = function (cNode)
	{
	this.levels=0;
	
	this.Looplevel(cNode)
	
	return this.levels;
	}
	
	this.Looplevel = function (cNode)
	{
		for (var No_lop_index=0; No_lop_index<this.arrNodes.length; No_lop_index++)
		{
			if(this.arrNodes[No_lop_index].id==cNode&&this.arrNodes[No_lop_index].pid!=0)
				{this.levels++;
				this.Looplevel(this.arrNodes[No_lop_index].pid)
				break;
				}
		}
		return this.levels;
	}
	
	this.ch = function (id)
	{
		if (this.hasChildren(this.getnode(id))){
			if (this.getCookie("co_"+id)=="true")
			{
			this.closeAll(id);
			this.setCookie("co_"+id,false)
			}else
			{
			this.openAll(id);
			this.setCookie("co_"+id,true)
			}
		}

	this.Gourl(id)
	}
	
	this.Gourl= function (id)
	{
		for (var n=0; n<this.arrNodes.length; n++)
		{
			if (this.arrNodes[n].id == id)
			{	if(this.arrNodes[n].url!=this.locationUrl)
				{location.href=this.arrNodes[n].url;
				break;
				return;}
			}
		}
		
	}
	
	this.divAction  = function (id)
	{if(this.IsOpen(id)){this.closeAll(id);this.setCookie("co_"+id,false);}else{this.openAll(id);this.setCookie("co_"+id,true);}}
	
	this.IsOpen = function (id)
	{
	for (var n=0; n<this.arrNodes.length; n++)
		{
			if (this.arrNodes[n].pid == id)
			{if(document.getElementById('dtree_'+this.arrNodes[n].id).style.display!="none") return true;}
		}
		return false;
	}
	
	
	this.closeAll = function (id)
	{
		for (var n=0; n<this.arrNodes.length; n++)
		{
			if ((this.arrNodes[n].id == id)&&(this.hasChildren(this.arrNodes[n]))){
			document.getElementById('dtree_'+id).className=this.arrNodes[n].classname=this.arrClass[this.Nodelevel(this.arrNodes[n].id)-1].class_close;
			}
			if (this.arrNodes[n].pid == id)
			{
			document.getElementById('dtree_'+this.arrNodes[n].id).style.display="none";
			document.getElementById('line_'+this.arrNodes[n].id).style.display="none";
			this.setCookie("co_"+this.arrNodes[n].id,false);
			this.closeAll(this.arrNodes[n].id);
			}
		}
	}
	
	
	
	this.openAll = function (id)
	{
		for (var n=0; n<this.arrNodes.length; n++)
		{
			if ((this.arrNodes[n].id == id)&&(this.hasChildren(this.arrNodes[n]))){
			document.getElementById('dtree_'+id).className=this.arrNodes[n].classname=this.arrClass[this.Nodelevel(this.arrNodes[n].id)-1].class_open;
			}
			if (this.arrNodes[n].pid == id)
			{	document.getElementById('dtree_'+this.arrNodes[n].id).style.display="block";
				document.getElementById('line_'+this.arrNodes[n].id).style.display="block";
				this.setCookie("co_"+this.arrNodes[n].id,true);
			}
		}
	}

	this.hasChildren = function(node){//	// Checks if a node has any children
		for (var Children_n=0; Children_n<this.arrNodes.length; Children_n++)
		if (this.arrNodes[Children_n].pid == node.id) return true;
		return false;
	}
	
	 this.getnode=function(id)
	{
				for (n=0; n<this.arrNodes.length; n++)
			if (this.arrNodes[n].id == id) return this.arrNodes[n];
	}
		
//	// Sets value in a cookie	
	this.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
		document.cookie =
			escape(cookieName) + '=' + escape(cookieValue)
			+ (expires ? '; expires=' + expires.toGMTString() : '')
			+ (path ? '; path=' + path : '')
			+ (domain ? '; domain=' + domain : '')
			+ (secure ? '; secure' : '');
	}

	// Gets a value from a cookie
	this.getCookie = function(cookieName) {
		var cookieValue = '';
		var posName = document.cookie.indexOf(escape(cookieName) + '=');
		if (posName != -1)
		{
			var posValue = posName + (escape(cookieName) + '=').length;
			var endPos = document.cookie.indexOf(';', posValue);
			if (endPos != -1)
				cookieValue = unescape(document.cookie.substring(posValue, endPos));
			else
				cookieValue = unescape(document.cookie.substring(posValue));
		}
		return (cookieValue);
	}

	// Returns ids of open nodes as a string
	this.updateCookie = function()
	{
		sReturn = '';
		for (n=0;n<this.arrNodes.length;n++)
		{
			if (this.arrNodes[n]._io && this.arrNodes[n].pid != this.rootNode)
			{if (sReturn) sReturn += '.';
				sReturn += this.arrNodes[n].id;
			}
		}
		this.setCookie('co' + this.instanceName, sReturn);
	}
}
//--------------------------------------------------------------------------
function datebean(cid,pcid,topic,links,images)//初级封装数据
{
this.cid = cid;
this.pcid = pcid;
this.topic = topic;
this.links = links;
this.images = images;
}



function dataFactory()
{	this.navArr = [];
	this.navTempArr = [];
	this.navtemparr = [];
	this.channelId = "";
	this.ppcids = "";
	this.class_link = "";
	this.class_line = "";
	this.espDots = false;
	this.topId = 0;
	
	this.setchannelId = function (cid){this.channelId = cid;}
	this.unUseTop = function(){this.topId = this.channelId;}
	this.useEspDot = function(){this.espDots = true;}
	this.initializeData = function (Str)
	{	
		this.navtemparr = Str.split(";");
		for(data_index=0;data_index<this.navtemparr.length;data_index++){
		this.datetemparr = this.navtemparr[data_index].split(",");
		this.pushinTempArr(this.datetemparr[0],this.datetemparr[1],this.datetemparr[2],this.datetemparr[3],this.datetemparr[4]);}
		this.getTopNavId(this.channelId)//获取顶端栏目的id
		this.reInit(this.topId)//数组装载
	}
	
	this.reInit	= function (topid){
		for(var re_index=0;re_index<this.navTempArr.length;re_index++)
		{

			if(this.navTempArr[re_index].pcid == topid){
			if(this.navTempArr[re_index].pcid!=0){this.navArr[this.navArr.length]=this.navTempArr[re_index];}
			if(this.haveCh(this.navTempArr[re_index].cid)){this.reInit(this.navTempArr[re_index].cid)}
				}
		}
		this.navtemparr.length = null;
		this.datetemparr = null;
		this.pushinTempArr = null;
	}

	this.pushinTempArr = function (cid,pcid,topic,links,images){this.navTempArr[this.navTempArr.length] = new datebean(cid,pcid,topic,links,images);}
	
	this.haveCh = function(id){
		for(var hvc_index=0;hvc_index<this.navTempArr.length;hvc_index++)
		{
		if (this.navTempArr[hvc_index].pcid == id){return true;}
		}
		return false;
	}
	
	this.getTopNavId = function(chnid){//获取祖父级栏目id
		for(var chn_index=0;chn_index<this.navTempArr.length;chn_index++){
			if(this.navTempArr[chn_index].cid == chnid){
				if(this.navTempArr[chn_index].pcid == this.topId){
					this.topId = this.navTempArr[chn_index].cid
					return ;
					}else{
					this.getTopNavId(this.navTempArr[chn_index].pcid)	
					}
				}
			}
	}
	
	
	this.setPpcids = function (ppcid){this.ppcids= ppcid;}//写栏目id
	
	this.addClass = function (class_link){this.class_link = class_link;}//添加划线样式
	
	this.insertChannelName = function (elemId){
		for(var hvc_index=0;hvc_index<this.navTempArr.length;hvc_index++)
			{if (this.navTempArr[hvc_index].cid == this.topId){
					if(document.getElementById(elemId)){document.getElementById(elemId).innerHTML = this.navTempArr[hvc_index].topic}
					}
		}
	}

	this.insertChannelPic = function (elemId){//加入图片
		for(var hvc_index=0;hvc_index<this.navTempArr.length;hvc_index++)
			{if (this.navTempArr[hvc_index].cid == this.topId)
				{if(document.getElementById(elemId)){document.getElementById(elemId).innerHTML = "<img src='"+this.navTempArr[hvc_index].images+"' />"}
				}
			}
		}

	this.writeNav = function ()
	{	//此处id最好可以更换
		d=new ehmTree('d','left');
		d.insertThis(this.channelId);
		for(data_index=0;data_index<this.navArr.length;data_index++){
		d.add(this.navArr[data_index].cid,this.navArr[data_index].pcid,this.navArr[data_index].links,this.navArr[data_index].topic,'','menu2 border_b link12_grey',this.navArr[data_index].topic,1,this.navArr[data_index].images);
		}
		d.addClasses(this.class_link);
		if(this.espDots){d.useEspDots();} 
		d.setPpcid();
		d.draw();
	}
}
