/**
 * @class
 * Esta clase se utiliza para manejar el path del arbol
 */

/**
 * @constructor
 * @param {objecto}	oNodo	Objeto nodo con la info para mostrar como texto y tambien con el onclick para ejecutar
 */
function arbolPath(oNodo) {
	// ==========================================
	// Propiedades
	// ==========================================
	if (typeof oNodo != "undefined") {
		this.aPila = new Array(oNodo);		
	} else {
		this.aPila = new Array();
	} 
	this.oArbolPath = document.getElementById("ruta");

	/**
	 * Esta funcion es la encargada de dibujar el path recorriendo aPila
	 */
	this.draw = function () 
		{
			//Vacio el div
			this.oArbolPath.innerHTML = "";
			//Lo Armo Nuevamente
			for(var i = 0; i < this.aPila.length; i++) {
				var oNodo = this.aPila[i];
				if (oNodo.parentNode) {
					if (i < this.aPila.length-1) {
						if (i > 0) {
							this.oArbolPath.innerHTML += " | ";
						}					
						this.oArbolPath.innerHTML += "<a onclick=\"oArbolPath.click(" + i + ",'" + oNodo.parentNode.id +"');\">" + this.getTextNode(oNodo) + "</a>";
					} else {
						if (i > 0) {
							this.oArbolPath.innerHTML += " / ";
						}
						//alert("Nodo: " + oNodo + "\nParent: " + oNodo.parentNode);					
						this.oArbolPath.innerHTML += "<span onclick=\"oArbolPath.click(" + i + ",'" + oNodo.parentNode.id +"');\">" + this.getTextNode(oNodo) + "</span>";						
					}					
				}
			}
		}

	/**
	 * Pone como fijo un item
	 */
	this.setFijo = function(oNodo) {
		if (this.aPila.length > 0) {
			this.aPila[0] = oNodo;
		} else {
			this.aPila.push(oNodo);
		}
		this.draw();
	}
	
	/**
	 * Devuelve verdadero si existe como fijo un item
	 * @return	bool
	 */
	this.isSetFijo = function () 
		{
			return (this.aPila.length > 0); 
		}
	/**
	 * Setea el path correspondiente en la barra
	 * @param {numeric}	nNivel	Nivel correspondiente en el nodo
	 * @param {object}	oNodo	Objeto con la informacion
	 */
	this.setPath = function(nNivel,oNodo) 
		{
			if (this.getTextNode(this.aPila[0]) == this.getTextNode(oNodo)) {
				//Hizo click en el home
				this.aPila = this.aPila.slice(0,1);
			} else {
				//Primero borro la informacion en el array desde ese nivel para abajo
				this.aPila = this.aPila.slice(0,nNivel);
				//Agrego la info
				this.aPila.push(oNodo);
			}
			//dibujo nuevamente la info
			this.draw();				
		}
		
	/**
	 * Esta funcion devuelve el texto que esta dentro del <span>
	 * @param {object}	oNode	Objeto para buscar el texto
	 * @return string
	 */
	this.getTextNode = function(oNodo) 
		{
			var re = /<span>(.+)<\/span>/i;
			var aResult = oNodo.innerHTML.match(re);
			var sReturn = "";
			if (aResult != null && aResult.length >= 2) {
				sReturn = aResult[1];
			}
			return sReturn;
		}
	/**
	 * Dispara el onclick del menu
	 */
	this.click = function(nIndex,sId) 
		{			
			var oParent = document.getElementById(sId);
			if (oParent) {
				var oLinks = oParent.getElementsByTagName("A");
				oLinks[0].click();
			}
			//this.aPila[nIndex].click();
		}
}

/**
 * Esta funcion se puso para no modificar el principal.xsl. Como se usa en un monton de lados entonces la creo aca y lo unico que hago es mandar al home
 */
function treeOnStart() {
	//if (typeof oArbolPath != "undefined" && oArbolPath.aPila.length > 0) {
	//	oArbolPath.click(0);
	//}
	if(document.getElementById("ruta")){
		if(document.getElementById("ruta").childNodes){
			if(document.getElementById("ruta").childNodes[0]){
				if(document.getElementById("ruta").childNodes[0].onclick){
					document.getElementById("ruta").childNodes[0].onclick();
				}
			}
		}
	}
}