/*
=========================================================
 Version: Jax 0.5
 Date: 2006-5-1
 Script Written by Utoper Jax Group
 Copyright (C) 2006 Utoper Corp. All rights reserved.
 Web: http://Jax.Utoper.com
 Email: UtoJax@163.com
=========================================================
****************************************************************** 
** 名: tree  属于JBM － Jax Base Module
** 创建：welsham 2006-8-27  修改: welsham 2006-8-27
** 描述: parser之树，树型结构的处理，树图……
** 修改描述: 
******************************************************************
*/

//**************初始化parser对象和tree类************************
if(typeof parser != "object") { var parser={}; }
if(typeof parser.tree != "function")
{
	parser.tree=function(_o,_DD,_rootKey,_target)
	{
		this.o=_o;
		this.DD=_DD;
		this.selectedDi="";
		if(_rootKey) this.rootKey=_rootKey;
		else this.rootKey="";
		this.openSicon="image/tree/2.gif";
		this.closeSicon="image/tree/1.gif";
		this.openIcon="image/tree/72.gif";
		this.closeIcon="image/tree/71.gif";
		
		this.target=_target;
		
		//预载入图像
		if(parser.file)
		{
			parser.file.preloadImages(this.openSicon,this.closeSicon,this.openIcon,this.closeIcon);
		}
	};
}

//**************重置子数****************************************
parser.tree.prototype.resetChildren=function(_key)
{
	var i=0,key;
	for(key in this.DD)
	{
		if(this.DD[key].parent==_key) i++;	
	}
	this.DD[_key].children=i;
};

//**************初始化树图****************************************
//参数：_pNode-生成后的html填充的页面元素
parser.tree.prototype.initTree=function(_pNode)
{
	var root=document.createElement("DIV"),node,key,nodeHtml;
	root.id="di"+this.rootKey;
	_pNode.appendChild(root);
	for(key in this.DD)
	{
		node=document.createElement("div");
		if(this.DD[key].children>0)
		{
			node.style.padding="0 0 0 "+this.DD[key].layer*18+"px";
			nodeHtml='<a href="javascript:'+this.o+'.go(\''+key+'\')">\
			<img id="diSicon'+key+'" src="'+this.closeSicon+'" border="0" align="absmiddle" /></a>';
			if(this.DD[key].closeIcon&&this.DD[key].closeIcon!='')
				nodeHtml += '<img id="diIcon'+key+'" src="'+this.DD[key].closeIcon+'" border="0" align="absmiddle" />';
			else
				nodeHtml += '<img id="diIcon'+key+'" src="'+this.closeIcon+'" border="0" align="absmiddle" />';
			
			nodeHtml += '<a id="dia'+key+'" onclick="'+this.o+'.onSelect(\''+key+'\')"';
			if(this.DD[key].url) nodeHtml += ' href="'+this.DD[key].url+'"';
			else nodeHtml += ' href="javascript:"';
			if(this.target) nodeHtml += ' target="'+this.target+'"';
			nodeHtml += '>'+this.DD[key].value+'</a>';
			
			node.innerHTML=nodeHtml;
			
			$("di"+this.DD[key].parent).appendChild(node);
			node=document.createElement("div");
			node.id="di"+key;
			node.style.display="none";
			$("di"+this.DD[key].parent).appendChild(node);
		}
		else
		{
			node.style.padding="0 0 0 "+(this.DD[key].layer+1)*18+"px";
			if(this.DD[key].closeIcon&&this.DD[key].closeIcon!='')
				nodeHtml='&nbsp;<img src="'+this.DD[key].closeIcon+'" border="0" align="absmiddle" />';
			else
				nodeHtml='&nbsp;<img src="'+this.closeIcon+'" border="0" align="absmiddle" />';			
			
			nodeHtml += '<a id="dia'+key+'" onclick="'+this.o+'.onSelect(\''+key+'\')"';
			if(this.DD[key].url) nodeHtml += ' href="'+this.DD[key].url+'"';
			else nodeHtml += ' href="javascript:"';
			if(this.target) nodeHtml += ' target="'+this.target+'"';
			nodeHtml += '>'+this.DD[key].value+'</a>';
			
			node.innerHTML=nodeHtml;
			$("di"+this.DD[key].parent).appendChild(node);
		}
	}
};
//**************显示切换****************************************
parser.tree.prototype.go=function(_key)
{
	var di=$("di"+_key);
	if(di.style.display=="none")
	{
		var pKey=_key.toString();
		while(this.DD[pKey]&&pKey!="")
		{
			if($("di"+pKey).style.display=="none")
			{
				$("di"+pKey).style.display="block";
				$("diSicon"+pKey).src=this.openSicon;
				if(this.DD[pKey].openIcon&&this.DD[pKey].openIcon!='')
					$("diIcon"+pKey).src=this.DD[pKey].openIcon;
				else
					$("diIcon"+pKey).src=this.openIcon;
			}
			pKey=this.DD[pKey].parent;
		}
	}
	else
	{
		di.style.display="none";
		$("diSicon"+_key).src=this.closeSicon;
		if(this.DD[_key].closeIcon&&this.DD[_key].closeIcon!='')
			$("diIcon"+_key).src=this.DD[_key].closeIcon;
		else
			$("diIcon"+_key).src=this.closeIcon;
	}
	this.afterGo();
};
parser.tree.prototype.afterGo=function(){};
//**************选择事件****************************************
parser.tree.prototype.onSelect=function(_key)
{
	//存储选中值及切换样式
	if(this.selectedDi!="") $("dia"+this.selectedDi).className="";
	this.selectedDi=_key.toString();
	$("dia"+this.selectedDi).className="sel";
	//打开下级、上级
	if($("di"+_key)&&$("di"+_key).style.display=="none") this.go(_key);
	else if(this.DD[_key].parent!=""&&$("di"+this.DD[_key].parent).style.display=="none") this.go(this.DD[_key].parent);
	//激发附加的事件
	this.afterSelect();
};
parser.tree.prototype.afterSelect=function(){};
//全部折叠和全部打开
parser.tree.prototype.switchTree=function(_type)
{
	var key,di;
	switch(_type)
	{
		case 1: //全部折叠
		{
			for(key in this.DD)
			{
				if(this.DD[key].layer==0&&this.DD[key].children>0)
				{
					di=$("di"+key);
					if(di.style.display!='none')
					{	
						di.style.display='none';
						$("diSicon"+key).src=this.closeSicon;
						if(this.DD[key].closeIcon&&this.DD[key].closeIcon!='')
							$("diIcon"+key).src=this.DD[key].closeIcon;
						else
							$("diIcon"+key).src=this.closeIcon;
					}
					
				}
			}
			break;
		}
		case 2: //全部打开
		{
			for(key in this.DD)
			{
				if(this.DD[key].children>0)
				{
					di=$("di"+key);
					if(di.style.display=='none')
					{	
						di.style.display="block";
						$("diSicon"+key).src=this.openSicon;
						if(this.DD[key].openIcon&&this.DD[key].openIcon!='')
							$("diIcon"+key).src=this.DD[key].openIcon;
						else
							$("diIcon"+key).src=this.openIcon;
					}
				}
			}
			break;
		}
	}
};

//**************初始化列表****************************************
//参数：_pNode-生成后的html填充的页面元素
parser.tree.prototype.initList=function(_pNodeID,_key)
{
	var _key=_key.toString();
	var pattern=$("listPattern").innerHTML.split("#split#");
	var sHtml="",drHtml;
	var childDis=parser.JSON.childNodes(this.DD,_key,true);
	var key,cKey,nKey;
	
	var tpl,_arrTpl=[];
	
	if( !parser.ie ){
		tpl=$("listPattern").getElementsByTagName("table");
		tpl = tpl[0].getElementsByTagName("tr");
		
		for(var i=0;i<tpl.length;i++)
		{
			if(i==0) _arrTpl[i] = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"2\" bgcolor=\"#FFFFFF\" class=\"mainBorder\"><tr>"+tpl[i].innerHTML+"</tr>";
			else if(i == tpl.length)
			{
				_arrTpl[i] = "<tr>"+tpl[i].innerHTML.replace("&#10;","&#13;")+"</tr></table>";
			}
			else
			{
				_arrTpl[i] = "<tr>"+tpl[i].innerHTML+"</tr>";
			}
		}
		pattern = _arrTpl;
	}
	
	for(cKey in childDis)
	{
		key=childDis[cKey];
		drHtml=pattern[2];
		drHtml=drHtml.replace(/#key#/gi,key);
		drHtml=drHtml.replace(/#click#/gi,this.o+".initList('"+_pNodeID+"','"+key+"')");
		for(nKey in this.DD[key])
		{
			drHtml=drHtml.replace(new RegExp("#"+nKey+"#","gi"),this.DD[key][nKey]);
		}
		sHtml += drHtml;		
	}
	var pKey=this.DD[_key].parent;
	if(this.DD[pKey]) sHtml = pattern[1].replace("#returnUp#",this.o+".initList('"+_pNodeID+"','"+pKey+"')")+sHtml;
	sHtml=pattern[0]+sHtml+pattern[3];
	$(_pNodeID).innerHTML=sHtml;
};

//**************初始化下拉框****************************************
//根据parentTree对DD的key进行排序，
parser.tree.prototype.initListBox=function(DD,oListBox)
{
	var key,i,keys=[];
	for(key in DD)
	{
		keys[keys.length]=key;
	}
	keys.sort
	(
		function(a,b)
		{
			if(DD[a].parentTree<DD[b].parentTree) return -1;
			else if(DD[a].parentTree==DD[b].parentTree) return DD[a].orders-DD[b].orders;
			else if(DD[a].parentTree>DD[b].parentTree) return 1;
		}
	);
	removeAllOption(oListBox);
	for(i in keys)
	{
		key=keys[i];
		oListBox.options[oListBox.length]=new Option(DD[key].value,key);
	}
};