// Arrays for nodes and icons
var nodes		= new Array();;
var openNodes	= new Array();
var icons		= new Array(6);


// Loads all icons that are used in the tree
function preloadIcons() {
	icons[0] = new Image();
	icons[0].src = imagePath + "/plus1.gif";
	icons[1] = new Image();
	icons[1].src = imagePath + "/plusbottom.gif";
	icons[2] = new Image();
	icons[2].src = imagePath + "/minus1.gif";
	icons[3] = new Image();
	icons[3].src = imagePath + "/minus1bottom.gif";
	icons[4] = new Image();
	icons[4].src = imagePath + "/folder.gif";
	icons[5] = new Image();
	icons[5].src = imagePath + "/folderopen.gif";
}
// Create the tree
function createTree(arrName, startNode, openNode) {
	nodes = arrName;
	if (nodes.length > 0) 
		{
			preloadIcons();
			if (startNode == null) startNode = 0;
			if (openNode != 0 || openNode != null) setOpenNodes(openNode);
	
		 if (NS4)
		 	{
	 			// document.write("<table border=0><tr><td><<img src='" + imagePath + "/base.gif' align='absbottom' border='0' alt=''></td><td>Categories</td></tr></table>");
	 			document.write("<table border=0><tr><td><br></td></tr></table>");
	 		}
   else document.write("<br>");	 		
	 	//else document.write("<img src='" + imagePath + "/base.gif' align='absbottom' border='0' alt=''>Categories<br>");		
	 
	 	var recursedNodes = new Array();
			addNode(startNode, recursedNodes);
		}
}
// Returns the position of a node in the array
function getArrayId(node) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==node) return i;
	}
}
// Puts in array nodes that will be open
function setOpenNodes(openNode) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==openNode) {
			openNodes.push(nodeValues[0]);
			setOpenNodes(nodeValues[1]);
		}
	} 
}
// Checks if a node is open
function isNodeOpen(node) {
	for (i=0; i<openNodes.length; i++)
		if (openNodes[i]==node) return true;
	return false;
}
// Checks if a node has any children
function hasChildNode(parentNode) {
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) return true;
	}
	return false;
}
// Checks if a node is the last sibling
function lastSibling (node, parentNode) {
	var lastChild = 0;
	for (i=0; i< nodes.length; i++) {
		var nodeValues = 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 addNode(parentNode, recursedNodes) {
		for (var i = 0; i < nodes.length; i++) {

		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) {
			
			var ls	= lastSibling(nodeValues[0], nodeValues[1]);
			var hcn	= hasChildNode(nodeValues[0]);
			var ino = isNodeOpen(nodeValues[0]);
			
						

			// Write out line & empty icons
			for (g=0; g<recursedNodes.length; g++) {
				if (NS4)
					{
						// do nothing
					}
				else
					{
						if (recursedNodes[g] == 1) document.write("<img src=\"" + imagePath + "/line.gif\" align=\"absbottom\" border=\"0\" width=\"19\" height=\"16\" alt=\"\" />");
						else  document.write("<img src=\"" + imagePath + "/empty.gif\" border=\"0\" align=\"absbottom\" width=\"19\" height=\"16\" alt=\"\" />");
					}
			}

			// put in array line & empty icons
			if (ls) recursedNodes.push(0);
			else recursedNodes.push(1);

			// Write out join icons
			if (hcn) 
				{
					if (ls) 
						{
							if (NS4)
								{
					  		// do nothing for ns4, just make a tree
								}
							else
								{
					  		document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 1);\"><img id=\"join" + nodeValues[0] + "\" src=\"" + imagePath + "/");
					 			if (ino) document.write("minus1");
									else document.write("plus1");
									document.write("bottom.gif\" align=\"absbottom\" width=\"19\" height=\"16\" alt=\"Open/Close node\" /></a>");
						  }
						} 
					else 
						{
							if (NS4)
								{
							  // do nothing for ns4, just make a tree
								}
							else
								{
									document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 0);\"><img id=\"join" + nodeValues[0] + "\" src=\"" + imagePath + "/");
									if (ino) document.write("minus1");
									else document.write("plus1");
									document.write(".gif\" align=\"absbottom\" border=\"0\" width=\"19\" height=\"16\" alt=\"Open/Close node\" /></a>");
						  }
						}
					} 
				else {
				if (NS4)
					{
							// do nothing
					}
				else
					{	
						if (ls) document.write("<img src=\"" + imagePath + "/join.gif\" align=\"absbottom\" border=\"0\" width=\"19\" height=\"16\" alt=\"\" />");
						else document.write("<img src=\"" + imagePath + "/joinbottom.gif\" align=\"absbottom\" border=\"0\" width=\"19\" height=\"16\" alt=\"\" />");
			  }
			}

			// Start link
			if (NS4)
				{
					// do nothing
				}
			else
				{	
					if (hcn)
						{
							document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 0);\">");
						}
					else
						{		
				  	document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
				  }
				}
				
			// Write out folder & page icons
			if (hcn) 
				{
					if (NS4)
						{
							document.write("<table border=0><tr><td><img id=\"icon" + nodeValues[0] + "\" src=\"" + imagePath + "/folder")
							document.write("open.gif\" align=\"absbottom\" width=\"19\" height=\"16\" alt=\"Folder\" border=\"0\" /></td>");						
						}
					else
						{	
							document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"" + imagePath + "/folder")
							if (ino) document.write("open");
							document.write(".gif\" align=\"absbottom\" width=\"19\" height=\"16\" alt=\"Folder\" border=\"0\" />");
				  }
				} 
			
			else
				{
					if (NS4)
						{
							document.write("<table border=0><tr><td><img id=\"icon" + nodeValues[0] + "\" src=\"" + imagePath + "/page.gif\" align=\"absbottom\" border=\"0\" width=\"19\" height=\"16\" alt=\"Page\" /></td>");
					
						}
					else
						{	
							document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"" + imagePath + "/page.gif\" align=\"absbottom\" border=\"0\" width=\"19\" height=\"16\" alt=\"Page\" />");
						}
				}
				
			// Write out node name
			if (NS4)
				{
				document.write("<td>");
				document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
				document.write(nodeValues[2]);
				document.write("</a></td></tr></table>");
				}
			else
				{
					document.write(nodeValues[2]);
				}

			// End link
			if (NS4)
				{
			 //	document.write("");
				}
			else
				{	
					document.write("</a><br />");
				}
			// If node has children write out divs and go deeper
			if (hcn) {
				if (NS4)
					{
					document.write("<table border=0><tr><td>\&#160;</td><td>");
					}
				else
					{	
						document.write("<div id=\"div" + nodeValues[0] + "\"");
						if (!ino) document.write(" style=\"display: none;\"");
						document.write("\>");
					}
						
				addNode(nodeValues[0], recursedNodes);
				
				if (NS4)
					{
						document.write("</td></tr></table>");
					}
				else
					{
						document.write("\</div\>");
			  }
			}
			
			// remove last line or empty icon 
			recursedNodes.pop();
		}
	}
}
// Opens or closes a node
function oc(node, bottom) {
var theDiv = document.getElementById("div" + node);
var theJoin	= document.getElementById("join" + node);
var theIcon = document.getElementById("icon" + node);

	
	if (theDiv.style.display == 'none') {
		if (bottom==1) theJoin.src = icons[3].src;
		else theJoin.src = icons[2].src;
		theIcon.src = icons[5].src;
		theDiv.style.display = '';
	} else {
		if (bottom==1) theJoin.src = icons[1].src;
		else theJoin.src = icons[0].src;
		theIcon.src = icons[4].src;
		theDiv.style.display = 'none';
	}
}
// Push and pop not implemented in IE(crap!    donīt know about NS though)
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;
}


