// Title: NosTree
// Description: See the demo at url
// URL: http://www.cooldev.com/nostree/
// Version: 1.1
// Date: 02-18-2002 (mm-dd-yyyy)
// Author: Sergey Nosenko <darknos@cooldev.com>
// Notes: Registration needed to use this script on your web site.
// 	Registration for this version (1.1) is free of charge.
//	See official site for details

function NTreeFormat( fmt, tree )
{
	this.init = function( fmt, tree )
	{
		this.left 	= fmt[0];		
		this.top 	= fmt[1];		
		this.showB  = fmt[2];
		this.clB  = fmt[3][0];
		this.exB  = fmt[3][1];
		this.iE  = fmt[3][2];
		this.Bw  = fmt[4][0];
		this.Bh  = fmt[4][1];
		this.Ew  = fmt[4][2];
		this.showF  = fmt[5];
		this.clF  = fmt[6][0];
		this.exF  = fmt[6][1];
		this.iF  = fmt[6][2];
		this.Fw  = fmt[7][0];
		this.Fh  = fmt[7][1];
		this.ident = fmt[8];
		this.back = new NTreeBack(this.left, this.top, fmt[9], 'cls'+tree.name+'_back');
		this.nst = fmt[10];
		this.nstl = fmt[11];
		this.so = fmt[12];
		this.pg = fmt[13][0];
		this.sp = fmt[13][1];
		//preloading
		if (this.showB){
			this.e = new Image();
			this.e.src = this.clB;
			this.e1 = new Image();
			this.e1.src = this.exB;
			this.e5 = new Image();
			this.e5.src = this.iE;
		}
		if (this.showF){
			this.e2 = new Image();
			this.e2.src = this.exF;
			this.e3 = new Image();
			this.e3.src = this.clF;
			this.e4 = new Image();
			this.e4.src = this.iF;
		}
	}
	this.nstyle = function ( lvl )
	{
		return ( und(this.nstl[lvl]) ) ? this.nst : this.nstl[lvl];
	}
	this.idn = function( lvl )
	{
		var r = ( und(this.ident[lvl]) ) ? this.ident[0]*lvl : this.ident[lvl];
		return r;
	}
	this.init(fmt, tree);
}



function NosTree( name, nodes, format )
{
	this.name = name;
	this.fmt = new NTreeFormat(format, this);
	NTrees[this.name] = this;
	this.Nodes = new Array();
	this.rootNode = new NTreeNode(null, "", "", "", null);
	this.rootNode.treeView = this;
	
	this.selectedNode = null;
	this.maxWidth = 0;
	this.maxHeight = 0;
    this.ondraw = null;	
	this.nbn = function( nm ){
		for (var i = 0;i<this.Nodes.length;i++)
			if (this.Nodes[i].text == nm)
				return this.Nodes[i];
		return null;
	} 	

	this.addNode = function (node)
	{
		var parentNode = node.parentNode;
		this.Nodes = this.Nodes.concat([node]);
		node.index = this.Nodes.length - 1;
		if (parentNode == null) {
			this.rootNode.children = this.rootNode.children.concat([node]);
		}
		else
			parentNode.children = parentNode.children.concat([node]);
		return node;
	}
	this.rebuildTree = function()
	{
		var s = "";
		for (var i = 0; i < this.Nodes.length; i++){
			s += this.Nodes[i].init();
		}
		document.write(s);
		for (var i = 0; i < this.Nodes.length; i++)
		if (bw.ns4) {
			this.Nodes[i].el = document.layers[this.Nodes[i].id()+"d"];
			if (this.fmt.showF)
				this.Nodes[i].nf = this.Nodes[i].el.document.images[this.Nodes[i].id()+"nf"];
			if (this.fmt.showB)
				this.Nodes[i].nb = this.Nodes[i].el.document.images[this.Nodes[i].id()+"nb"];
		} else {
			this.Nodes[i].el = document.all? document.all[this.Nodes[i].id()+"d"] : document.getElementById(this.Nodes[i].id()+"d");		
			if (this.fmt.showB)
				this.Nodes[i].nb = document.all? document.all[this.Nodes[i].id()+"nb"] : document.getElementById(this.Nodes[i].id()+"nb");		
			if (this.fmt.showF)
				this.Nodes[i].nf = document.all? document.all[this.Nodes[i].id()+"nf"] : document.getElementById(this.Nodes[i].id()+"nf");		
		}
	}
	this.draw = function()
	{
		this.currTop = this.fmt.top;
		this.maxHeight =0; this.maxWidth=0;
		for (var i = 0; i < this.rootNode.children.length; i++)
			this.rootNode.children[i].draw(true);
		//this.fmt.back.resize(this.maxWidth-this.fmt.left, this.maxHeight - this.fmt.top);
		if (this.ondraw != null) this.ondraw();
	}
	
	this.updateImages = function ( node )
	{
		var srcB = node.expanded? this.fmt.exB : this.fmt.clB;
		var srcF = node.expanded? this.fmt.exF : this.fmt.clF;
		if (node.treeView.fmt.showB && node.nb && node.nb.src != srcB) node.nb.src = srcB;
		if (node.treeView.fmt.showF && node.nf && node.nf.src != srcF) node.nf.src = node.hasChildren() ? srcF : this.fmt.iF;
	}
	this.expandNode = function( index )
	{
		var node = this.Nodes[index];
// NUNCA PLIEGO/EXPANDO UNA OPCION SON SUBOPCION MARCADA
	    var hijoON=false;
		for (var i = 0; i < node.children.length; i++)
				if (node.children[i].text==menu) hijoON=true;
if (!hijoON){		
		if (!und(node) && node.hasChildren())
		{
			node.expanded = !node.expanded;
			this.updateImages(node);
			if (!node.expanded){
				node.hideChildren();
			} else {
				if (this.fmt.so && node.parentNode == null)
				{
					for (var i = 0; i < this.Nodes.length; i++){
						this.Nodes[i].show(false);
						if ( this.Nodes[i] != node && this.Nodes[i].parentNode == null) {
							this.Nodes[i].expanded = false;
							this.updateImages(this.Nodes[i]);
						}
					}
				}
			}
            this.draw();
//para aumentar el iframe si es necesario
parent.document.getElementById('cuerpo').style.height = document.getElementById("tablaprincipal").clientHeight;
		}
}	}

	this.selectNode = function( index )
	{
		var node = this.Nodes[index];
		if ( !und(node) )
			this.selectedNode = node;		
		node.draw();
	}
	
		this.readNodes = function (nodes)
		{
			var ind = 0;
			var par = null;
				function readOne( arr , tree)
				{
					if (und(arr)) return;
					var text = arr[0];
					var url = arr[1] == null? "javascript:none()": arr[1];
					var targ = arr[2] == null? "": arr[2];
					
					var node = tree.addNode(new NTreeNode(tree, par, text, url, targ))
					var i = 3;
					while (!und(arr[i]))
					{
						par = node;
						readOne(arr[i], tree);
						i++;
					}
				}
			if (und(nodes) || und(nodes[0]) || und(nodes[0][0])) return;
			for (var i = 0; i < nodes.length; i++){
				par = null;
				readOne(nodes[i], this);
			}
		}

		this.collapseAll = function( rd )
		{
			for (var i = 0; i < this.Nodes.length; i++){
				if (this.Nodes[i].parentNode != this.rootNode)
					this.Nodes[i].show(false);
				this.Nodes[i].expanded = false;
				this.updateImages(this.Nodes[i]);
			}
			if (rd) this.draw();
		}

		this.expandAll = function( rd )
		{
			for (var i = 0; i < this.Nodes.length; i++){
				this.Nodes[i].expanded = true;
				this.updateImages(this.Nodes[i]);
			}
			if (rd) this.draw();
		}
    

		this.init = function()
		{
			this.readNodes(nodes);
		// intento averiguar la url del nodo seleccionado
 		// 1. obtener el nodo raíz
			function buscaraiz (node){				
				while (node.parentNode != null){					
				node = node.parentNode;
				} 
				return node;
			}

			var raiz=buscaraiz(this);

		 // 2. recorre los nodos y devuelve la url del que está seleccionado actualmente
			function selec(){
				for (var i = 0;i<=raiz.Nodes.length;i++)					
					if (raiz.Nodes[i].text == menu)
						return raiz.Nodes[i].url;
				return "";
			}
			var urlactual=selec();

		// 3. construyo la cadena ruta que añadirá a la url de cada opción tantos '../' como sean necesarios según el nivel de la url actual        	

			splitValues=urlactual.split("/");
			//alert("urlactual = "+urlactual+"pasos = "+splitValues.length-1);
			ruta="";
			for(i=1;i<splitValues.length;i++)
				ruta="../"+ruta;

			//alert("RUTA = "+ruta);
			this.rebuildTree();
			this.draw();
		}

	this.init();


}



function NTreeNode( treeView, parentNode , text, url, target){
	this.index = -1;
	this.treeView = treeView;
	this.parentNode = parentNode;
	this.text = text;
	this.url = url;
	this.target = target;
	this.expanded = false;
	this.children = new Array();
	this.level = function(){
		var node = this;
		var i = 0;
		while (node.parentNode != null){
			i++;
			node = node.parentNode;
		}
		return i;
	}
	this.hasChildren = function(){
		return this.children.length > 0;
	}
	this.init = function(){
		var s = "";
		if (bw.ns4) {
			s = '<layer id="'+this.id()+'d" z-index="'+this.index+10+'" visibility="hidden">'+this.getContent()+'</layer>';
		} else {
			s = '<div id="'+this.id()+'d" style="display:none;z-index:'+eval(this.index+10)+';">';
		    if(this.index==0) s+='<img src="" width=1 height=9>';
			s += this.getContent()+'</div>';
             //alert(s);
		}    
		return s;
	}
	this.getH = function(){return bw.ns4 ? this.el.clip.height : this.el.offsetHeight;}
    this.getW = function(){return bw.ns4 ? this.el.clip.width : this.el.offsetWidth;}
	this.id = function(){return 'nt'+this.treeView.name+this.index;}
	this.getContent = function(){
		function itemSquare(node){
				var img = node.hasChildren() ? (node.expanded ? node.treeView.fmt.exF : node.treeView.fmt.clF) : node.treeView.fmt.iF;
				var w = node.treeView.fmt.Fw; var h = node.treeView.fmt.Fh;
				return "<td valign=\"middle\" width=\""+w+"\"><img id=\""+node.id()+"nf\" name=\""+node.id()+"nf\" src=\"" + img + "\" width="+w+" height="+h+" border=0></td>\n";
		}
		function buttonSquare(node){
			
			var img = node.expanded ? node.treeView.fmt.exB : node.treeView.fmt.clB;
			var w = node.treeView.fmt.Bw; var h = node.treeView.fmt.Bh;
			return '<td valign=\"middle\" width="'+w+'"><a href="javascript:none()" onclick="javascript:NTrees[\''+node.treeView.name+'\'].expandNode('+node.index+')" style="text-decoration:none; color:#000000"><img name=\''+node.id()+'nb\' id=\''+node.id()+'nb\' src="' + img + '" width="'+w+'" height="'+h+'" border=0></a></td>\n';
		}
		function blankSquare(node, ww){
			var img = node.treeView.fmt.iE;
			return "<td width=\""+ww+"\"><img src=\"" + img + "\" width="+ww+" height=1 border=0></td>\n"
		}
	
		var s = '';
		var ll = this.level();
		
		s += (this.text=='Publicidad') ? '<table width=150 cellpadding='+this.treeView.fmt.pg+' cellspacing='+this.treeView.fmt.sp+' border=0 class="cls'+this.treeView.name+'_back'+ll+'"><tr>'
			: '<table width=150 cellpadding='+this.treeView.fmt.pg+' cellspacing='+this.treeView.fmt.sp+' border=0 class="cls'+this.treeView.name+'_back'+ll+'" style="padding-left:5px;"><tr height=21>';
		//ident
		var idn = this.treeView.fmt.idn(ll);
	    //if (idn > 0)
			//s += blankSquare(this, idn);
		if ( this.treeView.fmt.showB)
			s += this.hasChildren() ? buttonSquare(this) : blankSquare(this, this.treeView.fmt.Ew);
		if ( this.treeView.fmt.showF)
			s += itemSquare(this);
//OPCION DE MENU ACTUAL MARCADA POR DEFECTO, LA VARIABLE "menu" SE DECLARA EN EL HTML QUE LLAMA AL SCRIPT
/*		if (menu == this.text){
			s += (ll==0) ? '<a href="'+this.url+'" target="'+this.target+'" onclick="javascript:NTrees[\''+this.treeView.name+'\'].expandNode('+this.index+')"><td width=150  nowrap=\"1\" class="opcion_over" id="fila'+this.index+'">'+this.text+'</td></a></tr></table>'
: '<a href="'+this.url+'" target="'+this.target+'" onclick="javascript:NTrees[\''+this.treeView.name+'\'].expandNode('+this.index+')"><td width=145  nowrap=\"1\" class="subopcion_over" id="fila'+this.index+'"><img src="'+ruta+'/imagenes/iconos/subop_menu_on.gif" style="margin-right:3px;vertical-align:middle;">'+this.text+'</td></a></tr></table>';
        }
// SI NO ES LA OPCION ACTUAL:
		else{	
				if(ll==0){	
					var hijoON=false;
					for (var i = 0; i < this.children.length; i++)
					if (this.children[i].text==menu) hijoON=true;
                    s+= (hijoON) ? '<td width=150  nowrap=\"1\" class="opcion_over" id="fila'+this.index+'">'+this.text+'</td></a></tr></table>' : '<a href="'+ruta+this.url+'" target="'+this.target+'" onMouseOver="fila'+this.index+'.className=\'opcion_over\'" onMouseOut="fila'+this.index+'.className=\'opcion\'" onclick="javascript:fila'+this.index+'.className=\'opcion_over\';NTrees[\''+this.treeView.name+'\'].expandNode('+this.index+')"><td width=150 nowrap=\"1\" class="opcion" id="fila'+this.index+'">'+this.text+'</td></a></tr></table>';
                 }
 				else   
					s += '<a href="'+ruta+this.url+'" target="'+this.target+'" onMouseOver="fila'+this.index+'.className=\'subopcion_over\'" onMouseOut="fila'+this.index+'.className=\'subopcion\'"  onclick="javascript:NTrees[\''+this.treeView.name+'\'].expandNode('+this.index+')"><td width=145 nowrap=\"1\" class="subopcion" id="fila'+this.index+'"><img src="'+ruta+'/imagenes/iconos/subop_menu_off.gif" style="margin-right:3px;;vertical-align:middle;">'+this.text+'</td></a></tr></table>';			
        }
*/



//SI ES LA OPCION ACTUAL

if (menu == this.text){
			//SI ESTAMOS EN LA OPCIÓN "Publicidad" LLAMAMOS A LA IMAGEN_ON EN LUGAR DE ESCRIBIR EL NOMBRE DE LA OPCIÓN - AHORA NO NECESITAMOS EL TAG, YA ESTAMOS EN PUBLICIDAD
			if (menu == 'Publicidad'){
				s += '<td width=150 id="fila'+this.index+'"><a href="'+ruta+this.url+'" target="'+this.target+'" style="text-decoration:none; color:#000000"><img src="imagenes/opcion_menu_on.gif" width=150 style="margin-top:20px; text-decoration:none; color:#000000" border=0></a></td></tr></table>';
			}
			else{
				s += (ll==0) ? '<td width=150  nowrap=\"1\" class="opcion_over" id="fila'+this.index+'"><a href="'+ruta+this.url+'" target="'+this.target+'" onclick="javascript:NTrees[\''+this.treeView.name+'\'].expandNode('+this.index+')" style="text-decoration:none; color:#000000">'+this.text+'</a></td></tr></table>'
	: '<td width=145  nowrap=\"1\" class="subopcion_over" id="fila'+this.index+'"><a href="'+ruta+this.url+'" target="'+this.target+'" onclick="javascript:NTrees[\''+this.treeView.name+'\'].expandNode('+this.index+')" style="text-decoration:none; color:#000000"><img src="'+ruta+'/img/iconos/subop_menu_on.gif" style="margin-right:3px;vertical-align:middle; text-decoration:none; color:#000000" border=0>'+this.text+'</a></td></tr></table>';
			} 
		}
// SI NO ES LA OPCION ACTUAL:
		else{	
	//SI ES OPCION DE NIVEL 0
				if(ll==0){	
							var hijoON=false;
							for (var i = 0; i < this.children.length; i++)
							if (this.children[i].text==menu) hijoON=true;
					// SI TIENE UN HIJO ACTIVO (está desplegada y no es enlace)
							if(hijoON) s+= '<td width=150  nowrap=\"1\" class="opcion_over" id="fila'+this.index+'" style="text-decoration:none; color:#000000">'+this.text+'</td></a></tr></table>' 
							else {
							//SI ES OPCION PRINCIPAL
								 //SI OPCION PRINCIPAL CON HIJOS
								 if (this.children.length > 0)	s+='<td width=150 nowrap=\"1\" class="opcion" id="fila'+this.index+'"><a href="javascript:;" onMouseOver="fila'+this.index+'.className=\'opcion_over\'" onMouseOut="fila'+this.index+'.className=\'opcion\'" onclick="javascript:fila'+this.index+'.className=\'opcion_over\';NTrees[\''+this.treeView.name+'\'].expandNode('+this.index+')" style="text-decoration:none;  color:#000000">'+this.text+'</a></td></tr></table>';
										
								 // OPCION PRINCIPAL SIN HIJOS
								 else	{
										//SI ESTAMOS EN UNA OPCIÓN DISTINTA DE "Publicidad"
										if (this.text != 'Publicidad') {
											s+='<td width=150 nowrap=\"1\" class="opcion" id="fila'+this.index+'"><a href="'+ruta+this.url+'" target="'+this.target+'" onMouseOver="fila'+this.index+'.className=\'opcion_over\'" onMouseOut="fila'+this.index+'.className=\'opcion\'" onclick="javascript:fila'+this.index+'.className=\'opcion_over\';" style="text-decoration:none;  color:#000000">'+this.text+'</a></td></tr></table>';
											}
										else  
											{
												//SI ESTAMOS EN LA OPCIÓN "Publicidad" LLAMAMOS A LA IMAGEN con roolover EN LUGAR DE ESCRIBIR EL NOMBRE DE LA OPCIÓN - AQUÍ VA EL TAG
// LAS SIGUIENTES 5 LINEAS CORRESPONDEN AL TAG DE CONTADOR DE VISITAS
										//ord=Math.random()*10000000000000000;
										//s += '<script language="JavaScript" src="http://ad.es.doubleclick.net/adj/travelplan.tsv/home;sz=150x48;ord=' + ord + '?" type="text/javascript"><\/script>';
										//s += '<noscript>';
										//s += '<a href="http://ad.es.doubleclick.net/jump/travelplan.tsv/home;sz=150x48;ord=123456789?" target="_blank"><img src="http://ad.es.doubleclick.net/ad/travelplan.tsv/home;sz=150x48;ord=123456789?" width="150" height="48" border="0" alt=""></a>';
										//s += '</noscript></td></tr></table>'; 	
									
 												
//ASI FUNCIONABA ANTES DEL TAG:  
					s += '<a href="'+ruta+this.url+'" target="'+this.target+'" onMouseOver="MM_swapImage(\'publicidad\',\'\',\''+ruta+'cuerpos/estatico/ofertas/turismo_tunez/imagenes/opcion_menu_on.gif\',1)" onMouseOut="MM_swapImgRestore()" style="text-decoration:none; color:#000000"><img src="'+ruta+'cuerpos/estatico/ofertas/turismo_tunez/imagenes/opcion_menu_off.gif" id="publicidad" border="0" style="margin-top:20px;"></a></td></tr></table>'


											}
														
												
                                 }
							}
						 }
					
	//SI ES SUBOPCION
 				else   
					s += '<td width=145 nowrap=\"1\" id="fila'+this.index+'"><a href="'+ruta+this.url+'" id="enlace'+this.index+'" class="subopcion" target="'+this.target+'" onMouseOver="enlace'+this.index+'.className=\'subopcion_over\'" onMouseOut="enlace'+this.index+'.className=\'subopcion\'"  onclick="javascript:NTrees[\''+this.treeView.name+'\'].expandNode('+this.index+')" style="text-decoration:none;"><img src="'+ruta+'img/iconos/subop_menu_off.gif" style="margin-right:3px; vertical-align:middle; text-decoration:none; color:#000000" border=0>'+this.text+'</a></td></tr></table>';			
        }
 

//LINEA QUE SEPARA LAS OPCIONES:
		if ( ll==0 || (ll>0 && this.index==parentNode.index+parentNode.children.length) ) s+= '<table width=150 height=1 cellpadding=0 cellspacing=0><tr><td><img src="../../img/pixel.gif" width="1" height="1" border="0"></td><tr></table>';      

		return s;
	}
	this.moveTo = function( x, y )
	{
		if (bw.ns4)
			this.el.moveTo(x,y);
		/*else
		{
			this.el.style.left=x;
			this.el.style.top=y;
		}*/
	}
	this.show = function(sh)
	{
		if (this.visible == sh)
			return;
		this.visible = sh;
		var vis = bw.ns4 ? (sh ? 'show': 'hide') : (sh ? 'inline': 'none');
		if (bw.ns4)
			this.el.visibility=vis;
		else
			this.el.style.display = vis;
	}
	this.hideChildren = function(){
			this.show(false);
			for (var i = 0; i < this.children.length; i++)
				this.children[i].hideChildren();
	}
	this.draw = function()
	{
		var ll = this.treeView.fmt.left;//this.treeView.fmt.idn(this.level());
		this.moveTo(this.treeView.fmt.left, this.treeView.currTop);
		if (ll+this.getW() > this.treeView.maxWidth)
			this.treeView.maxWidth = ll+this.getW();
		this.show(true);
		this.treeView.updateImages(this);
		this.treeView.currTop += this.getH();
		if (this.treeView.currTop > this.treeView.maxHeight)
			this.treeView.maxHeight = this.treeView.currTop;
		
//	PARA EXPANDIR-CONTRAER LOS NODOS CON HIJOS
			//if (this.hasChildren() )

 		var hijoON=false;
		for (var i = 0; i < this.children.length; i++)
				{if (this.children[i].text==menu) hijoON=true;}
        //if (hijoON){alert("tiene hijos activos");}
		if(hijoON || (this.expanded && this.hasChildren() ) ){
			for (var i = 0; i < this.children.length; i++)
				this.children[i].draw();}
	}
}

function NTreeBack( aleft, atop, color, name )
{
	this.left = aleft;
	this.top = atop;
	this.name = name;
	this.color = color;
	/*this.resize = function(w,h){
		if (bw.ns4){
			//this.el.resizeTo(w,h);
			this.resizeTo(w,h);
		}
		else
		{
			//this.el.style.width=w;
			//this.el.style.height=h;
			this.style.width=w;
			this.style.height=h;
		}
	}*/
	this.init = function(){
		if (bw.ns4) {
			var bgc = this.color == ""? "" : ' bgcolor="'+this.color+'" ';
			document.write('<layer '+bgc+' top="'+this.top+'" left="'+this.left+'" id="'+this.name+'" z-index="0"></layer>');
			this.el = document.layers[this.name];
		} else {
        document.write('<div id="'+this.name+'" style="background-color:'+this.color+';position:absolute;z-index:0;top:'+this.top+'px;left:'+this.left+'px"></div>');
		this.el = document.all? document.all[this.name] : document.getElementById(this.name);	
		}
	}
	this.init();
}

function none(){}

function lib_bwcheck(){
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera=this.agent.indexOf("Opera")>-1
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera);
}

function und( val )
{
	return typeof(val) == 'undefined';
}

var bw = new lib_bwcheck();
var NTrees;
if (typeof(NTrees) == 'undefined')
	NTrees = new Array();

