/**
Ãû³Æ:		jsÄ¿Â¼Ê÷
Name:		jsTree
Release:	0.01
Author:		Ð¡Öñ
Revision:	2005-10-29 16:30:40
Licenses:	GPL(The GNU General Public License)
Descript:	jsÄ¿Â¼Ê÷
*/


var Icon={
		open		:'images1/folderopen.gif',
		homepage	:'images1/Menu/home.gif',
		close		:'images1/ball25.gif',
		file		:'images1/file.gif',
		help		:'images1/help.gif',
		join		:'images1/T.gif',
		joinbottom	:'images1/L.gif',
		plus		:'images1/Tplus.gif',
		plusbottom	:'images1/Lplus.gif',
		minus		:'images1/Tminus.gif',
		minusbottom	:'images1/Lminus.gif',
		blank		:'images1/blank.gif',
		line		:'images1/I.gif',
		mudule		:'images1/mudule.gif',
		root		:'images1/root.gif'
};

function PreLoad(){
	for(i in Icon){
	var tem=Icon[i]
	Icon[i]=new Image()
	Icon[i].src=tem
	}
}
PreLoad()

function Cls_jsTree(obj,target,ExpandOne){
	this.obj=obj;
	this.Tree=new Node(0)
	this.Root=null
	this.Nodes=new Array()
	this.target=target?target:"_self";
	this.ie=document.all?1:0;
	this.ExpandOne=ExpandOne?1:0
}

function Node(id,pid,txt,link,title,target,xicon){
	this.Index=null;
	this.id=id;
	this.pid=pid
	this.parent=null
	this.txt=txt?txt:"New Item"
	this.link=link
	this.title=title?title:this.txt
	this.target=target
	this.xicon=xicon
	this.indent=""
	this.hasChild=false;
	this.lastNode=false;
	this.open=false
}

Cls_jsTree.prototype.add=function(id,pid,txt,link,title,target,xicon){
	target=target?target:this.target
	var nNode=new Node(id,pid,txt,link,title,target,xicon);
	if(pid==this.Tree.id)nNode.open=true;
	nNode.Index=this.Nodes.length
	this.Nodes[this.Nodes.length]=nNode
}

Cls_jsTree.prototype.InitNode=function(oNode){
	var last;
	for(i=0;i<this.Nodes.length;i++){
		if(this.Nodes[i].pid==oNode.id){this.Nodes[i].parent=oNode;oNode.hasChild=true}
		if(this.Nodes[i].pid==oNode.pid)last=this.Nodes[i].id
	}
	if(last==oNode.id)oNode.lastNode=true
}

Cls_jsTree.prototype.DrawLine=function(pNode,oNode){
	oNode.indent=pNode.indent+(oNode.pid!=this.Tree.id&&oNode.pid!=this.Root.id?("<img align='absmiddle' src='"+(pNode.lastNode?Icon.blank.src:Icon.line.src)+"'>"):'')
}

Cls_jsTree.prototype.DrawNode=function(nNode,pNode){
	var str=""
	var indents=""
	var imgFolder=""
	var nid=nNode.id
	var nIndex=nNode.Index;
	this.DrawLine(pNode,nNode)
	if(nNode.hasChild)
		indents=nNode.indent+(this.Tree.id!=nNode.pid?("<img align='absmiddle' onclick='"+this.obj+".Toggle("+nIndex+",this)' src='"+(nNode.lastNode?(nNode.open?Icon.minusbottom.src:Icon.plusbottom.src):(nNode.open?Icon.minus.src:Icon.plus.src))+"' id='icon"+nid+"' style='cursor:hand' border=0>"):"")
	else
		indents=nNode.indent+(nNode.pid==this.Tree.id?'':("<img style='cursor:hand' align='absmiddle' src='"+(nNode.lastNode?Icon.joinbottom.src:Icon.join.src)+"'>"))
	imgFolder="<img id='folder"+nid+"' align='absmiddle' style='cursor:hand' src='"+(nNode.xicon?nNode.xicon:(nNode.hasChild?(nNode.open?Icon.open.src:Icon.close.src):Icon.file.src))+"'>"
	str+="<div class='node'><nobr>"+indents+"<span onclick='"+this.obj+".Toggle("+nIndex+",this)' style='cursor:hand'>"+imgFolder+this.DrawLink(nNode)+"</span></nobr></div>"
	if(nNode.hasChild){
		str+="<div id='Child"+nid+"' style='cursor:hand' style='display:"+(nNode.open?"":"none")+"'>"
		str+=this.DrawTree(nNode)
		str+="</div>"
	}
	return str;
}

Cls_jsTree.prototype.DrawTree=function(pNode){
	var str=""
	for(var i=0;i<this.Nodes.length;i++)
		if(this.Nodes[i].pid==pNode.id)
		str+=this.DrawNode(this.Nodes[i],pNode)
		return str
}

Cls_jsTree.prototype.DrawLink=function(oNode){
	var str=""
	sArray=oNode.txt.split('|');
	dispTxt=sArray[0];
	dispTitle=sArray[1];
	if(oNode.txt.indexOf("|",0)==-1){dispTitle=""}
	str+=" <span id='NodeItem"+oNode.id+"' tabindex='1' onclick='"+this.obj+".Select(this)' onMOver='"+dispTitle+"'>"+(oNode.link?("<a href='"+oNode.link+"' target='"+oNode.target+"' onMOver='"+dispTitle+"'>"+dispTxt+"</a>"):dispTxt)+"</span>"
	return str
}

Cls_jsTree.prototype.toString=function(){
	var str=""
	for(var i=0;i<this.Nodes.length;i++){
		if(!this.Root)
		if(this.Nodes[i].pid==this.Tree.id)this.Root=this.Nodes[i]
		this.InitNode(this.Nodes[i])
	}
	str+=this.DrawTree(this.Tree)
	return str
}

Cls_jsTree.prototype.Toggle=function(nIndex,o){
	var nNode=this.Nodes[nIndex]
	//o.blur();
	if(!nNode.hasChild)return;
	if(nNode.open)this.Collapse(nNode)
	else this.Expand(nNode)
}

Cls_jsTree.prototype.Expand=function(nNode){
	var nid=nNode.id
	var node=this.GetElm('Child'+nid)
	var oicon=this.GetElm('icon'+nid)
	node.style.display=''
	var img1=new Image()
	img1.src=(nNode.lastNode?Icon.minusbottom.src:Icon.minus.src)
	if(oicon)oicon.src=img1.src
	if(!nNode.xicon){
		var img2=new Image()
		img2.src=Icon.open.src
		this.GetElm("folder"+nid).src=img2.src
	}
	if(this.ExpandOne)this.CloseOtherItem(nNode);
	nNode.open=true
}

Cls_jsTree.prototype.Collapse=function(nNode){
	var nid=nNode.id
	var node=this.GetElm('Child'+nid)
	var oicon=this.GetElm('icon'+nid)
	node.style.display='none'
	var img1=new Image()
	img1.src=(nNode.lastNode?Icon.plusbottom.src:Icon.plus.src)
	if(oicon)oicon.src=img1.src
	if(!nNode.xicon){
		var img2=new Image()
		img2.src=Icon.close.src
		this.GetElm("folder"+nid).src=img2.src
	}
//	alert ("folder"+nid);
	nNode.open=false
}

Cls_jsTree.prototype.ExpandAll=function(){
	for(i=0;i<this.Nodes.length;i++)
		if(this.Nodes[i].hasChild)this.Expand(this.Nodes[i])
}

Cls_jsTree.prototype.CloseAll=function(){
	for(i=0;i<this.Nodes.length;i++) {
		if(this.Nodes[i].hasChild) {this.Collapse(this.Nodes[i]);}
	}
}

Cls_jsTree.prototype.CollapseAll=function(){
	for(i=0;i<this.Nodes.length;i++)
		if(this.Nodes[i].hasChild&&this.Nodes[i].pid!=this.Tree.id&&this.Nodes[i]!=this.Root)this.Collapse(this.Nodes[i])
}

Cls_jsTree.prototype.CloseOtherItem=function(nNode){
	for(var i=0;i<this.Nodes.length;i++)
		if(this.Nodes[i].pid==nNode.pid&&this.Nodes[i].open){this.Collapse(this.Nodes[i]);break}
}
Cls_jsTree.prototype.Select=function(objNode,flag){
	if(!this.current)this.current=objNode
	this.current.className=""
	objNode.className="highlight"
	this.current=objNode
	var a=objNode.getElementsByTagName("A")
//	if(a.length!=0&&flag)window.open(a[0].href,a[0].target);
	if(this.ie)objNode.focus()
}

Cls_jsTree.prototype.openTo=function(nIndex){
	if(nIndex<0||nIndex>=this.Nodes.length)return;
	var nNode=this.Nodes[nIndex]
	while(nNode){
		if(!nNode.open&&nNode.hasChild)this.Expand(nNode)
		nNode=nNode.parent
	}
	this.Select(this.GetElm("NodeItem"+this.Nodes[nIndex].id),true)
}

Cls_jsTree.prototype.GetElm=function(uid){
	return document.getElementById(uid)
}