// JavaScript Document
// Opciones es un array del tipo
// opciones[0]=new Array('0','1','Poblaciones','C','','index.html');
//[0]=nodopadre
//[1]=nodo
//[2] texto
//[3] abierto/cerrado
//[4] S=submenu
//[5] link
//[6] target
//[7] Descripcion
//Constructor
function MenuVertical(nombre,obj){
    this.nombre = nombre
    this.MenuItems = new Array();
    this.Prof=-1;
    this.objeto=obj;
    this.PathClick=getCookie("PathClick"+this.objeto);
    if (this.PathClick==null) this.PathClick='';
	
	var color = gup( 'color' );
	if (color=='')
		color ='';
	else
		color ='_'+color;
	
    this.imgOpen='/apariencias/menu/imagenes/abriroff'+color+'.gif';
    this.imgClose='/apariencias/menu/imagenes/abrir'+color+'.gif';
}

MenuVertical.prototype.add = function(nodopadre, nodo, texto, estado, submenu, link, target, descripcion) {

 this.MenuItems[this.MenuItems.length] = new Array(nodopadre,nodo,texto,estado,submenu,link,target,descripcion);
 this.MenuItemsLong=this.MenuItems.length;

}
MenuVertical.prototype.TieneHijos = function (index)
{
	for (var i=0; i<this.MenuItemsLong; i++)
		if (this.MenuItems[i][0]==index) return true;
	return false;
}

MenuVertical.prototype.GetPadre = function (nodo)
{
	for (var i=0; i<this.MenuItemsLong; i++)
		if (this.MenuItems[i][1]==nodo) return i;
	return -1;

}

MenuVertical.prototype.GetHijo=function(nodo)
{
	for (var i=0; i<this.MenuItemsLong; i++)
		if (this.MenuItems[i][0]==nodo) return i;
	return -1;

}
MenuVertical.prototype.Identar=function(num)
{
	cadena='';
	for (var i=0; i<num; i++) cadena+='&nbsp;&nbsp;&nbsp;';
	return cadena;
}

//capturar parametros GET con javascript
function gup( name ){
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp ( regexS );
	var tmpURL = window.location.href;
	var results = regex.exec( tmpURL );
	if( results == null )
		return"";
	else
		return results[1];
}

MenuVertical.prototype.Dibujar=function(padre)
{
	Dibujo='';
	this.Prof++;
	for (var i=0; i<this.MenuItemsLong; i++) 
	{
		MTPadre=this.MenuItems[i][0];
		MTNodo=this.MenuItems[i][1];
		MTTexto=this.MenuItems[i][2];
		MTEstado=this.MenuItems[i][3];
		MTSubmenu=this.MenuItems[i][4];
		MTLink=this.MenuItems[i][5];
		MTTarget=this.MenuItems[i][6];
		MTTitulo=this.MenuItems[i][7];
     
	  // Prueba de Angel
	    imgsrc=this.imgClose;
		
		if (MTPadre==padre)	
		{
			trdisplay='block';
			if (padre!='0' && MTEstado=='C')
			{
				trdisplay='none'; 
				if (MTSubmenu=='S' && this.MenuItems[this.GetPadre(MTPadre)][3]=='A')  trdisplay='block'; 
			}
			divclass='OpcionNormal';
			if (MTEstado=='C') 
			{
				imgsrc=this.imgClose;
				divclass='OpcionNormal'; 
			}
			else
			{
				if (MTSubmenu=='S' && this.MenuItems[this.GetHijo(MTNodo)][3]=='A') {imgsrc=this.imgOpen; divclass='OpcionBold';}
			}
			
			classStyle='Prof'+this.Prof;
			classStyleTD='TDProf'+this.Prof;
			Dibujo+='<tr id=TR'+this.nombre+i+' class="'+classStyle+'" style="display: '+trdisplay+';" onmouseover=\'this.className="ProfOver";\' onmouseout=\'this.className="'+classStyle+'"\' onclick=\''+this.objeto+'.AbrirOpcion('+i+')\' width=100%><td width=100% class="'+classStyleTD+'" id=menu_'+MTNodo+'>';
				
			Dibujo+='<table border=0 cellspacing=0 cellpadding=2 class=TablaInt><tr>';
			Dibujo+='<td>'+this.Identar(this.Prof)+'</td>';
			Dibujo+='<td width=100%>';
			
			if (MARCAR_MENU=='si')
			{
				if (MTLink.substring(0,7) != 'http://')
				{
					if (MTLink.indexOf('?') != '-1')
						MTLink=MTLink+'&id_menu='+MTNodo;
					else
						MTLink=MTLink+'?id_menu='+MTNodo;
				}
			}
			
			if (MTLink!='') Dibujo+='<a href="'+MTLink+'" target="'+MTTarget+'" title="'+MTTitulo+'" >'+MTTexto+'</a>';
			else		Dibujo+='<a href="javascript:void(0);" title="'+MTTitulo+'">'+MTTexto+'</a>';
			Dibujo+='</td><td align=right valign=top>';
			if (this.TieneHijos(MTNodo)==true) { Dibujo+='<img id=IMG'+i+' src="'+imgsrc+'" align=absmiddle>'; } 
			Dibujo+='</td></tr></table>';
			Dibujo+='</td>';
			
			Dibujo+='</tr>';	
			
			Dibujo+=this.Dibujar(MTNodo);
		}
	}
	this.Prof--;

	return Dibujo;
}



MenuVertical.prototype.Init = function ()
{
	for (var i=0; i<this.MenuItemsLong; i++)
	{
		if (this.TieneHijos(this.MenuItems[i][1])==true) this.MenuItems[i][4]='S';		
	}

	for (var i=0; i<this.MenuItemsLong; i++) 
	{

		if (this.PathClick!='')
		{
			if (this.PathClick.charAt(i)=='A')   this.MenuItems[i][3]='A';
			else							this.MenuItems[i][3]='C';
		}
		else
		{
			if (this.MenuItems[i][0]!=0)
			{
				if (this.MenuItems[this.GetPadre(this.MenuItems[i][0])][3]=='A')
				{
					if (this.MenuItems[i][4]!='S') this.MenuItems[i][3]='A';
				}
				else this.MenuItems[i][3]='C';
			}
		}
		if (this.MenuItems[i][0]=='0' && this.MenuItems[i][4]!='S')		this.MenuItems[i][3]='A';

	}
	if (this.PathClick=='')
	{
		for (var i=0; i<this.MenuItemsLong; i++) 
		{
			this.PathClick+=this.MenuItems[i][3];
		}
		setCookie("PathClick"+this.objeto, this.PathClick,'','/');
	}
	Dibujo='<table border=0 cellspacing=0 cellpadding=0 class="Menu" width=100%>';
	Dibujo+=this.Dibujar(0);
	Dibujo+='</table>';
	//setTimeout("document.getElementById(this.nombre).innerHTML=Dibujo",2);
	document.getElementById(this.nombre).innerHTML=Dibujo;
}



MenuVertical.prototype.AbrirOpcion=function(opc)
{
	   this.CerrarTodoNivel(opc);
       this.CerrarTodo(opc);

	// abrir el indicado
	for (var i=0; i<this.MenuItemsLong; i++) 
	{
		
		if (this.MenuItems[i][0]==this.MenuItems[opc][1])
		{
			//oDivHijo=eval('document.getElementById(\'DIV'+i+'\');');
			oTr=eval('document.getElementById(\'TR'+this.nombre+i+'\');');
			if (this.MenuItems[opc][3]=='A')
			{	
				this.MenuItems[i][3]='C';
				oTr.style.display='none';
			}
			else
			{
				if (this.MenuItems[i][4]!='S') this.MenuItems[i][3]='A';
				oTr.style.display='block';
			}
		}
	}
	oImg=eval('document.getElementById(\'IMG'+opc+'\');');
	//oDiv=eval('document.getElementById(\'DIV'+opc+'\');');
	if (this.MenuItems[opc][3]=='A')
	{
		if (this.MenuItems[opc][4]=='S')
		{
			this.MenuItems[opc][3]='C';
			oImg.src=this.imgClose;
			//oDiv.className='OpcionNormal';
		}
	}
	else
	{
		if (this.MenuItems[opc][4]=='S')
		{
			this.MenuItems[opc][3]='A';
			oImg.src=this.imgOpen;
			//oDiv.className='OpcionBold';
		}
	}

	PathClick='';
	for (var i=0; i<this.MenuItemsLong; i++) 
	{
		PathClick+=this.MenuItems[i][3];
	}
	setCookie("PathClick"+this.objeto, PathClick,'','/');

}



MenuVertical.prototype.CerrarTodo=function(nodo)
{

 for (var i=0; i<this.MenuItemsLong; i++)
 {
  if (this.MenuItems[i][0]==this.MenuItems[nodo][1])
  {
   oTr=eval('document.getElementById(\'TR'+this.nombre+i+'\');');
   oTr.style.display='none';
   this.MenuItems[i][3]='C';
   if (this.MenuItems[i][3]=='C' && this.MenuItems[i][4]=='S')
   {
    oImg=eval('document.getElementById(\'IMG'+i+'\');');
    oImg.src=this.imgClose;
   }
   if (this.MenuItems[i][4]=='S') this.CerrarTodo(i);
  }
 }
}


MenuVertical.prototype.CerrarTodoNivel=function(nodo)
{
 padre=this.MenuItems[nodo][0];
 for (var i=0; i<this.MenuItemsLong; i++)
 {
  if (this.MenuItems[i][0]==padre && this.MenuItems[i][1]!=this.MenuItems[nodo][1])
  {
   oImg=eval('document.getElementById(\'IMG'+i+'\');');
   if (this.MenuItems [i][3]=='A' && this.MenuItems[i][4]=='S')
   {
     this.MenuItems[i][3]='C';
     oImg.src=this.imgClose;
   }
   if (this.MenuItems[i][4]=='S') this.CerrarTodo(i);
  }
 }
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

