/**************************************************************************
	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 nodes2			= new Array();
var openNodes2	= new Array();
var icons2			= new Array(6);

function inArray2(valor, arr) {
  i = 0;
  while (i < arr.length) {
    if (arr[i] == valor)
      return true;
    i++;
  }
  return false;
}

// Loads all icons that are used in the tree
function preloadIcons2() {
	icons2[0] = new Image();
	icons2[0].src = "img/plus.gif";
	icons2[1] = new Image();
	icons2[1].src = "img/plusbottom.gif";
	icons2[2] = new Image();
	icons2[2].src = "img/minus.gif";
	icons2[3] = new Image();
	icons2[3].src = "img/minusbottom.gif";
	icons2[4] = new Image();
	icons2[4].src = "img/folder.gif";
	icons2[5] = new Image();
	icons2[5].src = "img/folderopen.gif";
}
// Create the tree
function createTree2(arrName, startNode, openNode) {
	nodes2 = arrName;
	
	if (nodes2.length > 0) {
		preloadIcons2();
		if (startNode == null) startNode = 0;
		if (openNode != 0 || openNode != null) setOpenNodes2(openNode);
	
		/*if (startNode !=0) {
			var nodeValues = nodes2[getArrayId2(startNode)].split("|");
			document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">" + nodeValues[2] + "</a><br />");
		} else
		  document.write("<img src=\"img/base.gif\" align=\"absbottom\" alt=\"\" />Website<br />");*/
	  /*if ((startNode >3) && (!inArray2(startNode,NodosInicio))) {
			var nodeValues = nodes2[getArrayId2(startNode)].split("|");
			document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">" + nodeValues[2] + "</a><br />");
		}*/
	
		var recursedNodes = new Array();
		addNode2(startNode, recursedNodes);
	}
}
// Returns the position of a node in the array
function getArrayId2(node) {
	for (i=0; i<nodes2.length; i++) {
		var nodeValues = nodes2[i].split("|");
		if (nodeValues[0]==node) return i;
	}
}
// Puts in array nodes2 that will be open
function setOpenNodes2(openNode) {
//	alert(openNode);
	for (i=0; i<nodes2.length; i++) {
		
		var nodeValues = nodes2[i].split("|");
		
		
		//alert(nodeValues[0]);
		
		if (nodeValues[0]==openNode) {
			
			openNodes2.push(nodeValues[0]);
			setOpenNodes2(nodeValues[1]);
		}
	} 
}
// Checks if a node is open
function isNodeOpen2(node) {
	for (i=0; i<openNodes2.length; i++)
		if (openNodes2[i]==node) return true;
	return false;
}
// Checks if a node has any children
function hasChildNode2(parentNode) {
	for (i=0; i< nodes2.length; i++) {
		var nodeValues = nodes2[i].split("|");
		if (nodeValues[1] == parentNode) return true;
	}
	return false;
}
// Checks if a node is the last sibling
function lastSibling2 (node, parentNode) {
	var lastChild = 0;
	for (i=0; i< nodes2.length; i++) {
		var nodeValues = nodes2[i].split("|");
		if (nodeValues[1] == parentNode)
			lastChild = nodeValues[0];
	}
	if (lastChild==node) return true;
	return false;
}
// Adds a new node to the tree
function addNode2(parentNode, recursedNodes) {
	
	for (var i = 0; i < nodes2.length; i++) {

		var nodeValues = nodes2[i].split("|");
		if (nodeValues[1] == parentNode) {
			
			var ls	= lastSibling2(nodeValues[0], nodeValues[1]);
			var hcn	= hasChildNode2(nodeValues[0]);
			var ino = isNodeOpen2(nodeValues[0]);

			// Write out line & empty icons
/***********************************************************
			for (g=0; g<recursedNodes.length; g++) {
				if (recursedNodes[g] == 1) document.write("<img src=\"img/line.gif\" align=\"absbottom\" alt=\"\" />");
				else  document.write("<img src=\"img/empty.gif\" 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: oc2(" + nodeValues[0] + ", 1);\"><img id=\"join" + nodeValues[0] + "\" src=\"img/");
					 	if (ino) document.write("minus");
						else document.write("plus");
					document.write("bottom.gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
				} else {
					document.write("<a href=\"javascript: oc2(" + nodeValues[0] + ", 0);\"><img id=\"join" + nodeValues[0] + "\" src=\"img/");
						if (ino) document.write("minus");
						else document.write("plus");
					document.write(".gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
				}
			} else {
				if (ls) document.write("<img src=\"img/joinbottom.gif\" align=\"absbottom\" alt=\"\" />");
				else document.write("<img src=\"img/join.gif\" align=\"absbottom\" alt=\"\" />");
			}
*************************************************/
			// Start link
			var imagen=3;
			if (nodeValues[4] < 2)
			 imagen=eval(nodeValues[4]+"+1");
			//alert(eval(nodeValues[4]));
      document.write('<table width="100%"  border="0" cellpadding="0" cellspacing="0">');
      document.write('<tr>');
      document.write('  <td class="menulateral" height="20">&nbsp;');
      
      for (ind=0; ind<eval(nodeValues[4]); ind++) {
        document.write('&nbsp;&nbsp;');
      }
//      document.write('  <img src="images/icono_menu_'+imagen+'.gif"><a href="' + nodeValues[3] + '" onmouseover="window.status=\'' + nodeValues[2] + '\';return true;" onmouseout="window.status=\' \';return true;">'+nodeValues[2]+'</a>');
      document.write('  <img src="images/icono_menu_'+imagen+'.gif">&nbsp;&nbsp;<a class="menulateral" href="' + nodeValues[3] + '" title="'+nodeValues[2]+'">'+nodeValues[2]+'</a>');
      document.write('  </td>');
      document.write('  <td class="menulateral"><img src="images/flecha_menu_lateral.gif" width="5" height="5"></td>');
      document.write('</tr>');
      document.write('<tr class="menulineadivision">');
      document.write('  <td colspan="2"><img src="images/null.gif" width="1" height="1"></td>');
      document.write('</tr>');
      document.write('</table>');
			//document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
			
/*********************************************
			// Write out folder & page icons
			if (hcn) {
				document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"img/folder")
					if (ino) document.write("open");
				document.write(".gif\" align=\"absbottom\" alt=\"Folder\" />");
			} else document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"img/page.gif\" align=\"absbottom\" alt=\"Page\" />");
*****************************************************/			
			// Write out node name
			//document.write(nodeValues[2]);

			// End link
			//document.write("</a>");
			
			// 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(">");
				addNode2(nodeValues[0], recursedNodes);
				document.write("</div>");
			}
			
			// remove last line or empty icon 
			recursedNodes.pop();
		}
	}
}
// Opens or closes a node
function oc2(node) {
	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 = icons2[3].src;
		//else theJoin.src = icons2[2].src;
		//theIcon.src = icons2[5].src;
		//theDiv.style.display = '';
		//VerNodos2();
		CerrarNodos2();
		//VerNodos2();
		setOpenNodes2(node);
		//VerNodos2();
		AbrirNodos2();
	} else {
		//if (bottom==1) theJoin.src = icons2[1].src;
		//else theJoin.src = icons2[0].src;
		//theIcon.src = icons2[4].src;
		theDiv.style.display = 'none';
	}
}

function VerNodos2() {

  var cadena="";
	for (i=0; i<openNodes2.length; i++) 
    cadena+=openNodes2[i]+"->";
  //alert(cadena);
}

function CerrarNodos2 () {

  var nodofuera;
  while (openNodes2.length>0) {
    nodofuera=openNodes2.pop();
    if (hasChildNode2(nodofuera))
      document.getElementById("div" + nodofuera).style.display = 'none';
  }
//  alert(openNodes2.length);
}

function AbrirNodos2 () {

  var cadena="";
  for (i=0; i<openNodes2.length; i++) {
    //alert(openNodes2[i]);
    //if (hasChildNode2(openNodes2[i]))
      document.getElementById("div" + openNodes2[i]).style.display = '';
	}
}


