/**************************************************************************
	Copyright (c) 2001 Geir Landrö (drop@destroydrop.com)
	JavaScript Tree - www.destroydrop.com/hugi/javascript/tree/
	Version 0.96	

	This script can be used freely as long as all copyright messages are
	intact.
**************************************************************************/

// Arrays for nodes and icons
// Néstor Mata C. Ingreso la variable para manejo de cerrar el menu abierto cuando se abre otro.
var itemAbierto = null;
var nodes		= new Array();
var openNodes	= new Array();
var icons		= new Array(12);
var defaultimgpath = "img";
var imgpath;
var OpenIconsEffect = true; // Cuando se crean más de dos árboles en una misma página,
                            // no se puede usar el efecto visual de iconos abiertos.  
							// a menos que los identificadores en cada uno de los árboles
							// sean totalmente distintos.

// Loads all icons that are used in the tree
function preloadIcons(iconspath) {	
	imgpath = iconspath;
	icons[0] = new Image();
	icons[0].src = iconspath + "/plus.gif";
	icons[1] = new Image();
	icons[1].src = iconspath + "/plusbottom.gif";
	icons[2] = new Image();
	icons[2].src = iconspath + "/minus.gif";
	icons[3] = new Image();
	icons[3].src = iconspath + "/minusbottom.gif";
	icons[4] = new Image();
	icons[4].src = iconspath + "/folder.gif";  // el nombre de este gif no puede cambiar porque se emplea abajo sin extensión.
	icons[5] = new Image();
	if (OpenIconsEffect) icons[5].src = iconspath + "/folderopen.gif"
	else icons[5].src = iconspath + "/folder.gif";
	icons[6] = new Image();
	icons[6].src = iconspath + "/base.gif"	
	icons[7] = new Image();
	icons[7].src = iconspath + "/line.gif";		
	icons[8] = new Image();
	icons[8].src = iconspath + "/empty.gif";			
	icons[9] = new Image();
	icons[9].src = iconspath + "/join.gif";	
	icons[10] = new Image();
	icons[10].src = iconspath + "/joinbottom.gif";	
	icons[11] = new Image();
	if (OpenIconsEffect) icons[11].src = iconspath + "/page.gif"
	else icons[11].src = iconspath + "/folder.gif";
	
	// [EDITAR]: En caso de tener que usar otra imagen para el menu
	// modificar los siguientes.
	icons[12] = new Image();
	icons[12].src = "images/boton.gif";	
	icons[13] = new Image();
	icons[13].src = "images/boton_on.gif";	
	// [EDITAR]: Lo mismo pero para los links que comienzar por "_"
	icons[14] = new Image();
	icons[14].src = "images/boton_claro.gif";	
	icons[15] = new Image();
	icons[15].src = "images/boton_claro.gif";
}

// Create the tree
// Si openNode es cero, muestra el arbol completamente abierto.
function createTree(arrName, treeName, startNode, openNode, iconsPath) {
	if(!treeName) treeName = '';
	nodes = arrName;
	if (nodes.length > 0) {
		if (iconsPath != null) preloadIcons(iconsPath)
		else preloadIcons(defaultimgpath);
		if (startNode == null) startNode = 0;
		if (openNode != 0 || openNode != null) setOpenNodes(openNode);		
		if (openNode == 0) openTree();	
		if (startNode !=0) {
			var nodeValues = nodes[getArrayId(startNode)].split("|");
			// Néstor Mata comento lo siguiente:
			//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 />");
		} else {
			document.write("<img src\"images\\line2.gif\" alt=\"\" />");
			document.write("<img src=\"" + icons[6].src + "\" align=\"absbottom\" alt=\"\" /><b> " + treeName + "</b><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[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) {
			if (!isNodeOpen(nodeValues[0])) 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;
}

function remove(s, t) {
  /*
  **  Remove all occurrences of a token in a string
  **    s  string to be processed
  **    t  token to be removed
  **  returns new string
  */
  i = s.indexOf(t);
  r = "";
  if (i == -1) return s;
  r += s.substring(0,i) + remove(s.substring(i + t.length), t);
  return r;
}
  
// Adds a new node in the tree
function addNode(parentNode, recursedNodes, opened, recur) {
	
	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]);


			var desplegar = nodeValues[2];
			var info = false;
			if(desplegar.charAt(1)=='_'){
				info = true;
				desplegar = desplegar.substr(2);
			}

			if(desplegar.charAt(4)=='_'){
				info = true;
				desplegar = desplegar.substr(0,3)+desplegar.substr(5);
			}
			
			if(recur)
				document.write("<tr><td background=\"" + icons[info?15:13].src + "\">");
			else document.write("<tr><td background=\"" + icons[info?14:12].src + "\">");
			
			
			// put in array line & empty icons
			if (ls) recursedNodes.push(0);
			else recursedNodes.push(1);

			// Start link
			document.write("<table cellspacing=0 cellpadding=0 border=0><tr class=\"menuitem\"><td>");
			document.write("<img src=\"images/spacer.gif\" width=\"");
			// [EDITAR]: Modificar el valor que corresponde al ancho del espacio
			// anterior al texto
			document.write(recur?25:15); 
			// [EDITAR]: Modificar el valor de "height" en las siguientes lineas
			// para el alto de la imagen del menu.
			if(hcn)
				document.write("\" height=23></td><td align=left nowrap width=\"200\"> <a target=\"" + nodeValues[4] + "\" href=\"" + nodeValues[3] + "\" onmouseover=\""+nodeValues[3]+"return true;\" onmouseout=\"window.status=' ';return true;\">");
			else 
				document.write("\" height=23></td><td align=left nowrap width=\"200\"> <a target=\"" + nodeValues[4] + "\" href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + desplegar + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
			
			if(desplegar.length > 20){
				document.write(remove(desplegar,"&nbsp;"));
			}else{
				// Write out node name
				document.write(desplegar);
			}
			// End link
			// Néstor cambio
			document.write("</a></td></tr></table></td></tr>");
			//Daniel Cambio.
			document.write("<tr><td><img src=\"images/spacer.gif\" height=\"5\" width=\"1\"></td></tr>");
			
			// If node has children write out divs and go deeper
			if (hcn) {
				// Néstor
				document.write("<tr><td width=\"100%\">");
				document.write("<div id=\"div" + nodeValues[0] + "\"");
					if (!ino) document.write(" style=\"display: none; padding-left: 10px; position: relative;\"");
				document.write(">");
				// Néstor
				document.write("<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" background=\"images/MenuIzA1.jpg\">");
				addNode(nodeValues[0], recursedNodes, opened,true);
				// Néstor
				document.write("</table>");
				document.write("</div>");
				//Néstor 
				document.write("</td></tr>");
			}
			
			// 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);
	

	// Néstor Mata Cuthbert. Cierra el menu abierto anteriormente
	if(itemAbierto != null && itemAbierto != theDiv){
		itemAbierto.style.display = "none";
		itemAbierto = 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 = '';
		itemAbierto = theDiv;
	} else {
		//if (bottom==1) theJoin.src = icons[1].src;
		//else theJoin.src = icons[0].src;
		//theIcon.src = icons[4].src;
		theDiv.style.display = 'none';
		itemAbierto = null;
	}

}

// Devuelve una lista con los identificadores de las hojas del arbol (nodos sin hijos).
function leavesList() {
	var listaDeHojas = '';
	for (x=0; x<nodes.length; x++) { // Notar que el contador no puede ser i, que es usado en setOpenNodes()
		var nodeValues = nodes[x].split("|");		
		var hasNoChildren = true;
		for (y=0; y<nodes.length; y++) {
			nodeValues2 = nodes[y].split("|");										
			if (nodeValues2[1]==nodeValues[0]) hasNoChildren = false;
		}		
		if (hasNoChildren) {
			if (listaDeHojas == '') listaDeHojas = nodeValues[0]
			else listaDeHojas = listaDeHojas + ',' + nodeValues[0]; 			
		}
	}	
	return listaDeHojas;
}

// Abre el arbol completamente.
function openTree() {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");	
		if (!isNodeOpen(nodeValues[0])) openNodes.push(nodeValues[0]);			
	} 
}

// 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;
}
