/**
 * 无穷级树形菜单 V1.0
 * 作者： Michael Young | windring@new-pp.net
 */

/* 菜单项 */

if (typeof g_imgPath!="undefined")
{
	imgPath = g_imgPath; //图片目录
}else{
	imgPath = "../../images/"; //图片目录
}

function treeNode(){

	this.nodeID;				//节点编号
	this.parentNodeID = 0;		//父节点编号
	this.nodeName;				//节点名称
	this.nodeAction;				//节点点击事件
	this.nodeImg = "leftmenu_arrow1.gif";				//节点图片
	this.nodeImgOver="leftmenu_arrow1Over.gif";			//节点鼠标悬浮
	this.sonNodes;
	this.sonFlag = 0;			//是否有子节点标志

	this.isCheckbox = false;
	this.checkboxValue = 1;
	this.checkboxName;
}

/* 菜单 */

function treeMenu(){

	this.tempStrBuffer=[];
	this.treeNodes;
	this.topNodes = new Array();
	
	//获取顶层节点
	this.getTopNodes = function(){
		var j = 0;
		for (var i = 0;i < this.treeNodes.length;i++){
			if (this.treeNodes[i].parentNodeID == 0){
				this.topNodes[j] = this.treeNodes[i];
				j++;
			}
		}
	}
	
	//获取某节点的子节点
	this.getSonNodes = function(node){
		
		var id = node.nodeID;
		var tmpNodes = new Array();
		var j = 0;

		for (var i = 0;i < this.treeNodes.length;i++){
			if (this.treeNodes[i].parentNodeID == id){
				tmpNodes[j] = this.treeNodes[i];
				this.getSonNodes(tmpNodes[j]); //递归
				node.nodeImg = "leftmenu_arrow2.gif";
				node.nodeImgOver = "leftmenu_arrow2Over.gif"
				node.sonFlag = 1;
				j++;
			}
		}

		node.sonNodes = tmpNodes;
	}
	
	//生成某节点子节点的ＨＴＭＬ
	this.showSonNodes = function(node,deepFlag){
		
		if (node.sonNodes != null && !isNaN(node.sonNodes.length)){

			this.tempStrBuffer.push('<tr id="node' + node.nodeID + '" style="display:none;"><td nowrap><table width="100%" border="0" cellspacing="1" cellpadding="1">');

			for (var i = 0;i < node.sonNodes.length;i++){
				this.tempStrBuffer.push('<tr style="cursor:default;"><td nowrap>');

				for (var j = 0;j < deepFlag;j++)
					this.tempStrBuffer.push('&nbsp;');

				this.tempStrBuffer.push('<img');

				if (node.sonNodes[i].sonFlag == 1)
					this.tempStrBuffer.push(' onclick="tree.showHideNodes(event,' + node.sonNodes[i].nodeID + ')"');

				this.tempStrBuffer.push(' src="'+ imgPath + node.sonNodes[i].nodeImg + '" > ');

				
				this.tempStrBuffer.push('<font style="cursor:hand;" title="' + node.sonNodes[i].nodeName + '" onmouseover="tree.mOver(this);" onmouseout="tree.mOut(this);" onclick="' + node.sonNodes[i].nodeAction + '">' + node.sonNodes[i].nodeName + '</font></td></tr>');

				if (node.sonNodes[i].sonFlag == 1)
					this.showSonNodes(node.sonNodes[i],deepFlag + 1); //递归
			}

			this.tempStrBuffer.push('</table></td></tr>');
		}
	}
	
	//生成所有节点的ＨＴＭＬ
	this.showMenu = function(divId){
		

		this.tempStrBuffer.push('<table width="93%" align="right" border="0" cellspacing="1" cellpadding="1"><tr><td height="20">&nbsp;</td></tr>');

		this.getTopNodes();  //获取顶层节点
		for (var i = 0;i < this.topNodes.length;i++){
			this.getSonNodes(this.topNodes[i]);

			this.tempStrBuffer.push('<tr><td style="cursor:default;" nowrap><img');
			if (this.topNodes[i].sonFlag == 1)
				this.tempStrBuffer.push(' onclick="tree.showHideNodes(event,' + this.topNodes[i].nodeID + ')"');
			this.tempStrBuffer.push(' src="'+ imgPath + this.topNodes[i].nodeImg + '" > ');
			

			this.tempStrBuffer.push('<font style="cursor:hand;" title="' + this.topNodes[i].nodeName + '" onmouseover="tree.mOver(this);" onmouseout="tree.mOut(this);" onclick="' + this.topNodes[i].nodeAction + '">' + this.topNodes[i].nodeName + '</font></td></tr>');
			
			if (this.topNodes[i].sonFlag == 1)
				this.showSonNodes(this.topNodes[i],1);
		}

		this.tempStrBuffer.push('</table>');
		document.getElementById(divId).innerHTML=this.tempStrBuffer.join("");
		
	}

	//显示/隐藏某节点的子节点
	this.showHideNodes = function(evt,id){
		
		/*改变父节点图标*/
		evt = evt || window.event;
		var e = evt.target || evt.srcElement;
		if(e.src.indexOf("leftmenu_arrow2.gif")>0)
		e.src=imgPath+"leftmenu_arrow3.gif";
		else
		e.src=imgPath+"leftmenu_arrow2.gif";




		var obj = eval("document.all.node" + id);
		if (obj.style.display == "none")
			obj.style.display = "";
		else
			obj.style.display = "none";
	}

	this.mOver = function(obj){
		obj.style.backgroundColor = MENU_MOUSEOVER_COLOR;
		obj.style.border = '1px ' + MENU_MOUSEOVER_BORDER_COLOR + ' solid';
	}

	this.mOut = function(obj){
		obj.style.backgroundColor = '';
		obj.style.border = '';
	}
}