/*引入CSS文件*/
var treeImgPath = "images/";

document.write("<style>");
document.write(".tree {");
document.write("font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;font-size: 11px;color: #666;white-space: nowrap;");
document.write("}");
document.write(".tree img {");
document.write("border: 0px;vertical-align: middle;");
document.write("}");
document.write(".tree a {");
document.write("color: #333;text-decoration: none;cursor:default;");
document.write("}");
document.write(".tree a.node, .tree a.nodeSel {");
document.write("white-space: nowrap;padding: 1px 2px 1px 2px;");
document.write("}");
document.write(".tree a.node:hover, .tree a.nodeSel:hover {");
document.write("color: #333;text-decoration: underline;");
document.write("}");
document.write(".tree a.nodeSel {");
document.write("background-color: #c0d2ec;");
document.write("}");
document.write(".tree .clip {");
document.write("overflow: hidden;");
document.write("}");
document.write("</style>");

/**
 * @static
 * @class
 * 辅助类,用于管理Tree实例并提供缺省节点展开方法
 */
TreeManager={
	trees:{},
	addTree:function(tree){
		this.trees[tree.id]=tree;
	},
	/**
	 * 获得treeId对应的树对象
	 * @param {string} treeId 树的id 
	 */
	getTree:function(treeId){
		return this.trees[treeId];
	},
	removeTree:function(treeId){
		this.trees[treeId]=null;
		delete this.trees[treeId];
	},
	/**
	 * 缺省的节点展开方法
	 * @param {string} treeId 树的id
	 * @param {string} nodeKey 要展开节点的key
	 */
	openTree : function(treeId,nodeKey){
		var tree=TreeManager.getTree(treeId);
		if(!tree){
			return;
		}
		var node=tree.getNodeByKey(nodeKey);
		if(!node){
			return;
		}
		var selector={"true":["collapseNode","hideSonNodes"],
				  "false":["expandNode","showSonNodes"]}
		var funcs=selector[node.available];
		node.available=!node.available;
		TreeViewDom[funcs[0]](node);
		node[funcs[1]]();
	}
}
/**
 * @class
 * 树对象模型类
 * @param {string} id 树对应的id
 */
Tree = function(id){
	/**
	 * 树的id
	 * @type {string}
	 */
	this.id=id;
	/**
	 * @private
	 * @type {array}
	 * 子节点数组
	 */
	this._nodes=[];
	/**
	 * 节点点击事件（函数名）
	 * @type {string}
	 */
	this.nodeClick="";
	/**
	 * 节点双击事件（函数名）
	 * @type {string}
	 */
	this.nodeDblClick=function(){return;};
	/**
	 * 当前选中节点
	 * @type {TreeNode}
	 */
	this.activeNode=null;
	/**
	 * @private
	 * @type {object}
	 * 节点key为索引的节点映射
	 */
	this._nodesMapping={};
}

Tree.prototype={
	/**
	 * 将节点加入到key索引映射中
	 * @param {TreeNode} node 节点对象
	 */
	addNodeToMapping:function(node){
		this._nodesMapping[node.key]=node;
	},
	/**
	 * 由节点key值获得节点
	 * @param {string} key 节点key值
	 */
	getNodeByKey:function(key){
		return this._nodesMapping[key];
	},
	/**
	 * 添加根节点
	 * @param {string} key 节点的key
	 * @param {string} text 节点显示的文字
	 * @param {string} url  节点对应的url
	 * @param {string} icon  节点非活动图标地址
	 * @param {string} activeicon 节点活动图标地址
	 * @param {string} expandMethod 节点展开方法
	 */
	addNode:function(key,text,url,icon,activeicon,expandMethod){
		var ic = icon;
		var aic = activeicon;
		if (ic==""){
			ic=treeImgPath+"folder.gif";
		}
		if (aic==""){
			aic=treeImgPath+"folderopen.gif"
		}
		var node=new TreeNode(key,text,url,ic,aic,expandMethod);
		node.deep=0;
		this._nodes.push(node);
		node.setTree(this);
		return node;
	},
	/**
	 * 获得子节点数组
	 */
	getNodes:function(){
		return this._nodes;
	},
	/**
	 * 渲染树到divId对应的DOM元素（该元素必须存在）
	 * @param {string} divId
	 */
	refresh:function(divId){
		document.getElementById(divId).innerHTML =  new TreeView(this).getCode();
		TreeManager.addTree(this);
	},
	/**
	 * 收缩展开的节点
	 */
	contract:function(){
		var treeNodes =this._nodesMapping;
		for (var i in treeNodes){
			var treeNode=treeNodes[i];
			TreeViewDom.collapseNode(treeNode);
			if(treeNode.parentNode){
				TreeViewDom.hideNode(treeNode);
			}
			treeNode.available=false;
		}
	},
	/**
	 * 聚焦到nodeKey对应的节点，并根据树结构从其祖先节点展开至该节点
	 * @param {string} nodeKey 节点的key
	 */
	focusNode : function(nodeKey){
		var node=this.getNodeByKey(nodeKey);
		if(!node){
			return;
		}
		this.setActive(node);
		var parent=node.parentNode;
		var pNode=null;
		while (parent!=null){
			parent.available=true;
			pNode=parent;
			parent=parent.parentNode;
		}
		if(pNode){
			pNode.showSonNodes();
		}
	},
	/**
	 * 将节点设置为活动状态（选中）
	 * @param {TreeNode} node 节点对象
	 */
	setActive:function(node){
		this.bluricon();
		this.activeNode=node;
		TreeViewDom.setNodeActive(node);
	},
	/**
	 * 取消当前活动节点的选中状态
	 */
	bluricon:function(){
		var node=this.activeNode
		if(!node){
			return;
		}
		TreeViewDom.blurNode(node);
		this.activeNode=null;
	},
	/**
	 * 展开整棵树
	 */
	outspread:function(){
		var treeNodes =this._nodesMapping;
		for (var i in treeNodes){
			var treeNode=treeNodes[i];
			TreeViewDom.expandNode(treeNode);
			treeNode.available=true;
		}
		var childNodes=this.getNodes();
		for(var i=0;i<childNodes.length;i++){
			childNodes[i].showSonNodes();
		}
	}
}
/**
 * @class
 * 树节点模型类
 * @param {string} key 节点的key
 * @param {string} text 节点显示的文字
 * @param {string} url  节点对应的url
 * @param {string} icon  节点非活动图标地址
 * @param {string} activeicon 节点活动图标地址
 * @param {string} expandMethod 节点展开方法
 */
TreeNode = function(key,text,url,icon,activeicon,expandMethod){
	/**
	 * 节点id,由TreeUtil辅助类自动生成
	 * @type {string}
	 */
	this.id=TreeUtil.genNodeId();
	/**
	 * 节点的key（值必须在节点所在树中唯一）
	 * @type {string}
	 */
	this.key=key;
	this.deep;
	/**
	 * 节点的父节点
	 * @type {TreeNode}
	 */
	this.parentNode=null;
	/**
	 * 节点显示的文字
	 * @type {string}
	 */
	this.text=text;
	/**
	 * 节点对应的url
	 * @type {string}
	 */
	this.url=url;
	/**
	 * 节点是否活动
	 * @type {boolrsn}
	 */
	this.active=false;
	/**
	 * @type {string}
	 */
	this.expandMethod=expandMethod||"TreeManager.openTree"
	/**
	 * 节点非活动图标地址
	 * @type {string}
	 */
	this.icon=icon||treeImgPath+"folder.gif";
	/**
	 * 节点活动图标地址
	 * @type {string}
	 */
	this.activeicon=activeicon||treeImgPath+"folderopen.gif";
	this.available=true;
	this._nodes=[];
	this._tree;
}

TreeNode.prototype={
	/**
	 * 获得节点所在的树对象
	 */
	getTree:function(){
		return this._tree;
	},
	setTree:function(tree){
		this._tree=tree;
		tree.addNodeToMapping(this);
	},
	/**
	 * 添加子节点
	 * @param {string} key 节点的key
	 * @param {string} text 节点显示的文字
 	 * @param {string} url  节点对应的url
 	 * @param {string} icon  节点非活动图标地址
 	 * @param {string} activeicon 节点活动图标地址
 	 * @param {string} expandMethod 节点展开方法
	 */
	addNode:function(key,text,url,icon,activeicon,expandMethod){
		var node=new TreeNode(key,text,url,icon,activeicon,expandMethod);
		this._nodes.push(node);
		node.setTree(this.getTree());
		node.parentNode=this;
		node.deep=this.deep+1;
		return node;
	},
	/**
	 * 获得子节点数组
	 */
	getNodes:function(){
		return this._nodes;
	},
	/**
	 * 该节点是否有子节点
	 */
	hasSon:function(){
		return this._nodes.length>0;
	},
	/**
	 * 该节点是否为父节点的最后一个子节点
	 */
	isLastSon :function(){
		var parentNode=this.parentNode||this.getTree();
		var nodes=parentNode.getNodes();
		return nodes[nodes.length-1] == this
	},
	/**
	 * 该节点是否为父节点的第一个子节点
	 */
	isFirstSon : function(){
		var parentNode=this.parentNode||this.getTree();
		return	parentNode.getNodes()[0] == this
	},
	/**
	 * 显示子节点
	 */
	showSonNodes:function(){
		var sons = this.getNodes();
		if(!this.available){
			return;
		}
		for (var i=0;i<sons.length;i++){
			var son=sons[i];
			TreeViewDom.showNode(son);
			son.showSonNodes();	
		}
	},
	/**
	 * 隐藏子节点
	 */
	hideSonNodes:function(){
		var sons = this.getNodes();
		for (var i=0;i<sons.length;i++){
			var son=sons[i];
			TreeViewDom.hideNode(son);
			son.hideSonNodes();	
		}
	},
	refresh:function(){
		div=document.getElementById(this.id);
		
	}
}
/**
 * @class
 * @static
 * Tree辅助类
 */
TreeUtil ={
	nodeCount:0,
	nodeIdPrefix:"treeNode_",
	/**
	 * 自动生成节点id
	 */
	genNodeId:function(){
		return this.nodeIdPrefix+this.nodeCount++;
	},
	/**
	 * 获得id为treeId中nodeKey对应的节点
	 * @param {string} treeId 树的id
	 * @param {string} nodeKey 节点的key
	 */
	getTreeNode:function(treeId,nodeKey){
		var tree=TreeManager.getTree(treeId);
		if(!tree) return null;
		return tree.getNodeByKey(nodeKey);
	},
	/**
	 * 触发id为treeId中nodeKey对应的节点的点击事件
	 * @param {string} treeId 树的id
	 * @param {string} key 节点的key
	 */
	clickEvent:function(treeId,key){
		var node=this.getTreeNode(treeId,key);
		if(!node){
			return;
		}
		var tree=TreeManager.getTree(treeId);
		var activeNode=tree.activeNode;
		tree.nodeClick(node);
		if(!(node==activeNode)){
			tree.setActive(node);
		}
	},
	/**
	 * 触发id为treeId中nodeKey对应的节点的点击事件
	 * @param {string} treeId 树的id
	 * @param {string} key 节点的key
	 */
	dblClickEvent:function(treeId,key){
		var node=this.getTreeNode(treeId,key);
		if(!node){
			return;
		}
		var tree=TreeManager.getTree(treeId);
		var activeNode=tree.activeNode;
		tree.nodeDblClick(node);
		if(!(node==activeNode)){
			tree.setActive(node);
		}
	}
}
/**
 * @class
 * @param {Tree} tree 树对象
 * 生成树对象对应的视图 
 */
TreeView = function(tree){
	this.htmlCode =[];
	this.rf=function(node){
		var nodes=node.getNodes();
		for (var i=0;i<nodes.length;i++){
			var thenode = nodes[i];
			var tmpstr="";
			this.htmlCode.push('<div class="tree" id="'+thenode.id+'" key="'+thenode.key+'">');
			
			var tmpnode = thenode.parentNode;
			var tmpstr = "";
			while (tmpnode!=null){
				if (tmpnode.isLastSon()){
					tmpstr = "<img src=\""+treeImgPath+"empty.gif\">"+tmpstr;
				}else{
					tmpstr = "<img src=\""+treeImgPath+"line.gif\">"+tmpstr;
				}
				tmpnode=tmpnode.parentNode;
			}
			this.htmlCode.push(tmpstr);
			
			if (tree.AjaxGet==true){

				var picName="plus.gif";
				var hasSon = thenode.hasSon();
				if (thenode.isFirstSon()&&thenode.parentNode==null){
					picName="top_plus_only.gif";
				}
				else if(thenode.isLastSon()){
				   picName="plusbottom.gif"
				}

			}else{
				var picName="minus.gif";
				var hasSon = thenode.hasSon();
				if (thenode.isFirstSon()&&thenode.parentNode==null){
					picName="top_minus_only.gif";
				}
				else if(thenode.isLastSon()){
				   picName="minusbottom.gif"
				}
				
				if(!hasSon){
					if(thenode.isLastSon()){
						picName="joinbottom.gif";
					}
					else{
						if (thenode.isFirstSon()&&thenode.parentNode==null){
							picName="top_join.gif";
						}else{
							picName="join.gif";
						}
					}
				}
			}
			this.htmlCode.push('<img src="'+treeImgPath+picName+'" id="expandImg_'+thenode.id+'" onclick="'+thenode.expandMethod+'(\''+tree.id+'\',\''+thenode.key+'\')">');
			if (thenode.active){
				this.htmlCode.push("<img id=\"activeImg_"+thenode.id+"\" src=\""+thenode.activeicon+"\">");
			}else{
				this.htmlCode.push("<img id=\"activeImg_"+thenode.id+"\" src=\""+thenode.icon+"\">");
			}
			this.htmlCode.push('<a href="javascript:void(0)" id="linker_'+thenode.id+'" ondblclick="TreeUtil.dblClickEvent(\''+tree.id+'\',\''+thenode.key+'\')" onclick="TreeUtil.clickEvent(\''+tree.id+'\',\''+thenode.key+'\');return false;" title="'+thenode.text+'">'+thenode.text+'</a>');
			this.htmlCode.push("</div>");
			
			if (thenode.hasSon()){
				this.rf(thenode);
			}
		}
	}
    
	this.getCode = function(){
		this.rf(tree);
		return this.htmlCode.join("");
	}
	
	this.refreshNode=function(thenode){
		this.rf(thenode);
		var div=document.getElementById(thenode.id);
		var tempStr=div.innerHTML
		var pos=0;
		pos=tempStr.search(/<\s*div/i);
		if(pos!=-1){
		   tempStr=tempStr.substr(0,pos);
		}
		div.innerHTML=""
		div.innerHTML =tempStr+this.htmlCode.join("");
	}
}
/**
 * @class
 * @static
 * 辅助类，将DOM操作从模型类中分离出来
 */
TreeViewDom={
	/**
	 * 将节点置为活动状态
	 * @param {TreeNode} node 节点对象
	 */
	setNodeActive:function(node){
		var img= document.getElementById("activeImg_"+node.id);
		var linker =document.getElementById("linker_"+node.id);
		img.src=node.activeicon;
		linker.className = "nodeSel";
	},
	/**
	 * 展开节点
	 * @param {TreeNode} node 节点对象
	 */
	expandNode:function(node){
		var theimg=this.getImgNode(node);
		theimg.src=theimg.src.replace("plus","minus");
	},
	/**
	 * 收缩节点
	 * @param {TreeNode} node 节点对象
	 */
	collapseNode:function(node){
		var theimg=this.getImgNode(node);
		theimg.src=theimg.src.replace("minus","plus");
	},
	/**
	 * 获得节点展开/收缩图片的IMG对象
	 * @param {TreeNode} node 节点对象
	 */
	getImgNode:function(node){
		return document.getElementById("expandImg_"+node.id);
	},
	/**
	 * 取消节点的活动状态
	 * @param {TreeNode} node 节点对象
	 */
	blurNode:function(node){
		var img= document.getElementById("activeImg_"+node.id);
		var linker =document.getElementById("linker_"+node.id);
		img.src=node.icon;
		linker.className = "";
	},
	/**
	 * 显示节点
	 * @param {TreeNode} node 节点对象
	 */
	showNode:function(node){
		try{
			document.getElementById(node.id).style.display="block";
		}catch (e){
			alert(e.message);
		}
	},
	/**
	 * 隐藏节点
	 * @param {TreeNode} node 节点对象
	 */
	hideNode:function(node){
		document.getElementById(node.id).style.display="none";
	}
}

