﻿function Node(){this.ID;this.text;this.isLastNode;this.childPanelOpened;this.isLeaf;}
function YTree(name,treeDataUrl,nodeCallFunction,imgPath,iconWidth,iconTop,w)
{
	this.Cf={
		n:name,
		treeID:name,
		imgPath:imgPath||"images",
		iconWidth:iconWidth||18,
		iconTop:iconTop||0,
		treeDataUrl:treeDataUrl||"",
		nodeCallFunction:nodeCallFunction||"drawPoint",
		callFunction:"",
		w:w==undefined?"":w
	};

	this.GetLeftImg = function(src){
		var src_="";
		if(src==undefined) return;
		src=src.replace("\\","/").toLowerCase();
		src=src.match(/[^/]+$/);
		switch(src.toString())
		{
			case "elbow-end-plus.gif":
				src_=this.Cf.imgPath+"elbow-end-minus.gif";
			break;
			
			case "elbow-end-minus.gif":
				src_=this.Cf.imgPath+"elbow-end-plus.gif";
			break;
			
			case "elbow-plus.gif":
				src_=this.Cf.imgPath+"elbow-minus.gif";
			break;
			
			case "elbow-minus.gif":
				src_=this.Cf.imgPath+"elbow-plus.gif";
			break;
		}
		return src_;
	};
	
	this.DrawNode=function()
	{
		if($("#"+name).html()!="")
		{
			$("#"+name).toggle();
			return;
		}
		this.Cf.treeID+="yingTree";
		//$("#"+name).show();
		$("#"+name).html("<div style=\"height:18px;width:auto;background:url("+this.Cf.imgPath+"loading.gif) no-repeat 0px 1px;padding-left:20px;\">正在载入…</div>");
		this.ListNode(0,0);
	};
	this.ListNode_=function(e)
	{
		e.data.tree.ListNode(e.data.pid,e.data.deepth);
	};
	this.ListNode=function(pid,deepth)
	{
		deepth++;
		var n=null;
		var nodes=new Array();
		if($("#"+this.Cf.treeID+pid).html()!="" && pid!=0)
		{
			if($("#"+this.Cf.treeID+pid).css("display")=="block")
			{
				$("#"+this.Cf.treeID+pid).slideUp("fast");
				$("#img"+this.Cf.treeID+pid+">img").attr("src",this.GetLeftImg($("#img"+this.Cf.treeID+pid+">img").attr("src")));
				$("#fd"+this.Cf.treeID+pid).css({background:"url("+this.Cf.imgPath+"folder.gif) no-repeat "+this.Cf.iconWidth+"px "+this.Cf.iconTop+"px"});
			}
			else
			{
				$("#"+this.Cf.treeID+pid).slideDown("fast");
				$("#img"+this.Cf.treeID+pid+">img").attr("src",this.GetLeftImg($("#img"+this.Cf.treeID+pid+">img").attr("src")));
				$("#fd"+this.Cf.treeID+pid).css({background:"url("+this.Cf.imgPath+"folder-open.gif) no-repeat "+this.Cf.iconWidth+"px "+this.Cf.iconTop+"px"});
			}
			return;
		}
		else
		{
			var folderImg=$("#fd"+this.Cf.treeID+pid).css("background");
			if(folderImg!=undefined && folderImg.toString().indexOf("leaf") == -1)
			{
				$("#fd"+this.Cf.treeID+pid).css({background:"url("+this.Cf.imgPath+"loading.gif) no-repeat "+this.Cf.iconWidth+"px "+this.Cf.iconTop+"px"});
				$("#fd"+this.Cf.treeID+pid).find("a").css({fontStyle:"italic"});
			}
		}
		var tree_=this;
		$.get(this.Cf.treeDataUrl+"?PID="+pid+"&"+this.Cf.w,function(obj){
			var Nodes=$("item",obj);
			for(var i=0;i<Nodes.length;i++)
			{
				n=new Node();
				n.ID=$(Nodes[i]).attr("id");
				n.text=$(Nodes[i]).attr("text");
				n.isLastNode=$(Nodes[i]).attr("isLastNode").toLowerCase()=="false"?false:true;
				n.childPanelOpened=$(Nodes[i]).attr("childPanelOpened").toLowerCase()=="false"?false:true;
				n.isLeaf=$(Nodes[i]).attr("isLeaf").toLowerCase()=="false"?false:true;
				nodes.push(n);
			}
			$("#fd"+tree_.Cf.treeID+pid).find("a").css({fontStyle:"normal"});
			tree_.ShowNode(nodes,deepth,pid);
		});
	};
	this.ShowNode=function(nd,deepth,pid)
	{
		var tB=new Array();
		if(nd.length<1)
		{
			if(parseInt(pid)==0)
			{
				$("#"+name).html("没有分类！");
			}
			return;
		}
		$("#fd"+this.Cf.treeID+pid).css({background:"url("+this.Cf.imgPath+"folder-open.gif) no-repeat "+this.Cf.iconWidth+"px "+this.Cf.iconTop+"px"});
		$("#img"+this.Cf.treeID+pid+">img").attr("src",this.GetLeftImg($("#img"+this.Cf.treeID+pid+">img").attr("src")));
		for(var i=0;i<nd.length;i++)
		{
			var nodeImg="folder.gif",nodeLeftImg="elbow-plus.gif",vLine="";
			var cursor="default";
			if(nd[i].isLeaf)
			{
				nodeImg="leaf.gif";
				if(nd[i].isLastNode)
				{
					nodeLeftImg="elbow-end.gif";
				}
				else
				{
					nodeLeftImg="elbow.gif";
				}
			}
			else
			{
				if(nd[i].isLastNode) nodeLeftImg="elbow-end-plus.gif";
				cursor="pointer";
			}
			
			var lary = new Array();
			var la;
			
			var pid_=pid;
			while(pid_!=0)
			{
				lary.push($("#nd"+this.Cf.treeID+pid_).attr("isLastNode"));
				pid_=parseInt($("#nd"+this.Cf.treeID+pid_).attr("pid"));
			}
			la=lary.reverse();
			
			var l=new Array();
			for(var d=0;d<deepth-1;d++)
			{
				if(la[d]=="false") vLine="background:url("+this.Cf.imgPath+"elbow-line.gif) repeat-y;";
				l.push("<div style=\"float:left;width:");
				l.push(this.Cf.iconWidth);
				l.push("px;height:18px;");
				l.push(vLine);
				l.push("\"></div>");
				vLine="";
			}

			tB.push("<div name=\"yingxutong\" style=\"height:18px;width:auto;\">");
			tB.push(l.join(""));
			tB.push("<div name=\"yingxutong\" style=\"float:left;height:18px;width:auto;\" id=\"nd");tB.push(this.Cf.treeID);tB.push(nd[i].ID);tB.push("\" pid=\"");tB.push(pid);tB.push("\" isLastNode=\"");tB.push(nd[i].isLastNode);tB.push("\">");
			tB.push("	<div name=\"yingxutong\" id=\"fd");tB.push(this.Cf.treeID);tB.push(nd[i].ID);tB.push("\" style=\"height:18px;width:auto;background:url(");tB.push(this.Cf.imgPath);tB.push(nodeImg);tB.push(") no-repeat ");tB.push(this.Cf.iconWidth);tB.push("px ");tB.push(this.Cf.iconTop);tB.push("px;\">");
			tB.push("		<div name=\"yingxutong\" id=\"img");tB.push(this.Cf.treeID);tB.push(nd[i].ID);tB.push("\" style=\"float:left;height:100%;width:34px;cursor:");tB.push(cursor);tB.push(";\">");
			tB.push("			<img name=\"yingxutong\" src=\"");tB.push(this.Cf.imgPath);tB.push(nodeLeftImg);tB.push("\" />");
			tB.push("		</div>");
			tB.push("		<div name=\"yingxutong\" style=\"float:left;height:100%;width:auto;margin-left:3px;\"><a name=\"yingxutong\" style=\"cursor:pointer;\">");tB.push(nd[i].text);tB.push("</a></div>");
			tB.push("	</div>");
			tB.push("</div></div>");
			tB.push("<div style=\"width:auto;clear:both;height:auto;display:none\" id=\"");tB.push(this.Cf.treeID);tB.push(nd[i].ID);tB.push("\"></div>");
		}

		if(parseInt(pid)==0)
		{
			this.g(name).innerHTML="<div id=\""+this.Cf.treeID+pid+"\" pid=\"0\" style=\"width:90%;overflow:hidden;\" isLastNode=\"true\"><div style=\"width:1000px;\">"+tB.join("")+"<div></div>";
			if($("#"+name).css("display")=="block")
			{}else{
			$("#"+name).slideDown("fast");
			}
		}
		else
		{
			this.g(this.Cf.treeID+pid).innerHTML=tB.join("");
			$("#"+this.Cf.treeID+pid).hide();
			$("#"+this.Cf.treeID+pid).slideDown("fast");
		}
		for(var i=0;i<nd.length;i++){
			$("#img"+this.Cf.treeID+nd[i].ID)
			.bind("click",{pid:nd[i].ID,deepth:deepth,tree:this},this.ListNode_)
			.parent().find("a")
			.bind("click",{id:nd[i].ID,tree:this,deepth:deepth,pid:pid},eval(this.Cf.nodeCallFunction));
		}
		eval(this.Cf.callFunction);
	};
	this.g=function(n){return document.getElementById(n)};
	this.Clear=function()
	{
		this.Cf.treeID=name;
		this.g(name).innerHTML="";
	};
	this.reload=function(){this.Clear();this.DrawNode();};
	this.GetSelect=function(ID)
	{
		var pid_=ID;
		var lary=new Array();
		lary.push("}");
		while(pid_!=0)
		{
			lary.push(pid_);
			lary.push(",");
			pid_=parseInt($("#nd"+this.Cf.treeID+pid_).attr("pid"));
		}
		lary.pop();
		lary.push("{");
		var i=0;
		while(i++<120){lary.push(" ");}
		lary.push($("#fd"+this.Cf.treeID+ID).find("a").html());
		var la=new Array();
		la=lary.reverse();
		return la.join("");
	};
}
