/**************************************************************************
	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 htmlStr = "";

// 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 = "/images/layout/plus.gif";
	icons[1] = new Image();
	icons[1].src = "/images/layout/plusbottom.gif";
	icons[2] = new Image();
	icons[2].src = "/images/layout/minus.gif";
	icons[3] = new Image();
	icons[3].src = "/images/layout/minusbottom.gif";
	icons[4] = new Image();
	icons[4].src = "/images/layout/folder.gif";
	icons[5] = new Image();
	icons[5].src = "/images/layout/folderopen.gif";
	icons[5] = new Image();
	icons[5].src = "/images/layout/page.gif";
}

function createLinkTree(sctn, sctnNo) {
	Tree = new Array();
	createTree(Tree, 0, null, sctn, sctnNo);
}

// Create the tree
function createTree(tree, startNode, openNode, sctn, sctnNo) {

	if(sctn.getElementsByClassName) {
		var links  = sctn.getElementsByClassName('categoria');

		var counter = 0;
		for(var i = 0; i < links.length; i++) {
			var lastId = counter+1;
			tree[counter] = (lastId)+"|0|"+links[i].firstChild.innerHTML+"|"+links[i].firstChild.href;
			
			counter++;
			var links2 = links[i].getElementsByClassName('sottocategoria');
			for(var j = 0; j < links2.length; j++) {
				tree [counter] = (counter+1)+"|"+(lastId)+"|"+links2[j].firstChild.innerHTML+"|"+links2[j].firstChild.href;
				counter++;
			}
		}	
	} else {
		var links = sctn.getElementsByTagName('li');
		
		var lastLink = 0;
		for(var i = 0; i < links.length; i++) {
			if(links[i].className == 'categoria') {
				lastLink = i+1;
				tree[i] = (i+1)+"|0|"+links[i].firstChild.innerHTML+"|"+links[i].firstChild.href;
			} else
				tree[i] = (i+1)+"|"+lastLink+"|"+links[i].firstChild.innerHTML+"|"+links[i].firstChild.href;
			/*var links2 = links[i].getElementsByClassName('sottocategoria');
			for(var j = 0; j < links2.length; j++) {
				tree [links.length + j] = (links.length + j)+"|"+(i+1)+"|"+links2[j].firstChild.innerHTML+"|"+links2[j].firstChild.href;
			}*/
		}
	}
		
	this.htmlStr = "";
	
	nodes = tree;
	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("|");
			writeInDiv("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\"/images/layout/folderopen.gif\" align=\"absbottom\" alt=\"\" /><span>" + nodeValues[2] + "</span></a><br />");
		} 
	
		var recursedNodes = new Array();
		addNode(startNode, recursedNodes, htmlStr, sctnNo);
	}
	sctn.innerHTML = htmlStr;
	sctn.style.display='block';
	delete Tree;
}
// 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 to the tree
function addNode(parentNode, recursedNodes, str, sctnNo) {
	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 (recursedNodes[g] == 1) writeInDiv("<img src=\"/images/layout/line.gif\" align=\"absbottom\" alt=\"\" />");
				else  writeInDiv("<img src=\"/images/layout/empty.gif\" align=\"absbottom\" alt=\"\" />");
			}

			// put in array line & empty icons
			if (ls) recursedNodes.push(0);
			else recursedNodes.push(1);

			var id= "\'"+sctnNo + "." + nodeValues[0]+"\'";
			// Write out join icons
			if (hcn) {
				if (ls) {
					writeInDiv("<img id=\"join" + sctnNo + "." + nodeValues[0] + "\" src=\"/images/layout/");
					 	if (ino) writeInDiv("minus");
						else writeInDiv("plus");
					writeInDiv("bottom.gif\" align=\"absbottom\" alt=\"Open/Close node\" />");
				} else {
					writeInDiv("<img id=\"join" + sctnNo + "." + nodeValues[0] + "\" src=\"/images/layout/");
						if (ino) writeInDiv("minus");
						else writeInDiv("plus");
					writeInDiv(".gif\" align=\"absbottom\" alt=\"Open/Close node\" />");
				}
			} else {
				if (ls) writeInDiv("<img src=\"/images/layout/joinbottom.gif\" align=\"absbottom\" alt=\"\" />");
				else writeInDiv("<img src=\"/images/layout/join.gif\" align=\"absbottom\" alt=\"\" />");
			}
	
			// Start link
			writeInDiv("<a id="+id+" href=\"" + nodeValues[3] + "\" >");
			
			// Write out folder & page icons
			if (hcn) {
				if (ino) {
					writeInDiv("<img id=\"icon" + sctnNo + "." + nodeValues[0] + "\" src=\"/images/layout/folder")
					writeInDiv("open");
					writeInDiv(".gif\" align=\"absbottom\" alt=\"Folder\" />");
				}
			} else {
				// QUI HO CAMBIATO nodeValues[0] CON nodeValues[1]
				writeInDiv("<img id=\"icon" + sctnNo + "." + nodeValues[1] + "\" src=\"/images/layout/page.gif\" align=\"absbottom\" alt=\"Page\" />");
			}
			
			// Write out node name
			if(hcn || nodeValues[1] == 0) {
				writeInDiv(nodeValues[2]);
			} else {
				
				var tmp = new Array();
				tmp = nodeValues[2].split(' ');
				
				if(tmp.length > 1) {
					var len = tmp[0].length;
					
					var j = 0;
					for ( ; (j < tmp.length-1) && (len < 39) ; j++ ) {
						writeInDiv(tmp[j] + ' ');
						len += tmp[j+1].length + 1;
					}
						
					if(nodeValues[2].length >= len && nodeValues[2].length >= 39) {
						var space = "<br /><img src=\"/images/layout/line2.gif\" />";
						writeInDiv(space + tmp[j] + ' ' + nodeValues[2].substring(len));
					} else  {
						writeInDiv(tmp[j]);
					}
				} else {
					writeInDiv(nodeValues[2]);
				}
				
			}
			// End link
			writeInDiv("</a><br />");
			
			// If node has children write out divs and go deeper
			if (hcn) {
				writeInDiv("<div id=\"div" + sctnNo + "." + nodeValues[0] + "\"");
					if (!ino) writeInDiv(" style=\"display: none;\"");
				writeInDiv(">");
				addNode(nodeValues[0], recursedNodes, str, sctnNo);
				writeInDiv("</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 != null) {
		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 = '';
			//theBox.style.height  = ''+(50 + theAcrd.style.height)+'px';
		} else {
			if (bottom==1) theJoin.src = icons[1].src;
			else theJoin.src = icons[0].src;
			theIcon.src = icons[4].src;
			theDiv.style.display = 'none';
			//theBox.style.height  = ''+(50 + theAcrd.style.height)+'px';
		}
	}
}
// 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;
}

function writeInDiv(content) {
	this.htmlStr += content;
}
