function defocus(x) { 
if (navigator.appName == 'Microsoft Internet Explorer' || document.all) x.blur(); 
}

/**************************************************************************
	Copyright (c) 2001 Geir Landrö (drop@destroydrop.com)
	JavaScript Tree - www.destroydrop.com/hugi/javascript/tree/
	Version 0.96B
	Modified by T.G. Dallas - www.techlink.gr

	This script can be used freely as long as all copyright 
	messages are intact.
**************************************************************************/

// Arrays for nodes and icons
var nodes	= new Array();
var openNodes	= new Array();
var icons	= new Array();
var iconspath	= '/common/images/';
var textoc 	= 'Open/Close';
var textfo 	= 'Folder';
var textpg 	= 'Page';
var textoo	= 'WEBSITE';

// Return the page name, stripped from extensions and paths
function stripURL(url) {
	url = url.substring(url.lastIndexOf('/')+1, url.lastIndexOf('.'));
	return url;
}

// Returns the position of a link in the array
function getArrayIdFromURL(node) {
	for (i=0; i<Tree.length; i++) {
		var nodeValues = Tree[i].split('|');
		if (stripURL(nodeValues[3])==stripURL(node)) return i;
	}
}

// Loads all icons that are used in the tree
function preloadIcons() {
	icons[0] = new Image();
	icons[0].src = iconspath + 'arrow_down.gif';
	icons[1] = new Image();
	icons[1].src = iconspath + 'arrow_down.gif';
	icons[2] = new Image();
	icons[2].src = iconspath + 'arrow_up.gif';
	icons[3] = new Image();
	icons[3].src = iconspath + 'arrow_up.gif';
/*	icons[4] = new Image();
	icons[4].src = iconspath + 'folder.gif';
	icons[5] = new Image();
	icons[5].src = iconspath + 'folderopen.gif';
	icons[6] = new Image();
	icons[6].src = iconspath + 'base.gif';
	icons[7] = new Image();
	icons[7].src = iconspath + 'page.gif';
	icons[8] = new Image();
	icons[8].src = iconspath + 'line.gif';
	icons[9] = new Image();
	icons[9].src = iconspath + 'empty.gif';
	icons[10] = new Image();
	icons[10].src = iconspath + 'join.gif';
	icons[11] = new Image();
	icons[11].src = iconspath + 'joinbottom.gif';
 Not needed in this version */
}

// Create the tree
function createTree(arrName, openNode) {
	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('|');
			document.write('<a onclick="defocus(this);" href="' + nodeValues[3] + '" onmouseover="window.status=\'' + nodeValues[2] + '\'; return true;" onmouseout="window.status=\' \'; return true;"><img src="' + icons[5].src + '" align="absmiddle" border="0" alt="" />' + nodeValues[2] + '</a><br />');
		} 
 		else { 
			document.write('<a onclick="defocus(this);" href="/"><img src="' + icons[6].src + '" align="right" border="0" alt="" />' + textoo + '<br /></a>');
		}
We do not want to display the home node or a non-0 start node */
		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]);
			
			// Set css classes according to the depth of tree
			level_class = 'level1';
			level_subclass = 'sublevel2';
			for (g=0; g<recursedNodes.length; g++) {
				if (recursedNodes[g] == 1) 
					level_class = 'level' + (g+2);
					level_subclass = 'sublevel' + (g+3);
			}

/*			// put in array line & empty icons
			if (ls) 
				recursedNodes.push(0);
			else  
*/				recursedNodes.push(1);

			// Start link
			document.write('<a onclick="defocus(this);" href="' + nodeValues[3] + '" class="' + level_class + '" onmouseover="window.status=\'' + nodeValues[2] + '\'; return true;" onmouseout="window.status=\' \'; return true;">');

/*			// Write out folder & page icons
			if (hcn) {
				if (ino)
					document.write('<img id="icon' + nodeValues[0] + '" src="' + icons[5].src + '" align="absmiddle" border="0" alt="' + textfo + '" />');
				else
					document.write('<img id="icon' + nodeValues[0] + '" src="' + icons[4].src + '" align="absmiddle" border="0" alt="' + textfo + '" />');
			} 
			else 
				document.write('<img id="icon' + nodeValues[0] + '" src="' + icons[7].src + '" align="absmiddle" border="0" alt="' + textpg + '" />');
No folder icons wanted */			

			// Write out node name
			document.write(nodeValues[2]);

			// End link
			document.write('</a>');

			// Write out arrow icons
			if (hcn) {
				if (ls) {
					document.write('<a onclick="defocus(this);" href="javascript: oc(' + nodeValues[0] + ', 1);"><img id="join' + nodeValues[0] + '" ');
					if (ino)
						document.write('src="' + icons[3].src + '" align="right" border="0" alt="' + textoc + '" />');
					else
						document.write('src="' + icons[1].src + '" align="right" border="0" alt="' + textoc + '" />');
					document.write('</a>');
				} 
				else {
					document.write('<a onclick="defocus(this);" href="javascript: oc(' + nodeValues[0] + ', 0);"><img id="join' + nodeValues[0] + '" ');
					if (ino)
						document.write('src="' + icons[2].src + '" align="right" border="0" alt="' + textoc + '" />');
					else
						document.write('src="' + icons[0].src + '" align="right" border="0" alt="' + textoc + '" />');
					document.write('</a>');
				}
			} 
/*			else {
				if (ls) 
					document.write('<img src="' + icons[10].src + '" align="right" border="0" alt="" />');
				else 
					document.write('<img src="' + icons[11].src + '" align="right" border="0" alt="" />');
			}
No join icons wanted if there is no subdivision */
			
			// If node has children write out divs and go deeper
			if (hcn) {
				document.write('<div id="div' + nodeValues[0] + '\" ');
					if (!ino) document.write('style="display: none;"');
				document.write(' class="' + level_subclass + '">');
				addNode(nodeValues[0], recursedNodes);
				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);
No need because there are no icons */
	
	if (theDiv.style.display == 'none') {
		if (bottom==1) theJoin.src = icons[3].src;
		else theJoin.src = icons[2].src;
/*		theIcon.src = icons[5].src;
No need because there are no icons */
		theDiv.style.display = '';
	} 
	else {
		if (bottom==1) theJoin.src = icons[1].src;
		else theJoin.src = icons[0].src;
/*		theIcon.src = icons[4].src;
No need because there are no icons */
		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;
}

