/**************************************************************************
	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.
**************************************************************************/

// 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/tree/plus.gif";
	icons[1] = new Image();
	icons[1].src = "images/tree/plusbottom.gif";
	icons[2] = new Image();
	icons[2].src = "images/tree/minus.gif";
	icons[3] = new Image();
	icons[3].src = "images/tree/minusbottom.gif";
	icons[4] = new Image();
	icons[4].src = "images/tree/folder.gif";
	icons[5] = new Image();
	icons[5].src = "images/tree/folderopen.gif";
}

// Colors base
col_r = 235;
col_g = 235;
col_b = 235;

// Sort ignoring Case
function sortic(a,b) {
a = string(a).toLowerCase();
b = string(b).toLowerCase();
if (a>b) return 1;
if (a<b) return -1;
return 0; }

// Create the tree
function createTree(arrName, startNode, openNode) {
	nodes = arrName;
	nodes.sort(function(x,y){ 
      var a = String(x).toUpperCase(); 
      var b = String(y).toUpperCase(); 
      if (a > b) 
         return 1 
      if (a < b) 
         return -1 
      return 0; 
    }); // Sort 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("|");
			document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[0] + "';return true;\" onmouseout=\"window.status=' ';return true;\" onfocus=\"blur()\"><img src=\"images/tree/folderopen.gif\" align=\"absbottom\" alt=\"\" />" + nodeValues[0] + "</a><br />");
		} //else document.write("<img src=\"images/tree/empty.gif\" align=\"absbottom\" alt=\"\" />Website<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[2]==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[2]==openNode) {
			openNodes.push(nodeValues[2]);
			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[2];
	}
	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("|");
		if (nodeValues[1] == parentNode) {
			var ls	= lastSibling(nodeValues[2], nodeValues[1]);
			var hcn	= hasChildNode(nodeValues[2]);
			var ino = isNodeOpen(nodeValues[2]);

			// Write out line & empty icons
			document.write("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"margin-left:-14px\">");
			document.write("<tr>");
			for (g=0; g<recursedNodes.length; g++) {
				if (recursedNodes[g] == 1) document.write("<td><img src=\"images/tree/line.gif\" align=\"absmiddle\" alt=\"li\"/></td>");
				else  document.write("<td><img src=\"images/tree/empty.gif\" align=\"left\" alt=\"em\" /></td>");
			}
			// 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("<td><img id=\"join" + nodeValues[2] + "\" src=\"images/tree/");
					 	if (ino) document.write("minus");
						else document.write("plus");
					document.write("bottom.gif\" align=\"absbottom\" alt=\"+-\" /></a></td>");
				} else {
					document.write("<td><img id=\"join" + nodeValues[2] + "\" src=\"images/tree/");
						if (ino) document.write("minus");
						else document.write("plus");
					document.write(".gif\" align=\"absbottom\" alt=\"+-\" /></a></td>");
				}
			} else {
				if (ls) document.write("<td><img src=\"images/tree/joinbottom.gif\" align=\"absbottom\" alt=\"jb\" /></td>");
				else document.write("<td><img src=\"images/tree/join.gif\" align=\"absbottom\" alt=\"jo\" /></td>");
			}
			// Start link
			document.write("<td><a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[0] + "';return true;\" onmouseout=\"window.status=' ';return true;\" onfocus=\"blur()\"></a></td>");
		
			// Write out folder & page icons
			if (hcn) {
  			if(nodeValues[3] == "#"){
  			  if(nodeValues[1] == "0"){
      			document.write("<td style=\"border-bottom:1px solid #BEC7CC;\"><img id=\"icon" + nodeValues[2] + "\" src=\"images/tree/folder")
					}
					else{
						document.write("<td style=\"border-bottom:1px solid #BEC7CC;\"><img id=\"icon" + nodeValues[2] + "\" src=\"images/tree/folder")
					}
				}
  			if (ino){
				  document.write("open");
				}
  			document.write(".gif\" align=\"absbottom\" alt=\"fo\" /></a></td>");
			}
			else{
			  document.write("<td style=\"border-bottom:1px solid #BEC7CC;\"><img id=\"icon" + nodeValues[2] + "\" src=\"images/tree/page.gif\" align=\"absbottom\" alt=\"pa\" /></td>");
			}
			
			// Write out node name
			document.write("<td width=\"100%\" style=\"border-bottom:1px solid #BEC7CC;\">");
			if(nodeValues[3] == "#"){
			  if(nodeValues[1] == "0"){
				  // Padre
				  document.write("<a href=\"javascript:void(null);\" onfocus=\"blur()\" onclick=\"openFathers("+nodeValues[2]+"); mostrarNuevoContenido('top_chimage.asp?idFam="+nodeValues[2]+"','top');\"><b>"+nodeValues[0]+"</b>");
				}
				else{
					// Familia
				  document.write("<a href=\"javascript:ocb("+nodeValues[1]+");oc("+nodeValues[2]+",1);\" onfocus=\"blur()\" onclick=\"mostrarNuevoContenido('families.asp?idFam="+nodeValues[2]+"','products'); mostrarNuevoContenido('top_chimage.asp?idFam="+nodeValues[2]+"','top');\"><b>"+nodeValues[0]+"</b>");
				}
			}
			else{
			// Articulo 
  			document.write("<a href=\"#Inicio\" onfocus=\"blur()\" onclick=\""+nodeValues[3]+";\">"+nodeValues[0]);
			}
			// End link
			document.write("</a></td>");
			document.write("</tr></table>");
			
			// If node has children write out divs and go deeper
			if (hcn) {
			  // Per anar canviant el color del div resultant
			    col_r = 235;
				col_g = 235;
				col_b = 235;

				col_r = col_r+10;
				col_g = col_g+10;
				col_b = col_b+10;
				
				document.write("<div id=\"div" + nodeValues[2] + "\"");
					if (!ino) document.write(" style=\"display: none; background-color:rgb("+col_r+","+col_g+","+col_b+")\"");
				document.write(">");
				addNode(nodeValues[2], recursedNodes);
				document.write("</div>");
			}
			// remove last line or empty icon 
			recursedNodes.pop();
		}
	}
}

// Manage open "Father-Nodes"
function openFathers(idNode){
  for(k=0; k<Fathers.length; k++){
  		var fathDiv = document.getElementById("div" + Fathers[k]);
		var fathJoin = document.getElementById("join" + Fathers[k]);
		var fathIcon = document.getElementById("icon" + Fathers[k]);
  	
		fathJoin.src = icons[0].src;
		fathIcon.src = icons[4].src;
		fathDiv.style.display = 'none';
	
		ocb(Fathers[k]);
	}
	oc(idNode, 1);
}

// 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
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;
}

// Close brothers
function ocb(parent){
	for (var i = 0; i < nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		var Divb = document.getElementById("div" + nodeValues[2]);
		var Iconb = document.getElementById("icon" + nodeValues[2]);
		if ((nodeValues[1] == parent) && (nodeValues[3] == '#')) {
				Iconb.src = icons[4].src;
				Divb.style.display = 'none';
		}
	}
}