/**************************************************************************
	Copyright (c) 2001 Geir Landrö (drop@destroydrop.com)
	JavaScript Tree - www.destroydrop.com/javascript/tree/
	Version 2.0	

	This script can be used freely as long as all copyright messages are
	intact.
**************************************************************************/

// Tree class
function Tree(nodes, startNode, openNode) {
	this.nodes			= nodes;
	this.icons			= new Array(6);
	this.startNode		= startNode || 0;
	this.openNode		= openNode || 0;
	this.target			= "_top";
	this.mTarget		= "";
	this.baseName		= "Website";
	this.openNodes		= new Array();
	this.folderlinks	= true;
	this.clickHighlight	= false;
	this.useCookies		= true;
	this.cookieExpires	= 10;
	this.cookieOpen		= "opennodes";
	this.cookieSelected	= "selnode";
	this.instanceName	= "";
	
	// Functions
	this.draw			= TreeDraw;
	this.getArrayPos	= TreeGetArrayPos;
	this.setOpenNodes	= TreeSetOpenNodes;
	this.isNodeOpen		= TreeIsNodeOpen;
	this.hasChildNode	= TreeHasChildNode;
	this.lastSibling	= TreeLastSibling;
	this.addNode		= TreeAddNode;
	this.setCookie		= TreeSetCookie;
	this.getCookie		= TreeGetCookie;
	this.cookieValueStr	= TreeCookieValueStr;
	this.isCookieOpen	= TreeIsCookieOpen;
	this.cookieSel		= TreeCookieSelected;
	this.preloadIcons	= TreePreloadIcons;
	this.oc				= TreeOpenClose;
	this.nh				= TreeHightlightNode;
	
	// Preaload icons
	this.preloadIcons();
}
// Draw the tree
function TreeDraw() {
	if (this.target != "") this.mTarget = " target=\"" + this.target + "\"";
	if (this.nodes.length > 0) {

		if (this.openNode != 0) this.setOpenNodes(this.openNode);
		else if (this.cookieSel()) this.setOpenNodes(this.cookieSel());
		
		if (this.startNode !=0) {
			var nodeValues = this.nodes[this.getArrayPos(this.startNode)].split("|");
			document.writeln("<a href=\"" + nodeValues[3] + "\"" + this.mTarget + " onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\"img/base.gif\" align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a><br />");
		} else document.writeln("<img src=\"img/base.gif\" align=\"absbottom\" alt=\"\" />" + this.baseName + "<br />");

		var recursedNodes = new Array();
		this.addNode(this.startNode, recursedNodes);
	}
}
// Returns the position of a node in the array
function TreeGetArrayPos(node) {
	for (i=0; i<this.nodes.length; i++) {
		var nodeValues = this.nodes[i].split("|");
		if (nodeValues[0]==node) return i;
	}
}
// Puts in array nodes that will be open
function TreeSetOpenNodes(openNode) {
	for (i=0; i<this.nodes.length; i++) {
		var nodeValues = this.nodes[i].split("|");
		if (nodeValues[0]==openNode) {
			this.openNodes.push(nodeValues[0]);
			this.setOpenNodes(nodeValues[1]);
		}
	} 
}
// Checks if a node is open
function TreeIsNodeOpen(node) {
	for (i=0; i<this.openNodes.length; i++)
		if (this.openNodes[i]==node) return true;
	return false;
}
// Checks if a node has any children
function TreeHasChildNode(parentNode) {
	for (i=0; i< this.nodes.length; i++) {
		var nodeValues = this.nodes[i].split("|");
		if (nodeValues[1] == parentNode) return true;
	}
	return false;
}
// Checks if a node is the last sibling
function TreeLastSibling (node, parentNode) {
	var lastChild = 0;
	for (i=0; i< this.nodes.length; i++) {
		var nodeValues = this.nodes[i].split("|");
		if (nodeValues[1] == parentNode)
			lastChild = nodeValues[0];
	}
	if (lastChild==node) return true;
	return false;
}
// Adds a new node in the tree
function TreeAddNode(parentNode, recursedNodes) {
	for (var i = 0; i < this.nodes.length; i++) {

		var nodeValues = this.nodes[i].split("|");
		if (nodeValues[1] == parentNode) {
			
			var ls	= this.lastSibling(nodeValues[0], nodeValues[1]);
			var hcn	= this.hasChildNode(nodeValues[0]);
			var ino = this.isNodeOpen(nodeValues[0]);
			if (!ino && this.useCookies) {
				ino = this.isCookieOpen(nodeValues[0]);
			}
			var trg="";
			if (nodeValues.length>4) {
				if (nodeValues[4].indexOf(".")==-1 ) trg = " target=\"" + nodeValues[4] + "\"";
				else trg = this.mTarget;
			} else trg = this.mTarget;
			
			// Write out line & empty icons
			for (g=0; g<recursedNodes.length; g++) {
				if (recursedNodes[g] == 1) document.write("<img src=\"img/line.gif\" align=\"absbottom\" alt=\"\" />");
				else  document.write("<img src=\"img/empty.gif\" align=\"absbottom\" alt=\"\" />");
			}

			// Put in array line & empty icons
			if (ls) recursedNodes.push(0);
			else recursedNodes.push(1);

			// Write out join icons
			if (hcn) {
				if (ls) {
					document.write("<a href=\"javascript: " + this.instanceName + ".oc(" + nodeValues[0] + ", 1);\"><img id=\"join" + this.instanceName + nodeValues[0] + "\" src=\"img/");
					 	if (ino) document.write("minus");
						else document.write("plus");
					document.write("bottom.gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
				} else {
					document.write("<a href=\"javascript: " + this.instanceName + ".oc(" + nodeValues[0] + ", 0);\"><img id=\"join" + this.instanceName + nodeValues[0] + "\" src=\"img/");
						if (ino) document.write("minus");
						else document.write("plus");
					document.write(".gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
				}
			} else {
				if (ls) document.write("<img src=\"img/join.gif\" align=\"absbottom\" alt=\"\" />");
				else document.write("<img src=\"img/joinbottom.gif\" align=\"absbottom\" alt=\"\" />");
			}

			// Start link
			if (!this.folderlinks && hcn) {
				nodeValues[3] = "javascript: " + this.instanceName + ".oc(" + nodeValues[0] + ", 1);\"";
				trg = "";
			}
			document.write("<a href=\"" + nodeValues[3] + "\"" + trg);
			
			// If hightlight link is on
			if (this.clickHighlight) {
				if (hcn) {
					if (this.folderlinks)
						document.write(" onclick=\"javascript: " + this.instanceName + ".nh(" + nodeValues[0] + ");\"");
				} else {
						document.write(" onclick=\"javascript: " + this.instanceName + ".nh(" + nodeValues[0] + ");\"");
				}
			}
			
			document.write(" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
			
			// Write out folder & page icons
			if (hcn) {
				document.write("<img id=\"icon" + this.instanceName + nodeValues[0] + "\" src=\"img/folder");
					if (ino) document.write("open");
				document.write(".gif\" align=\"absbottom\" alt=\"Folder\" />");
			} else {
				if (nodeValues.length>4) {
					if (nodeValues.length==6) document.write("<img id=\"icon" + this.instanceName + nodeValues[0] + "\" src=\"img/" + nodeValues[5] + "\" align=\"absbottom\" alt=\"Page\" />");
					else {
						if (nodeValues[4].indexOf(".")!=-1) document.write("<img id=\"icon" + this.instanceName + nodeValues[0] + "\" src=\"img/" + nodeValues[4] + "\" align=\"absbottom\" alt=\"Page\" />");
						else document.write("<img id=\"icon" + this.instanceName + nodeValues[0] + "\" src=\"img/page.gif\" align=\"absbottom\" alt=\"Page\" />");
					}
				}
				else document.write("<img id=\"icon" + this.instanceName + nodeValues[0] + "\" src=\"img/page.gif\" align=\"absbottom\" alt=\"Page\" />");
			}

			// Write out span
			if (this.clickHighlight) {
				document.write("<span");
				if (this.cookieSel()==nodeValues[0]) document.write(" class=\"nodeHighlight\"");
				document.write(" id=\"s" + nodeValues[0] + "\">");
			}
			// Write out node name
			document.write(nodeValues[2]);

			// End span
			if (this.clickHighlight) document.write("</span>");
			
			// End link
			document.writeln("</a><br />");
			
			// If node has children write out divs and go deeper
			if (hcn) {
				document.write("<div id=\"div" + this.instanceName + nodeValues[0] + "\"");
					if (!ino) document.write(" style=\"display: none;\"");
				document.writeln(">");
				this.addNode(nodeValues[0], recursedNodes);
				document.writeln("</div>");
			}
			
			// remove last line or empty icon 
			recursedNodes.pop();
		}
	}
}
// Sets open nodes in a cookie
function TreeSetCookie (cookieValue, cookieName) {
	
	if (cookieName == this.cookieOpen+this.instanceName) {
		if (this.getCookie(cookieName) != null) // Checks if a cookie is allready set
			cookieValue = this.cookieValueStr(cookieValue);
	}

	var expires = new Date();
	expires.setTime(expires.getTime() + ( this.cookieExpires*60*1000 ) );
	document.cookie = 
 		escape(cookieName) + '=' + escape(cookieValue) 
		+ (expires ? '; expires=' + expires.toGMTString() : '');
}
// gets open nodes from a cookie
function TreeGetCookie (cookieName) {
	var cookieValue = null;
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	if (posName != -1) {
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1)
			cookieValue = unescape(document.cookie.substring(posValue, endPos));
		else
			cookieValue = unescape(document.cookie.substring(posValue));
	}
	return cookieValue;
}
// Creates the cookie value string
function TreeCookieValueStr(cookieValue) {
	var newNodes = new Array();
	var oldCookieValue = this.getCookie(this.cookieOpen+this.instanceName);
	
	cookieNodes = oldCookieValue.split(".");
	
	if (this.isCookieOpen(cookieValue)) {
		for(i=0; i<cookieNodes.length; i++) {
			if (cookieNodes[i] != cookieValue  && cookieNodes[i] != null)
				newNodes.push(cookieNodes[i]);
		}
	} else {
		newNodes = cookieNodes;
		newNodes.push(cookieValue);
	}
	var strCookieValue = "";
	for (i=0;i<newNodes.length;i++) {
		if (i != 0) strCookieValue += "." + newNodes[i];
		else strCookieValue += + newNodes[i];
	}
	cookieValue = strCookieValue;

	return cookieValue;
}
// Checks if a node is in a cookie
function TreeIsCookieOpen(node) {
	var cookieValue = this.getCookie(this.cookieOpen+this.instanceName);
	if (cookieValue != null) {
		cookieNodes = cookieValue.split(".");
		for (i=0; i<cookieNodes.length; i++)
			if (node == cookieNodes[i]) return true;
	}
	return false;
}
// Checks if a node is in a cookie
function TreeCookieSelected() {
	var cookieValue = this.getCookie(this.cookieSelected+this.instanceName);
	if (cookieValue != null) return cookieValue;
	return false;
}
// Loads all icons that are used in the tree
function TreePreloadIcons() {
	this.icons[0] = new Image();
	this.icons[0].src = "img/plus.gif";
	this.icons[1] = new Image();
	this.icons[1].src = "img/plusbottom.gif";
	this.icons[2] = new Image();
	this.icons[2].src = "img/minus.gif";
	this.icons[3] = new Image();
	this.icons[3].src = "img/minusbottom.gif";
	this.icons[4] = new Image();
	this.icons[4].src = "img/folder.gif";
	this.icons[5] = new Image();
	this.icons[5].src = "img/folderopen.gif";
}
// Opens or closes a node
function TreeOpenClose(node, bottom) {
	var theDiv = document.getElementById("div" + this.instanceName + node);
	var theJoin	= document.getElementById("join" + this.instanceName + node);
	var theIcon = document.getElementById("icon" + this.instanceName + node);
	
	if (theDiv.style.display == 'none') {
		if (bottom==1) theJoin.src = this.icons[3].src;
		else theJoin.src = this.icons[2].src;
		theIcon.src = this.icons[5].src;
		theDiv.style.display = '';
	} else {
		if (bottom==1) theJoin.src = this.icons[1].src;
		else theJoin.src = this.icons[0].src;
		theIcon.src = this.icons[4].src;
		theDiv.style.display = 'none';
	}
	if (this.useCookies) this.setCookie (node, this.cookieOpen+this.instanceName);
}
function TreeHightlightNode(node) {
	var theSpan = document.getElementById("s" + node);

	for(i=0; i< this.nodes.length; i++) {
		var nodeValues = this.nodes[i].split("|");
		d = document.getElementById("s" + nodeValues[0]);
		if (d != null) {
			if (d.className=="nodeHighlight") d.className = "";
		}
	}
	theSpan.className = 'nodeHighlight';
	this.setCookie (node, this.cookieSelected+this.instanceName);
}
// Push and pop not implemented in IE
if(!Array.prototype.push) {
	function array_push() {
		for(var i=0;i<arguments.length;i++)
			this[this.length]=arguments[i];
		return this.length;
	}
	Array.prototype.push = array_push;
}
if(!Array.prototype.pop) {
	function array_pop(){
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		return lastElement;
	}
	Array.prototype.pop = array_pop;
}