// Example:
// simplePreload( '01.gif', '02.gif' ); 
function simplePreload()
{ 
  var args = simplePreload.arguments;
  document.imageArray = new Array(args.length);
  for(var i=0; i<args.length; i++)
  {
    document.imageArray[i] = new Image;
    document.imageArray[i].src = args[i];
  }
}

simplePreload('images/folder_1_closed.png','images/folder_1_closed_inactive.png','images/folder_1_opened.png','images/folder_1_opened_inactive.png','images/folder_2_closed.png','images/folder_2_closed_inactive.png','images/folder_2_opened.png','images/folder_2_opened_inactive.png','images/doc_1_closed.png','images/doc_1_closed_inactive.png','images/doc_1_opened.png','images/doc_1_opened_inactive.png','images/doc_2_closed.png','images/doc_2_closed_inactive.png','images/doc_2_opened.png','images/doc_2_opened_inactive.png');

function node (title,url) {

	this.title = title;
	this.url = url;

 this.treeName = '';	
 this.id = 0;
	this.id_parent = 0;
	this.depth = 0;
	
	this.opened = false;
 this.visible = false;
	this.selected = false;

	this.nodes = new Array();
	
	this.addNode = function (title,url) {
	 var i = this.nodes.length;
 	this.nodes[i] = new node(title,url);
		this.nodes[i].treeName = this.treeName;
		this.nodes[i].id = this.id+'_'+this.nodes.length;
		this.nodes[i].id_parent = this.id;
		this.nodes[i].depth = this.depth+1;
	}
	
	this.getNode = function (id) {
		if (this.id==id) { return this; }
	 for (var i=0; i<this.nodes.length; i++) {
   var subNode = this.nodes[i].getNode(id);
			if (subNode) { return subNode; }				
		}
	}
		
	this.updateDisplay = function () {
		var nodeDiv = document.getElementById(this.treeName+'_'+this.id);
		if (this.visible) {
			nodeDiv.style.display = 'block';
   if (this.nodes.length) { var src = 'images/folder'; } else { var src = 'images/doc'; }
			if (this.depth=='1') { src += '_1'; } else { src += '_2'; }
			if (this.opened) { src += '_opened.png'; } else { src += '_closed.png'; }
			var nodeImage = document.getElementById(this.treeName+'_'+this.id+'_image');
			nodeImage.src = src;
			var nodeTitle = document.getElementById(this.treeName+'_'+this.id+'_title');
			if (this.selected || this.opened) { nodeTitle.style.fontWeight = 'bold'; } else { nodeTitle.style.fontWeight = 'normal'; }
		} else {
			nodeDiv.style.display = 'none';
		}
	 for (var i=0; i<this.nodes.length; i++) {
   this.nodes[i].updateDisplay();				
		}
	}
	
	this.setSelectedNode = function (id) {
	 if (this.id==id) { this.selected = true; } else  { this.selected = false; }
		for (var i=0; i<this.nodes.length; i++) {
			this.nodes[i].setSelectedNode(id);
		}	
	}
	
	this.setActive = function () {
		if (this.nodes.length) { var src = 'images/folder'; } else { var src = 'images/doc'; }
		if (this.depth=='1') { src += '_1'; } else { src += '_2'; }
		if (this.opened) { src += '_opened.png'; } else { src += '_closed.png'; }
		var nodeImage = document.getElementById(this.treeName+'_'+this.id+'_image');
		nodeImage.src = src;
		var nodeTitle = document.getElementById(this.treeName+'_'+this.id+'_title');	 
		if (this.depth=='1') { nodeTitle.style.color = 'rgb(79,137,192)'; }
		else { nodeTitle.style.color = 'rgb(0,0,0)'; }
		for (var i=0; i<this.nodes.length; i++) {
				this.nodes[i].setActive();
			}		
	}
	
	this.setInactive = function () {
		if (this.nodes.length) { var src = 'images/folder'; } else { var src = 'images/doc'; }
		if (this.depth=='1') { src += '_1'; } else { src += '_2'; }
		if (this.opened) { src += '_opened_inactive.png'; } else { src += '_closed_inactive.png'; }
		var nodeImage = document.getElementById(this.treeName+'_'+this.id+'_image');
		nodeImage.src = src;
		var nodeTitle = document.getElementById(this.treeName+'_'+this.id+'_title');	 
		if (this.depth=='1') { nodeTitle.style.color = 'rgb(187,210,233)'; }
		else { nodeTitle.style.color = 'rgb(88,89,91)'; }
		for (var i=0; i<this.nodes.length; i++) {
				this.nodes[i].setInactive();
			}		
	}
	
	this.addNodesFromArray = function (treeArray) {
		for (var j = 0; j<treeArray.length; j++) {
			this.addNode(treeArray[j][0],treeArray[j][1]);	
			if (treeArray[j][2]!=null) { this.nodes[this.nodes.length-1].addNodesFromArray(treeArray[j][2]); }
		}		 
	}
	
	this.nodeState = function () {
	 alert(this.title+' '+this.id);
		for (var i=0; i<this.nodes.length; i++) {
   this.nodes[i].nodeState();				
		}
	}
		
}

function myTree (name) {

	this.title = 'root';
	this.url = '';
	
 this.id = 0;
	this.id_parent = 0;
	this.depth = 0;

 this.treeName = name;
	this.active = false;
	
	this.nodes = new Array();
	
	this.addNode = function (title,url) {
		var i = this.nodes.length;
 	this.nodes[i] = new node(title,url);
		this.nodes[i].treeName = this.treeName;
		this.nodes[i].id = this.nodes.length;
		this.nodes[i].id_parent = 0;
		this.nodes[i].depth = 1;
		this.nodes[i].opened = false;
		this.nodes[i].visible = true;	
	}
	
	this.getNode = function (id) {
	 if (id==0) { return this; }
	 for (var i=0; i<this.nodes.length; i++) {
   var subNode = this.nodes[i].getNode(id);
			if (subNode) { return subNode; }				
		}
	}
	
	this.getParentNode = function (node) {
		if (node.depth) { return this.getNode(node.id_parent); }
	}
	
 this.closeNode = function (node) {
		if (node) { 
		 node.opened = false;
		 for (var i=0; i<node.nodes.length; i++) {
			 node.nodes[i].visible = false;
			 this.closeNode(node.nodes[i]);
			}
		}
	}
	
	this.openNode = function (node) {
	 if (node) {
			
			node.opened = true;
			node.visible = true;
		 
			for (var i=0; i<node.nodes.length; i++) {
				node.nodes[i].visible = true;
				this.closeNode(node.nodes[i]);
			}
			
			var parentNode = this.getNode(node.id_parent);
			parentNode.opened = true;
			parentNode.visible = true;
			if (node.nodes.length==0) {
				for (var i=0; i<parentNode.nodes.length; i++) {
				 if (parentNode.nodes[i].id!=node.id) {
					 parentNode.nodes[i].visible = true;
					 this.closeNode(parentNode.nodes[i]);
					}
				}
			} else {
				for (var i=0; i<parentNode.nodes.length; i++) {
				 if (parentNode.nodes[i].id!=node.id) {
					 if (node.depth>1) { parentNode.nodes[i].visible = false; }
					 this.closeNode(parentNode.nodes[i]);
					}
				}
			}			
			
			while (parentNode.id_parent!=0) {
			 node = parentNode;
				parentNode = this.getNode(node.id_parent);
				parentNode.opened = true;
				parentNode.visible = true;
				for (var i=0; i<parentNode.nodes.length; i++) {
					if (parentNode.nodes[i].id!=node.id) {
						if (node.depth>1) { parentNode.nodes[i].visible = false; }
						this.closeNode(parentNode.nodes[i]);
					}
				}
			}					
			
		}

	}
	
	this.setSelectedNode = function (id) {
		for (var i=0; i<this.nodes.length; i++) {
			this.nodes[i].setSelectedNode(id);
		}	
	}
	
	this.select = function (id) {
	 var node = this.getNode(id);
		if (node.depth) {
		 if (node.opened) { 
			 this.closeNode(node);
				var parentNode = this.getNode(node.id_parent);
				this.openNode(parentNode);
				if (node.nodes.length) { writeCookie(this.treeName+'State',node.id_parent); } 
			} else { this.openNode(node);	writeCookie(this.treeName+'State',id); }
			if (node.url && !node.selected) { document.location.href = node.url; }
			if (this.active && !node.nodes.length) { 
    this.setSelectedNode(node.id);
				if (this.treeName=='tree') {
				 writeCookie('tree2State',0);
				} else {
				 writeCookie('treeState',0);
				}
				writeCookie('pathState',this.treeName);
				writeCookie('pathState2',node.id);
			}
   this.updateDisplay();
		}
	}
	
		this.firstSelect = function (id) {
	 var node = this.getNode(id);
		if (node && node.depth) {
		 if (node.opened) { 
			 this.closeNode(node);
				var parentNode = this.getNode(node.id_parent);
				this.openNode(parentNode);
				if (node.nodes.length) { writeCookie(this.treeName+'State',node.id_parent); } 
			} else { this.openNode(node);	writeCookie(this.treeName+'State',id); }
			if (this.active && !node.nodes.length) { 
    this.setSelectedNode(node.id);
			}
   this.updateDisplay();
		}
	}
	
	this.updateDisplay = function () {
	 for (var i=0; i<this.nodes.length; i++) {
   this.nodes[i].updateDisplay();				
		}
	}

 this.setActive = function () {
	 if (!this.active) {
			for (var i=0; i<this.nodes.length; i++) {
				this.nodes[i].setActive();				
			}	
  }
	}

 this.setInactive = function () {
	 if (!this.active) {
			for (var i=0; i<this.nodes.length; i++) {
				this.nodes[i].setInactive();				
			}
		}
	}

	this.init = function (ix) {
	 this.initNode(this);
		this.updateDisplay();
		if (this.active) { this.setActive(); } else { this.setInactive(); }
  var id = readCookie(this.treeName+'State');
		if (id && ix!='0') { this.firstSelect(id); } else { this.firstSelect(0); }
	}
	
	this.initNode = function (node) {
	 if (node) {
		 if (this.nodes.length) { var src = 'images/folder'; } else { var src = 'images/doc'; }
			if (this.depth=='1') { src += '_1'; } else { src += '_2'; }
			if (this.opened) { src += '_opened.png'; } else { src += '_closed.png'; }
			if (node.depth) {
				document.write ('<div id="'+this.treeName+'_'+node.id+'" style="display:block;">');
				if (node.depth==1) {
					document.write ('<table cellspacing=0 cellpadding=0 border=0><tr><td><div style="cursor:hand; width:15px;background:#F7F3E7; text-align:left;" onClick="'+this.treeName+'.select(\''+node.id+'\');"><img id="'+this.treeName+'_'+node.id+'_image" src="'+src+'"></div></td><td><div id="'+this.treeName+'_'+node.id+'_title" style="cursor:hand; width:150px;background:#F7F3E7;" onClick="'+this.treeName+'.select(\''+node.id+'\');" class="menu">'+node.title+'</div></td></tr></table>');
    } else {
					document.write ('<table cellspacing=0 cellpadding=0 border=0><tr><td><div style="cursor:hand; width:15px;background:#F7F3E7; text-align:left;" onClick="'+this.treeName+'.select(\''+node.id+'\');"><img id="'+this.treeName+'_'+node.id+'_image" src="'+src+'"></div></td><td><div id="'+this.treeName+'_'+node.id+'_title" style="cursor:hand; width:150px;background:#F7F3E7;" onClick="'+this.treeName+'.select(\''+node.id+'\');" class="submenu">'+node.title+'</div></td></tr></table>');				
				}
				document.write ('<div id="'+this.treeName+'_'+node.id+'_nodes" style="display:block;">');
			} else {
				document.write ('<div id="'+this.treeName+'" style="display:block;" onMouseOver="'+this.treeName+'.setActive();" onMouseOut="'+this.treeName+'.setInactive(); ">');
			}
			for (var i=0; i<node.nodes.length; i++) {
				this.initNode(node.nodes[i]);				
			}
			if (node.depth) {
				document.write ('</div></div>');
			} else {
				document.write ('</div>');
			}
		}
	}

 this.addNodesFromArray = function (treeArray) {
		for (var i = 0; i<treeArray.length; i++) {
		 this.addNode(treeArray[i][0],treeArray[i][1]);
			if (treeArray[i][2]!=null) { this.nodes[this.nodes.length-1].addNodesFromArray(treeArray[i][2]); }
		}
		this.active = true;		 
	}

	this.path = function (id) {
	 var node = this.getNode(id);
	 var str = '<span class="path" style="cursor:hand;" onClick="'+this.treeName+'.select(\''+node.id+'\')">'+node.title+'</span>'
	 if (node.id_parent==0) {
		 return str;
		} else {
		 var parent = this.getParentNode(node);
		 return this.path(parent.id)+'&nbsp;|&nbsp;'+str;
		}
	}
	
}

function loadPath () {
	var treeName = readCookie('pathState');
 var node_id = readCookie('pathState2');
	if (treeName!='undefined' && node_id!='undefined') {
		var pathDiv = document.getElementById('path');
		var mytree = eval(treeName);
		if (mytree) pathDiv.innerHTML = mytree.path(node_id);	
	}
}
