
function TreeNode(){
	var nodeE;//树节点
	var headE;//图标部分节点
	var nameE;//名称部分节点
	var subE;//子节点部分节点


	var expE;//展开操作图标节点
	var cloE;//合拢操作图标节点
	var expE_;//末端展开操作图标节点
	var cloE_;//末端合拢操作图标节点
	var lineTE;//T形线图标
	var lineLE;//L形线图标
	var lineIE;//I形线图标
	var expIconE;//展开时的图标节点
	var cloIconE;//合拢时的图标节点
	var leafIconE;//叶子图标节点
	var tabIcon;//图标缓存
	var tg;//界面相关设置

	var state;//打开为1，关闭为0

	var isLeaf;//是否是叶子节点

	//////树结构部分///////
	var subs;
	var subSize;
	var parent;
	///////其他数据////////
	var name;
	///////////////////////
	var height;//显示用的高度
	var NODEHEIGHT;
	/////////////////////

	//name:节点名称
	//tg:节点样式(TreeGlobe)
	//parent:父节点
	//value:值
	this.init = function(name,tg,parent,value){
          	//alert("init");
		//数据结构添加;
		this.subs = new Array(0);
		this.subSize = 0;
		this.parent = parent;
		this.state = 0;

		this.name = name;
		//基本参数配置
		this.NODEHEIGHT = 20;
		///////////
		//节点设置
		this.nodeE = this.getIns(tg.nodeIns);
		this.nameE = this.getIns(tg.nameIns);
		this.headE = document.createElement("A");
		this.subE = this.getIns(tg.nameIns);

		this.nodeE.appendChild(this.headE);
		this.nodeE.appendChild(this.nameE);
		this.nodeE.appendChild(document.createElement("BR"));
		this.lineIE = this.getIconIns(tg.lineIE,true);//I形线图标
		this.lineIE.style.top = -2;
		this.lineIE.top = 0;
		this.lineIE.width = 1;
		var t = tg.subsIns.cloneNode();
		var tbody = document.createElement("TBODY");
		var tr = document.createElement("TR");
		var td1 = document.createElement("TD");
		var td2 = document.createElement("TD");
		t.appendChild(tbody);
		tbody.appendChild(tr);
		tr.appendChild(td1);
		tr.appendChild(td2);
		td1.appendChild(this.lineIE);
		td2.appendChild(this.subE);
		this.nodeE.appendChild(t);
		this.nameE.innerHTML = name;

		this.subE.style.display="none";

		//界面配置
		this.tg = tg;
		this.lineTE = this.getLineIns(tg.lineTE);//T形线图标
		this.lineLE = this.getLineIns(tg.lineLE);//L形线图标

		this.expE = this.getLineIns(tg.expE);//展开操作图标节点
		this.cloE = this.getLineIns(tg.cloE);//合拢操作图标节点
		this.expE_ = this.getLineIns(tg.expE_);//末端展开操作图标节点
		this.cloE_ = this.getLineIns(tg.cloE_);//末端合拢操作图标节点
		if(parent == null){
		  this.expIconE = this.getIconIns(tg.rootIconE);//展开时的图标节点
		  this.cloIconE = this.getIconIns(tg.rootIconE,true);//合拢时的图标节点
		}
		else{
		  this.expIconE = this.getIconIns(tg.expIconE);//展开时的图标节点
		  this.cloIconE = this.getIconIns(tg.cloIconE,true);//合拢时的图标节点
		}
		this.leafIconE = this.getIconIns(tg.leafIconE);//叶子图标节点
		///////////
		//添加图标功能
		if(parent != null){
			this.expE.onclick = this.expAct;
			this.expE_.onclick = this.expAct;
			this.cloE.onclick = this.cloAct;
			this.cloE_.onclick = this.cloAct;
		}

		//设置节点的缩进量
		this.nodeE.style.left = tg.tab;
		this.renew();
	}
	this.getIns = function(src){
		var e = src.cloneNode();e.ownner = this;
		return e;
	}
	this.getIconIns = function(src,show){
		var e = src.cloneNode();e.ownner = this;
		this.headE.appendChild(e);
		if(show)e.style.display="inline";
		return e;
	}
	this.getLineIns = function(src,show){
		var e = null;
		if(this.parent != null){
			e = this.getIconIns(src,show);
		}
		else{
			e = this.getIconIns(this.tg.blankE,show);
		}
		e.height = "20";
		return e;
	}

	this.setLeaf = function(){//设置为叶子节点
		this.isLeaf = true;
		this.setHeadIcon(true);
		this.renew();
	}

	this.isLastChild = function(){
		if(!this.parent)return false;
		if(this.parent.subSize < 2)return true;
		return this.parent.subs[this.parent.subSize - 1] == this;
	}

	this.renew = function(){
		this.lineLE.style.display="none";
		this.lineTE.style.display="none";
		this.expE_.style.display="none";
		this.expE.style.display="none";
		this.cloE_.style.display="none";
		this.cloE.style.display="none";
		this.height = this.NODEHEIGHT;
		if(this.state == 1){
			for(var i = 0;i<this.subSize;i++){
				this.height += this.subs[i].height;
			}
		}
		var i1 = this.lineLE;
		var i2 = this.lineTE;
		this.lineIE.style.left = 7;
		this.lineIE.height = 0;
		if(this.parent && !this.isLastChild()){
			this.lineIE.height = this.height - this.NODEHEIGHT;
		}
		if(!this.isLeaf){
			if(this.state == 0){
				i1 = this.expE_;i2 = this.expE;
			}
			else{
				i1 = this.cloE_;i2 = this.cloE;
			}
		}
		if(this.isLastChild()){
			i1.style.display="inline";
		}
		else{
			i2.style.display="inline";
		}
		var e = this.parent;
		while(e){
		  e.renew();
		  e = e.parent;
		}
	}

	this.addNode = function(name,value){
          	//alert("addNode");
                //alert(name);
		var node = this.tg.createTreeNode(name,this,value);
		this.subE.appendChild(node.nodeE);
		this.subs[this.subSize] = node;
		this.subSize++;
		if(this.subSize>1){
			this.subs[this.subSize-2].renew();
		}
		node.renew();
		return node;
	}

	this.loadSubs = function(){
		if(this.loaded)return;
		this.tg.loadSubsAjax(this);
	}


	///////////////////////////事件函数/////////////////////////////////////
	this.expAct = function(){
          	//alert("expAct");
		var o = this.ownner;
		if(o == null)o = this;
		o.state = 1;
		o.loadSubs();
		o.renew();
		if(o.subE.childNodes.length>0){
			o.subE.style.display="inline";
		    o.setHeadIcon(false);
		}
	}
	this.cloAct = function(){
		var o = this.ownner;
		if(o == null){
			o = this;
		}
		o.state = 0;
		o.renew();
		o.subE.style.display="none";
		o.setHeadIcon(true);
	}
	this.setHeadIcon = function(clo){
		if(this.isLeaf){
			this.expIconE.style.display="none";
			this.cloIconE.style.display="none";
			this.leafIconE.style.display="inline";
		}
		else{
			this.expIconE.style.display=clo?"none":"inline";
			this.cloIconE.style.display=clo?"inline":"none";
			this.leafIconE.style.display="none";
		}
	}
}

function TreeGlobe(){//树的全局参数设置
	var iconPath;//图标的路径

	var expE;//展开操作图标节点
	var cloE;//合拢操作图标节点
	var expE_;//末端展开操作图标节点
	var cloE_;//末端合拢操作图标节点
	var lineTE;//T形线图标
	var lineLE;//L形线图标
	var lineIE;//I形线图标
	var blankE;//空白区域图标
	var expIconE;//展开时的图标节点
	var cloIconE;//合拢时的图标节点
	var leafIconE;//叶子图标节点
	var rootIconE;//根图标节点

	var nodeIns;//树节点样例
	var nameIns;//树节点名称部分样例
	var imgIns;//图标节点样例
	var subsIns;//子节点部分样例

	var tab;

	var url;

	var ajaxQueue;// ajax发送队列
	var waitBox;//

	this.getRootNode = function(xml,url,id,name,value){
		this.ajaxQueue = new AjaxTaskQueue();
		this.url = url;
		this.tab = 13
		var list = xml.childNodes;
		for(var i = 0;i<list.length;i++){
			var item = list.item(i);
			if(item.tprop == "node"){
				this.nodeIns = item;
			}
			else if(item.tprop == "icon"){
				this.imgIns = item;
			}
			else if(item.tprop == "subs"){
				this.subsIns = item;
			}
			else if(item.tprop == "name"){
				this.nameIns = item;
			}
			else if(item.tprop == "icon_path"){
				this.iconPath = item.value;
			}
		}
		this.expE = this.getImg("open.gif");
		this.cloE = this.getImg("close.gif");
		this.expE_=this.getImg("open_.gif");
		this.cloE_=this.getImg("close_.gif");
		this.lineTE=this.getImg("line_t.gif");
		this.lineLE=this.getImg("line_l.gif");
		this.lineIE=this.getImg("line_i.gif");
		this.blankE = this.getImg("blank.gif");
		this.expIconE=this.getImg("node_open.gif");
		this.cloIconE=this.getImg("node_close.gif");
		this.leafIconE=this.getImg("leaf.gif");
		this.rootIconE=this.getImg("root.gif");
		var node = this.createTreeNode(name,null,value);
		node.id = id;
		this.waitBox = document.getElementById("treeWaitBox");
		return node;
	}

	this.getImg = function(name){
		var e = this.imgIns.cloneNode();
		//e.src="file://D:/zyp/layoutTest/layoutTest/waf/layout/image/"+name;
		e.src=this.iconPath+name;
		e.width = "16";
		e.height = "16";
		return e;
	}
	this.createTreeNode = function(name,parent,value){
		node = new TreeNode();
		node.init(name,this,parent,value);
		return node;
	}

	this.showWait = function(src){
		src.waitBox.innerHTML = "正在加载...";
	}

	this.hideWait = function(src){
		src.waitBox.innerHTML = "&nbsp;";
	}

	this.loadSubsAjax = function(node){
          	//alert("loadSubsAjax");
		var at = new AjaxTask();
		at.url = this.url;
                //alert(node.id);
		at.msg = "funcid=listChildrenXML&groupId="+node.id;
		at.beforeLoad = func_bind(this.showWait,[this]);
		at.afterLoad = func_bind(this.hideWait,[this]);
		at.load = func_bind(this.loadSubs,[at,this,node]);
		this.ajaxQueue.addTask(at);
	}

	this.loadSubs = function(src,tg,node){
          	//alert("loadSubs,参数");
		src.loadDoc();
		var xmldoc = src.msg_doc;
		if(xmldoc && xmldoc.documentElement){
		  var list = xmldoc.documentElement.childNodes;
		  if(!list){
		    return;
		  }
		  for(j=0;j<list.length;j++){
			var nd = node.addNode(name,list[j]);
			var value = list[j];
			nd.id = value.getAttribute("id");
			tg.modifyNode(nd,value,node);
			if(value.getAttribute("leaf")){
			  nd.setLeaf();
			}
		  }
		  node.loaded = true;node.expAct();
		}
	}

	this.modifyNode = function(node,value,parent){
	}

	this.findByProp = function(node,name){
	  if(node.getAttribute("prop") == name){
		return node;
	  }
	  var list = node.childNodes;
	  for(var i = 0;i<list.length;i++){
		var item = this.findByProp(list.item(i),name);
		if(item)return item;
	  }
	  return null;
	}
}
