//
// [AV] @fixme: is this file used anymore ? else remove
// [AV] @fixme: yes: used in ZprabiFolder Image editor
// [AV] @fixme: wrap into zprabo or (better) use dojo tree
//
/**************************************************************************
	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.
	
	<SH>
	Function calls:
		1 createTree()
		2 setOpenNodes()
		3 looping on:	1 addNode()
						2 lastSibling()
						3 hasChildNode()
						4 isNodeOpen()
		=> then the tree is displayed on the web browser with all nodes
		
	 	action on Tree: opening or closing nodes = function oc()
**************************************************************************/

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

// Loads all icons that are used in the tree
function preloadIcons(icons_root) {
	icons[0] = new Image();
	icons[0].src = icons_root + "plus.gif";
	icons[1] = new Image();
	icons[1].src = icons_root + "plusbottom.gif";
	icons[2] = new Image();
	icons[2].src = icons_root + "minus.gif";
	icons[3] = new Image();
	icons[3].src = icons_root + "minusbottom.gif";
	icons[4] = new Image();
	icons[4].src = icons_root + "imgfolder.gif";
	icons[5] = new Image();
	icons[5].src = icons_root + "folderopen.gif";
	icons[6] = new Image();
	icons[6].src = icons_root + "line.gif";
	icons[7] = new Image();
	icons[7].src = icons_root + "empty.gif";
	icons[8] = new Image()
	icons[8].src = icons_root + "joinbottom.gif";
	icons[9] = new Image()
	icons[9].src = icons_root + "joinbottom.gif";
}
// Create the tree
function createTree(icons_root, nodes, destNode, startNode, openNode) {
	
	if (nodes.length > 0) {
		
		preloadIcons(icons_root);
		//<SH>comment startNode = undefined
		if (startNode == null) startNode = 0;
		
		if (openNode != 0 || openNode != null) {
			openNodes = setOpenNodes(nodes, openNode);
		}
	
		if (startNode !=0) {
			var nodeValues = nodes[getArrayId(nodes, startNode)].split("|");
			document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] 
							+ "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\"" 
							+ icons[5].src + "\" align=\"absbottom\" alt=\"\" />" 
							+ nodeValues[2] + "</a><br />");
		}
		
		var recursedNodes = new Array();
		addNode(nodes, openNodes, startNode, recursedNodes, destNode, icons_root);
	}
}
// Returns the position of a node in the array
function getArrayId(nodes, 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(nodes, openNode) {
	
	var openNodes	= new Array();
	
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==openNode) {
			openNodes.push(nodeValues[0]);
			setOpenNodes(nodeValues[1]);
		}
	}
	return openNodes 
}
// Checks if a node is open
function isNodeOpen(openNodes, 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(nodes, 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(nodes, 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;
}

//<EC>
function setURL(node, url) {
   nod = document.getElementsByName(node)
   nod[0].value=url
   //nod[1].src=url
	}
	
// Adds a new node to the tree
function addNode(nodes, openNodes, parentNode, recursedNodes, destNode, icons_root) {

	for (var i = 0; i < nodes.length; i++) {

		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) {
			
			var ls	= lastSibling(nodes, nodeValues[0], nodeValues[1]);
			var hcn	= hasChildNode(nodes, nodeValues[0]);
			var ino = isNodeOpen(openNodes, nodeValues[0]);
			
			// Write out line & empty icons
			for (g=0; g<recursedNodes.length; g++) {
				if (recursedNodes[g] == 1) document.write("<img src=\""
															+ icons[6].src 
															+ "\" align=\"absbottom\" alt=\"\" />");
				else  document.write("<img src=\""
										+ icons[7].src 
										+ "\" 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: oc('"+destNode+"'," + nodeValues[0] + ", 0);\"><img id=\"join"+destNode+nodeValues[0] 
										+ "\" src=\"" + icons_root);
					 	if (ino) document.write("minus");
						else document.write("plus");
					document.write("bottom.gif\" align=\"absbottom\" alt=\"OC\" /></a>");
				} else {
					document.write("<a href=\"javascript: oc('"+destNode+"'," + nodeValues[0] + ", 0);\"><img id=\"join"+destNode+nodeValues[0] 
										+ "\" src=\"" + icons_root);
						if (ino) document.write("minus");
						else document.write("plus");
					document.write(".gif\" align=\"absbottom\" alt=\"OC\" /></a>");
				}
			} else {
				if (ls) document.write("<img src=\""
											+ icons[8].src +  "\" align=\"absbottom\" alt=\"\" />");
				else document.write("<img src=\""
										+icons[9].src + "\" align=\"absbottom\" alt=\"\" />");
			}

			// Start link
			//<SH> Send the URL of the selected image to the form
			if (nodeValues[3] != '#') document.write("<span onClick=\"setURL('"+destNode+"','" + nodeValues[2] + "')\">");
	   		else document.write("<span onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
	   		
   			
   			
			// Write out folder & page icons
			if (hcn) {
				document.write("<img id=\"icon"+destNode+nodeValues[0] + "\" src=\"" + icons_root + "imgfolder")
					if (ino) document.write("open");
				document.write(".gif\" align=\"absbottom\" alt=\"\" />");
			} else {
				// <SH> code # means that it's a current folder without any icon to display
				if (nodeValues[3] != '#') document.write("<img id=\"icon"+destNode+nodeValues[0] + "\" src=\""+nodeValues[3]+"\" width=\"16\" align=\"absbottom\" alt=\"\" /> ");
			}
			
			// Write out node name
			document.write(nodeValues[2]);
			if (!hcn) {document.write(nodeValues[4]+nodeValues[5]);}

			// End link
			document.write("</span><br />");
			
			// If node has children write out divs and go deeper
			if (hcn) {
				document.write("<div id=\"div"+destNode+nodeValues[0] + "\"");
					if (!ino) document.write(" style=\"display: none;\"");
				document.write(">");
				addNode(nodes, openNodes, nodeValues[0], recursedNodes, destNode, icons_root);
				document.write("</div>");
			}
			
			// remove last line or empty icon 
			recursedNodes.pop();
		}
	}
}
// Opens or closes a node
function oc(destiNode, node, bottom) {
	
	var theDiv = document.getElementById("div"+destiNode+node);
	var theJoin	= document.getElementById("join"+destiNode+node);
	var theIcon = document.getElementById("icon"+destiNode+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
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;
}
