/*********************************************************************************
	Copyright (c) 2001-2003 Geir Landrö (drop@destroydrop.com)
	JavaScript Tree - www.destroydrop.com/hjavascripts/tree/    	Version 0.96	
	This script can be used freely as long as all copyright messages are	intact.
*********************************************************************************/


var tree_items_separator='::';
var recursed_padding=17;
var op_cl_col_width = 18;

var nodes = new Array();
var openNodes = new Array();
var icons = new Array();

function preloadIcons() {
	icons[0] = new Image();
	icons[0].src = "includes/tree_menu/plus.gif";
	icons[1] = new Image();
	icons[1].src = "includes/tree_menu/minus.gif";
}


function createTree(arrName, startNode, openNode) {
	startNode=0;

	nodes = arrName;
	if (nodes.length > 0) {
		preloadIcons();
		if (startNode == null) startNode = 0;
		if (openNode != 0 || openNode != null) setOpenNodes(openNode);
	
		if (startNode !=0) {
			var nodeValues = nodes[getArrayId(startNode)].split(tree_items_separator);
			document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\"img/folderopen.gif\" align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a><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(tree_items_separator);
		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(tree_items_separator);
		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(tree_items_separator);
		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(tree_items_separator);
		if (nodeValues[1] == parentNode)
			lastChild = nodeValues[0];
	}
	if (lastChild==node) return true;
	return false;
}

// Adds a new node to the tree
function addNode(parentNode, recursedNodes) {
	for (var i = 0; i < nodes.length; i++) {

		var nodeValues = nodes[i].split(tree_items_separator);
		if (nodeValues[1] == parentNode) {
			
			var ls	= lastSibling(nodeValues[0], nodeValues[1]);
			var hcn	= hasChildNode(nodeValues[0]);
			var ino = isNodeOpen(nodeValues[0]);

         document.write('<div class="te" style="padding-left:'+(recursed_padding*recursedNodes.length)+'px">');
         document.write('<table cellpadding="0" cellspacing="0"><tr><td width="'+op_cl_col_width+'">');
			if (ls) recursedNodes.push(0); else recursedNodes.push(1);
			if (hcn) { //has child nodes
            if(ino) op_cl=1; else op_cl=0;
				if (ls) if_ls=1; else if_ls=0;
            document.write('<a href="#" onclick="oc(this,'+nodeValues[0]+','+if_ls+');return false"><img id="join'+nodeValues[0]+'" src="'+icons[op_cl].src+'" /></a>');
			}

			if(ino) nodeValues[2]='<span class="tree_item_selected">'+nodeValues[2]+'</span>';

         document.write('</td><td><a href="'+nodeValues[3]+'">'+nodeValues[2]+'</a></td></tr></table>');
			document.write('</div>');
			
			if(hcn) { // If node has children write out divs and go deeper
				document.write('<div id="div'+nodeValues[0]+'"');
				if(!ino) document.write(' style="display:none;"');
				document.write('>');
				addNode(nodeValues[0], recursedNodes);
				document.write('</div>');
			}
			
			recursedNodes.pop(); // remove last line or empty icon 
		}
	}
}

// Opens or closes a node
function oc(obj,node,bottom) {
   obj.blur();

	var theDiv = document.getElementById("div"+node);
	var theJoin	= document.getElementById("join"+node);
	
	if (theDiv.style.display=='none') {
		theJoin.src = icons[1].src;
		theDiv.style.display='';
	} else {
		theJoin.src = icons[0].src;
		theDiv.style.display='none';
	}
}




// 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;
}