// author:Linus
// date:2009/01/22
// for IE and FF  object method

function myTree(){
	this.icon={						
		root		: "nwebimg/earth.gif",
		folder		: "nwebimg/nwebicon2.gif",
		folderOpen	: "nwebimg/nwebicon2.gif",
		node		: "nwebimg/r2.gif",
		empty		: "nwebimg/empty.gif",
		line		: "nwebimg/empty.gif",
		join		: "nwebimg/empty.gif",
		joinBottom	: "nwebimg/empty.gif",
		plus		: "nwebimg/empty.gif",
		plusBottom	: "nwebimg/empty.gif",
		minus		: "nwebimg/empty.gif",
		minusBottom	: "nwebimg/empty.gif",
		nlPlus		: "nwebimg/empty.gif",
		nlMinus		: "nwebimg/empty.gif"
	};
	this.aNode=new Array();	
	this.init_root=-1;
	this.ec=[];				//空img的陣列
	this.folder_array=[];	//設計全開及全關時用到的陣列
	this.iconOpen_array=[];
	this.open_array=[];
	this.cookie_array=[];
	this.flag_array=[];		//記錄folder是否展開	
	this.now = new Date();	
	this.expiry = new Date(this.now.getTime() + 1 * 1000);	//設定 cookies 存活時間
	this.myCookie = document.cookie;
	this.is_cookie=true;	//是否開啟cookies  true/false
	this.Node_Num=0;		
}

myTree.prototype.out=function(objID){
	var str='<div class="dtree">';
	//str+='<p><a href="javascript:d.openAll();">open all</a> | <a href="javascript:d.closeAll();">close all</a></p>';
	
	for(var i=0;i< this.aNode.length;i++){
		if(this.aNode[i][1]==this.init_root){
			str+='<img src='+this.icon.root+'>'+this.aNode[i][2];
			str+=this.addnode_func(this.aNode[i][0],0);								
		}	
	}	
	str+='</div>';
	objID.innerHTML=str;
	this.last_status();
}

myTree.prototype.add=function(id, pid, name, url, title, target, icon, iconOpen, open){
	this.aNode[this.aNode.length]=new Array(id, pid, name, url, title, target, icon, iconOpen, open, this.Node_Num++);
}

myTree.prototype.addnode_func=function(nodeid,line_img){	
	var tmpstr="";
	var j_c=0;
	for(var j=0;j<this.aNode.length;j++){
		if(this.aNode[j][1]==nodeid){
			j_c++;
		}			
	}
	var jj_c=0;
	for(var j=0;j<this.aNode.length;j++){				
		if(this.aNode[j][1]==nodeid){					
			jj_c++;
			var jj_folder=0;
			
			for(var k=0;k<this.aNode.length;k++){	//先判斷是否為folder(有兩個以上子id)
				if(this.aNode[k][1]==this.aNode[j][0]){
					jj_folder=1;	
				}										
			}
			
			tmpstr+='<div class="dtree">';			
			for(var p=0;p<line_img;p++){
				var is_E=0;
				for(var r=0;r<this.ec.length;r++){
					if(p==this.ec[r])
					is_E=1;	
				}								
				if(is_E)
					tmpstr+='<img src='+this.icon.empty+'>';				
				else
					tmpstr+='<img src='+this.icon.line+'>';	
			}		
						
			if(jj_folder==1){
				tmpstr+='<img id="i'+this.aNode[j][9]+'" src=';	
				if(j_c==jj_c){		//判斷是不是最後一個folder，是的話則改img圖案
					tmpstr+=this.icon.plusBottom;
				}else{
					tmpstr+=this.icon.plus;
				}	
				
				tmpstr+=' on click="d.chg('+this.aNode[j][9]+')" st yle="cursor:pointer"><img id="f'+this.aNode[j][9]+'" src='+this.icon.folder+'>';			
				tmpstr+='<a href="#" class="node" onclick="d.chg('+this.aNode[j][9]+')"';
				this.flag_array[this.aNode[j][9]]=0;
				if(this.aNode[j][4]!=null && this.aNode[j][4]!="")	//判斷有無title
					tmpstr+=' title='+this.aNode[j][4]+'>'+this.aNode[j][2]+'</a>';
				else
					tmpstr+='>'+this.aNode[j][2]+'</a>';

				tmpstr+='<div id="h'+this.aNode[j][9]+'" class="dtree" style="display:none">';
				this.folder_array[this.folder_array.length]=this.aNode[j][9];
				
				if(this.aNode[j][6]!=null && this.aNode[j][6]!="" && this.aNode[j][7]!=null && this.aNode[j][7]!="")
					this.iconOpen_array[this.iconOpen_array.length]=this.aNode[j][9];				
				
				if(this.aNode[j][8]!=null && this.aNode[j][8]!="")
					this.open_array[this.open_array.length]=this.aNode[j][9];
				
				if(j_c==jj_c){
					this.ec[this.ec.length]=line_img;
					tmpstr+=this.addnode_func(this.aNode[j][0],line_img+1);
					this.ec.pop();	//移除陣列最後一個元素			
				}else{
					tmpstr+=this.addnode_func(this.aNode[j][0],line_img+1);
				}
				tmpstr+='</div>';
				tmpstr+='</div>';
			}else{			
				if(j_c==jj_c){
					tmpstr+='<img src='+this.icon.joinBottom+'>';									
				}else{				
					tmpstr+='<img src='+this.icon.join+'>';			
				}
				tmpstr+='<img src='+this.icon.node+'>';
				if(this.aNode[j][3]!=null && this.aNode[j][3]!="")	//判斷有無url
					tmpstr+='<a href='+this.aNode[j][3]+' class="node"';
				else
					tmpstr+='<a href="#" class="node"';
				
				if(this.aNode[j][4]!=null && this.aNode[j][4]!="")	//判斷有無title
					tmpstr+=' title='+this.aNode[j][4];
				
				if(this.aNode[j][5]!=null && this.aNode[j][5]!="")	//判斷有無target
					tmpstr+=' target='+this.aNode[j][5];				
								
				tmpstr+='> '+this.aNode[j][2]+'</a>';
				tmpstr+='</div>';			
			}
		}	
	}
	return tmpstr;
}


myTree.prototype.chg=function(N){
	if(this.flag_array[N]==0){	//展開
		if(document.getElementById("i"+N).getAttribute('src').search(this.icon.plusBottom)>=0){
			document.getElementById("i"+N).setAttribute('src',this.icon.minusBottom);
		}else{
			document.getElementById("i"+N).setAttribute('src',this.icon.minus);
		}				
		
		if(this.aNode[N][6]!=null && this.aNode[N][6]!=""){	//設定要開的folder圖案
			document.getElementById("f"+N).setAttribute('src',this.aNode[N][6]);
		}else{
			document.getElementById("f"+N).setAttribute('src',this.icon.folderOpen);
		}			
		document.getElementById("h"+N).style.display='';
		this.flag_array[N]=1;
	}else{			
		if(document.getElementById("i"+N).getAttribute('src').search(this.icon.minusBottom)>=0){
			document.getElementById("i"+N).setAttribute('src',this.icon.plusBottom);
		}else{
			document.getElementById("i"+N).setAttribute('src',this.icon.plus);						
		}
		document.getElementById("f"+N).setAttribute('src',this.icon.folder);
		document.getElementById("h"+N).style.display='none';
		this.flag_array[N]=0;
	}
	this.updatecookies();
}

myTree.prototype.updatecookies=function(){
	var tmp_array=new Array();                                                                                                	
	for(var j=0;j<this.folder_array.length;j++){
		N=this.folder_array[j];	
		if(this.flag_array[N]==1){
			tmp_array[tmp_array.length]=N;	
		}
	}
	this.cookie_array=tmp_array;
	this.setcookie(this.cookie_array);
}

myTree.prototype.setcookie=function(v_array){
	document.cookie = "cookieopenfolder=" + v_array + "; expires=" + this.expiry.toGMTString();
}

myTree.prototype.getCookie=function(name) {
	name += "=";    
	var i = 0;            
	while (i < this.myCookie.length) {
		var offset = i + name.length;   
		if (this.myCookie.substring(i, offset) == name) {   		
			var endstr = this.myCookie.indexOf(";", offset);
			if (endstr == -1) 
				endstr = this.myCookie.length;			
			return unescape(this.myCookie.substring(offset, endstr));
		}
		i = this.myCookie.indexOf(" ", i) + 1;   
		if (i == 0) 
			break;  
	}
	return null; 
}

myTree.prototype.last_status=function(){
	if(this.is_cookie)
		var h_v=this.getCookie("cookieopenfolder");
	else
		var h_v=null;
	
	if(h_v==null){
		for(var a=0;a<this.iconOpen_array.length;a++){	//若icon有設定且iconOpen預設打開則網頁一載入即打開此icon folder
			this.chg(this.iconOpen_array[a]);
		}
		for(var a=0;a<this.open_array.length;a++){	//若open有設定網頁一載入即打開folder
			this.chg(this.open_array[a]);
		}
	}else{
		var cookie_open=h_v.split(",");
		for(var a=0;a<cookie_open.length;a++){
			this.chg(cookie_open[a]);
		}
	}
}

myTree.prototype.openAll=function(){
	var N;
	for(var j=0;j<this.folder_array.length;j++){
		N=this.folder_array[j];	
		if(document.getElementById("i"+N).getAttribute('src').search(this.icon.plusBottom)>=0 || document.getElementById("i"+N).getAttribute('src').search(this.icon.minusBottom)>=0){
			document.getElementById("i"+N).setAttribute('src',this.icon.minusBottom);
		}else{
			document.getElementById("i"+N).setAttribute('src',this.icon.minus);		
		}
		if(this.aNode[N][6]!=null && this.aNode[N][6]!=""){	//設定要開的folder圖案
			document.getElementById("f"+N).setAttribute('src',this.aNode[N][6]);
		}else{
			document.getElementById("f"+N).setAttribute('src',this.icon.folderOpen);	
		}
		document.getElementById("h"+N).style.display='';
		this.flag_array[N]=1;
	}
}

myTree.prototype.closeAll=function(){
	var N;
	for(var j=0;j<this.folder_array.length;j++){
		N=this.folder_array[j];	
		if(document.getElementById("i"+N).getAttribute('src').search(this.icon.minusBottom)>=0 || document.getElementById("i"+N).getAttribute('src').search(this.icon.plusBottom)>=0){
			document.getElementById("i"+N).setAttribute('src',this.icon.plusBottom);
		}else{
			document.getElementById("i"+N).setAttribute('src',this.icon.plus);							
		}
		document.getElementById("f"+N).setAttribute('src',this.icon.folder);
		document.getElementById("h"+N).style.display='none';
		this.flag_array[N]=0;
	}
}
