﻿function TreeMenu(name){
  this.name = name;
	this.treeBody = null;
  this.iconPath = "./";
  this.icons    = {
    L0        : "L0.gif",  //┏
    L1        : "L1.gif",  //┣
    L2        : "L2.gif",  //┗
    L3        : "L3.gif",  //━
    L4        : "L4.gif",  //┃
    P0        : "P0.gif",  //＋┏
    P1        : "P1.gif",  //＋┣
    P2        : "P2.gif",  //＋┗
    P3        : "P3.gif",  //＋━
    M0        : "M0.gif",     //－┏
    M1        : "M1.gif",     //－┣
    M2        : "M2.gif",     //－┗
    M3        : "M3.gif",     //－━
    blank     : "blank.gif",     //空白图
    root      : "root.gif",   //缺省的根节点图标
    folder    : "folder.gif", //缺省的文件夹图标
    file      : "file.gif",   //缺省的文件图标
    exit      : "exit.gif"
  };
  
  this.indexOfEntries = new Array;
  this.nEntries = 0;
  this.auxs = new Array;
}

TreeMenu.prototype.preloadIcons = function(){
  for(var i = 0; i < this.icons.length; i++){
    var iconName = this.icons[i];
    var icon = new Image;
    icon.src = this.getIcon(iconName);
  }
}


TreeMenu.prototype.getIcon = function(name){
  return this.iconPath + this.icons[name];
}

TreeMenu.prototype.addItem = function(level, text, url, target, desc, iconSrc,isOpen){
	if(level > 0){
		this.auxs[level] =
				this.auxs[level - 1].addChild(new Folder(this, text, url, target, desc, iconSrc,isOpen));
	}
	else{
		this.auxs[0] = new Folder(this, text, url, target, desc, iconSrc);
	}
}


TreeMenu.prototype.initialize = function(){
	document.write("<table><tr><td id=\"treeMenu_" + this.name+ "\">");
	document.write("</td></tr></table>");
	this.treeBody = findObj("treeMenu_" + this.name);
}

TreeMenu.prototype.draw = function(){
	if(this.treeBody){
		var foldersTree;
		foldersTree = this.auxs[0];
		foldersTree.initialize(0, 1, "");
	}
}

TreeMenu.prototype.clear = function(){
	if(this.treeBody){
		this.indexOfEntries = new Array;
		this.nEntries = 0;
		this.auxs = new Array;
		this.treeBody.innerHTML = "";
	}
}
TreeMenu.prototype.expandAll = function(){
	  for(k=0;k<this.nEntries;k++){
	  clickOnNode(this, k);
	  }
}


function clickOnNode(treeMenu, folderID){
	var clickedFolder = 0;
	var state = 0;
	clickedFolder = treeMenu.indexOfEntries[folderID];
	state = clickedFolder.isOpen;
	if( clickedFolder.children.length>0 ){
		clickedFolder.setState(!state);
	}
}

//目录对象
function Folder(treeMenu, text, url, target, desc, iconSrc, isOpen){
  this.treeMenu = treeMenu;
	this.text = text;
	this.url = url;
	this.target = target;
	this.desc = desc;
	this.id = -1;
	this.navObj =0;
	this.nodeImg =0;
	this.isLastNode = 0;
	this.father = 0;
	this.isOpen = isOpen;
	if(iconSrc)
    this.iconSrc = iconSrc;
  else
    this.iconSrc = this.treeMenu.iconPath + this.treeMenu.icons["folder"];    
	this.children = new Array;
	this.nChildren = 0;
	this.selectedChildren = 0;
	this.leftSide="";
	this.hasDrawed = false;
	this.fatherDiv = 0;
	this.checked = false;
}

Folder.prototype.getName = function(){
  return ("folder_" + this.treeMenu.name + "_" + this.id);
}

Folder.prototype.getNodeIconName = function(){
  return ("nodeIcon_" + this.treeMenu.name + "_" + this.id);
}

Folder.prototype.getImageName = function(){
  return ("image_" + this.treeMenu.name + "_" + this.id);
}

Folder.prototype.addChild = function(childObject){
	this.children[this.nChildren] = childObject;
	childObject.father= this;
	this.nChildren++;
	return childObject;
}


Folder.prototype.initialize = function(level, lastNode, leftSideOther){
	var j=0;
	var i=0;
	var numberOfFolders;
	var numberOfDocs;
	var nc;
    nc = this.children.length;
	this.createEntryIndex();
	
	if (level>0){
		if (lastNode){
			if( nc>0){			
			this.leftSide = leftSideOther+"<img id=\"" + this.getNodeIconName() + "\" src="+ this.treeMenu.getIcon("P2") +" border=0 cellspacing=0 cellpadding=0 align=\"absmiddle\" onclick=\"clickOnNode(" + this.treeMenu.name + ", "+ this.id + ");\">";
			}
			else{			
			this.leftSide = leftSideOther+"<img id=\"" + this.getNodeIconName() + "\" src="+ this.treeMenu.getIcon("L2") +" border=0 cellspacing=0 cellpadding=0 align=\"absmiddle\">";
			}				
			leftSideOther = leftSideOther + "<img src=" + this.treeMenu.getIcon("blank") +" border=0 cellspacing=0 cellpadding=0 align=\"absmiddle\">";			
			this.isLastNode =1;			
		}//第二个if over
		else{
			if( nc>0){				
			this.leftSide = leftSideOther+"<img id=\"" + this.getNodeIconName() + "\" src=" +  this.treeMenu.getIcon("P1") +" border=0 cellspacing=0 cellpadding=0 align=\"absmiddle\" onclick=\"clickOnNode(" + this.treeMenu.name + ", "+ this.id + ");\">";
			}			
			else{						
			this.leftSide = leftSideOther+ "<img id=\"" + this.getNodeIconName() + "\" src=" +  this.treeMenu.getIcon("L1") +" border=0 cellspacing=0 cellpadding=0 align=\"absmiddle\">";
			}
			leftSideOther = leftSideOther + "<img src=" + this.treeMenu.getIcon("L4") + " border=0 cellspacing=0 cellpadding=0 align=\"absmiddle\">";
			this.isLastNode = 0;
		}
		
	}//第一个if over
	else{
		this.draw();
	}

	if (nc > 0){
		level = level + 1;
		for (i=0 ; i < this.nChildren; i++){
			if (i == this.nChildren-1){
				this.children[i].initialize(level, 1, leftSideOther);
			}
			else{
				this.children[i].initialize(level, 0, leftSideOther);
			}
		}//for over
	}//if over
}//fun over



Folder.prototype.draw = function(){
	var str="";
	str = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
	str +="<tr>";
	
	//不再点td展开
	//str +="<td nowrap onclick=\"clickOnNode(" + this.treeMenu.name + ", "+ this.id + ");\">";
	str +="<td nowrap>";
	//
	str +=this.leftSide;
	
	//节点图标
	//点图标展开
	str +="<img name=\"" + this.getImageName() + "\" src=\"" + this.iconSrc + "\" border=0 cellspacing=0 cellpadding=0 align=\"absmiddle\" onclick=\"clickOnNode(" + this.treeMenu.name + ", "+ this.id + ");\">";	
	
	
	//节点文字
	if( this.url.length>0 && this.url != "#"){
    str += "&nbsp;";
		str +="<a href=\"" + this.url +"\" target=\"" + this.target + "\" title=\"" + this.desc + "\">";
		str +=this.text ;
		str +="</a>";
	}
	else{	
    str += "&nbsp;";
		str +="<font style=\"cursor:hand\" title=\"" + this.desc + "\" onclick=\"clickOnNode(" + this.treeMenu.name + ", "+ this.id + ");\">";
		str +=this.text ;
		str +="</font>";
	}
	
	str +="</td>";	
	str +="</tr>";	
	//如果该节点是父节点则生成父节点容器HTML
 	if( this.nChildren>0){
    str +="<tr><td>";	
		str += "<div id=\"" + this.getName() + "\" ></div>";
    str +="</td></tr>";	
	}
  str += "</table>";
	//把节点的HTML写入父节点中
	if( this.id==0){
		this.treeMenu.treeBody.innerHTML += str;
	}
	else{
		this.fatherDiv.innerHTML += str;
	}
	
	//节点图片对象
  this.nodeImg =  findObj(this.getNodeIconName());
}

Folder.prototype.setState = function(isOpen){
	if (isOpen != this.isOpen){	
	this.isOpen = isOpen;	
	this.repaint(this);
	}	
}



Folder.prototype.repaint = function(folder){
	var i=0;
	if (folder.isOpen){
		if (folder.nodeImg){
			if (folder.isLastNode){
				if(folder.nChildren>0){
					folder.nodeImg.src = this.treeMenu.getIcon("M2");
				}				    
			}
			else{
				if( folder.nChildren>0) {
					folder.nodeImg.src = this.treeMenu.getIcon("M1");
				}
			}
		 }			
			folder.display();
			if(folder.hasDrawed){
				for(i=0;i<folder.nChildren;i++){
					this.repaint(folder.children[i]);
				}
			}		
	}
	else{
		if (folder.nodeImg){
			if (folder.isLastNode){
				if( folder.nChildren>0){
					folder.nodeImg.src = this.treeMenu.getIcon("P2");
				}
			}
			else{
				if( folder.nChildren>0){
					folder.nodeImg.src = this.treeMenu.getIcon("P1");
				}
			}
		}	
		folder.hide();
	}
}

Folder.prototype.createEntryIndex = function(){
	this.id = this.treeMenu.nEntries;
	this.treeMenu.indexOfEntries[this.treeMenu.nEntries] = this;
	this.treeMenu.nEntries++;
}



Folder.prototype.drawChildren = function(){
	var i;
	var str = "";
	if( this.nChildren>0){
		var currentFolder = findObj(this.getName());		
		this.navObj = currentFolder;
	}	
	for(i=0;i<this.nChildren;i++){
		this.children[i].fatherDiv = currentFolder;		
		this.children[i].draw();
	}
	this.hasDrawed = true;
}


Folder.prototype.hide = function(){
	if( this.hasDrawed) {
		this.navObj.style.display = "none";
	}
}

Folder.prototype.display = function(){
	if( !this.hasDrawed){
		this.drawChildren();
	}
	else{
		this.navObj.style.display = "block";
	}
}