//*****************************************************
//script para hacer un menú jerárquico en IE>=4 y NS>=6
//
// Autor: Ariel Alonzo Medina Vázquez http://www.prodigyweb.net.mx/arielmedina1978
// Última actualización: 27/04/2005
//*****************************************************

var ns=(navigator.userAgent.indexOf("Netscape")>-1)? 1: 0;
var ie=(navigator.userAgent.indexOf("MSIE")>-1)? 1: 0;

var menus=new Array;


//genera el menú
function doMenu()
{
var k;
var strmenu="";
var t=0;
var l=0;

document.write("<style type='text/css'> #submenulink {text-decoration:none; color:"+this.submenutextcolor+"}</style>");
document.write("<style type='text/css'> #menulink {text-decoration:none; color:"+this.menutextcolor+"}</style>");

for(k=0; k<this.root.childs.length; ++k)
	{
	if(this.hmenu)
		l=this.left+k*this.width;
	else
		t=this.top+k*this.height;
	strmenu+=this.root.childs[k].doMenu1(k,true,t,l);
	}

document.write(strmenu);
}

//utiliza las capas [129,129+profundidadMaximaDelMenu]
//por lo que si utilizas capas, deben ser de un indice <=128
function doMenu1(k,horiz,t,l)	{
var newIndex=128+this.level;
var strmenu="";
var top=0;
var left=0;

if(horiz)
	{
	if(this.url==null)
		strmenu+="<div id="+this.ident+" style='position:absolute; top:"+t+"px; left:"+l+"px; z-index:"+newIndex+";  width:"+(this.parentmenu.width-1)+"px; height:"+(this.parentmenu.height-1)+"px; text-align:center; color:"+this.parentmenu.menutextcolor +"; background:"+this.parentmenu.menubgcolor+"; visibility:visible; cursor:default ' onmouseover='expand(this)' onmouseout='this.style.background=\""+this.parentmenu.menubgcolor+"\"'>";
	else
		strmenu+="<div id="+this.ident+" style='position:absolute; top:"+t+"px; left:"+l+"px; z-index:"+newIndex+";  width:"+(this.parentmenu.width-1)+"px; height:"+(this.parentmenu.height-1)+"px; text-align:center; color:"+this.parentmenu.menutextcolor +"; background:"+this.parentmenu.menubgcolor+"; visibility:visible; cursor:default ' onmouseover='expand(this)' onmouseout='this.style.background=\""+this.parentmenu.menubgcolor+"\"' onclick='window.open(\""+this.url+"\")'>";

	if(this.childs.length>0)
		strmenu+=this.caption+" !";
	else
		{
		if(this.url==null)
			strmenu+=this.caption;
		else
			{
			if(this.samewindow==null || this.samewindow==false)
				strmenu+="<span id='menulink'>"+this.caption+"</span>";
				//strmenu+="<a href='"+this.url+"' id='menulink' target='_blank'>"+this.caption+"</a>";
			else
				strmenu+="<span id='menulink'>"+this.caption+"</span>";
				//strmenu+="<a href='"+this.url+"' id='menulink'>"+this.caption+"</a>";
			}
		}
	}
else
	{
	if(this.url==null)
		strmenu+="<div id="+this.ident+" style='position:absolute; top:"+t+"px; left:"+l+"px; z-index:"+newIndex+";  width:"+(this.parentmenu.width)+"px; height:"+(this.parentmenu.height)+"px; border-style:solid; border-width:1px 1px 1px 1px; border-color:black; color:"+this.parentmenu.submenutextcolor +"; background:"+this.parentmenu.submenubgcolor+"; visibility:hidden; cursor:default ' onmouseover='expand(this)' onmouseout='this.style.background=\""+this.parentmenu.submenubgcolor+"\"'>";
	else
		strmenu+="<div id="+this.ident+" style='position:absolute; top:"+t+"px; left:"+l+"px; z-index:"+newIndex+";  width:"+(this.parentmenu.width)+"px; height:"+(this.parentmenu.height)+"px; border-style:solid; border-width:1px 1px 1px 1px; border-color:black; color:"+this.parentmenu.submenutextcolor +"; background:"+this.parentmenu.submenubgcolor+"; visibility:hidden; cursor:default ' onmouseover='expand(this)' onmouseout='this.style.background=\""+this.parentmenu.submenubgcolor+"\"' onclick='window.open(\""+this.url+"\")'>";

	if(this.childs.length>0)
		strmenu+=this.caption+" &gt;&gt;";
	else
		{
		if(this.url==null)
			strmenu+=this.caption;
		else
			{
			if(this.samewindow==null || this.samewindow==false)
				strmenu+="<span id='submenulink'>"+this.caption+"</span>";
				//strmenu+="<a href='"+this.url+"' id='submenulink' target='_blank'>"+this.caption+"</a>";
			else
				strmenu+="<span id='submenulink'>"+this.caption+"</span>";
				//strmenu+="<a href='"+this.url+"' id='submenulink'>"+this.caption+"</a>";
			}
		}
	}

strmenu+="</div>";

if(this.childs.length>0)
	{
	if(this.parentmenu.hmenu)
		{
	for(var k=0; k<this.childs.length; ++k)
		{
		if(horiz)
			{
			top=(k+1)*this.parentmenu.height+t;
			left=l;
			}
		else
			{
			top=k*this.parentmenu.height+(t+5);
			left=l+(this.parentmenu.width-10);
			}

		strmenu+=this.childs[k].doMenu1(k,false,top,left);
		}
		}
	else
		{
	for(var k=0; k<this.childs.length; ++k)
		{
		if(horiz)
			{
			//top=(k+1)*this.parentmenu.height+t;
			top=k*this.parentmenu.height+t;
			left=l+(this.parentmenu.width);
			}
		else
			{
			top=k*this.parentmenu.height+(t+5);
			left=l+(this.parentmenu.width-10);
			}

		strmenu+=this.childs[k].doMenu1(k,false,top,left);
		}
		}
	}

return strmenu;
}

var capaauxiliar=0;

function setauxtier()
{
var it;

it=searchItem("tier0");
it.style.zIndex=127;
it.style.visibility="visible";

capaauxiliar=1;
}

function ocultarsubmenu()
{
var it;

for(var j=0; j<menus.length; ++j)
	{//revisar expanded
	ocultarhijos(menus[j].root.childs[menus[j].root.nsubmenu]);
	}
	
it=searchItem("tier0");
it.style.zIndex=0;
it.style.visibility="hidden";

capaauxiliar=0;
}

//expande los hijos del nodo cuando el ratón está sobre el elemento HTML relacionado
function expand(i)
	{
	
	if(capaauxiliar==0)
		{
		setauxtier();
		}

	var n=searchnode(i.id);
	var np=n.parent;

	if(np.parent!=null)
		n.htmlNode.style.background=n.parentmenu.mouseovercolor;

	if(np.nsubmenu!=n.parentindex && np.childs[np.nsubmenu].expanded)
		{
		ocultarhijos(np.childs[np.nsubmenu]);
		}

	if(n.expanded)
		{
		}
	else
		{
		np.nsubmenu=n.parentindex;
		mostrarhijos(n);
		}

	}

//Constructor de menu
function menu(t,l,w,h,mbgcolor,smbgcolor,mocolor,mtcolor,smtcolor,vert)
{
if(t!=null)
	this.top=t;
else
	this.top=0;

if(l!=null)
	this.left=l;
else
	this.left=0;

if(w!=null)
	this.width=w;
else
	this.width=100;

if(h!=null)
	this.height=h;
else
	this.height=20;

if(mbgcolor!=null)
	this.menubgcolor=mbgcolor;
else
	this.menubgcolor="red";

if(smbgcolor!=null)
	this.submenubgcolor=smbgcolor;
else
	this.submenubgcolor="yellow";

if(mocolor!=null)
	this.mouseovercolor=mocolor;
else
	this.mouseovercolor="brown";

if(mtcolor!=null)
	this.menutextcolor=mtcolor;
else
	this.menutextcolor="black";

if(smtcolor!=null)
	this.submenutextcolor=smtcolor;
else
	this.submenutextcolor="blue";

if(vert!=null && vert==true)
	this.hmenu=false;
else
	this.hmenu=true;

this.parentindex=menus.length;
this.contenedor=new Array; //arreglo asociativo para hacer búsqueda lineal en vez de búsqueda en el árbol
this.doMenu=doMenu;
this.root=new nodo("root",null,null,null,this);

menus[menus.length]=this; //Agregarlo al arreglo de menús para después asociar los nodos HTML

}

//función constructora de nodo
function nodo(caption,parentnode,url,samew,parentmenu)
	{
	if(parentnode!=null)
		{
		this.level=parentnode.level+1;
		parentnode.nsubmenu++;
		this.parentindex=parentnode.nsubmenu;
		}
	else
		{	
		this.level=0;
		this.parentindex=0;
		}

	this.parentmenu=parentmenu;
	this.ident=("i"+this.parentmenu.parentindex)+this.parentmenu.contenedor.length;
	this.parentmenu.contenedor[this.parentmenu.contenedor.length]=new Array(2);
	this.parentmenu.contenedor[this.parentmenu.contenedor.length-1][0]=this.ident;
	this.parentmenu.contenedor[this.parentmenu.contenedor.length-1][1]=this;

	this.caption=caption;
	this.childs=new Array;
	this.parent=parentnode;
	this.insnode=insnode;
	if(this.level>1)
		this.visible=false;
	else
		this.visible=true;

	if(this.level>0)
		this.expanded=false;
	else
		this.expanded=true;
	this.nsubmenu=-1;
	this.htmlNode=null;

	this.url=url;
	this.samewindow=samew;

	this.doMenu1=doMenu1;
	}

//inserta un nodo en el nodo que hace la llamada a esta función
function insnode(caption,url,samew)
	{
	this.childs[this.childs.length]=new nodo(caption,this,url,samew,this.parentmenu);
	return this.childs[this.childs.length-1];
	}

//oculta los hijos de un nodo y por lo tanto, los nietos si están visibles
function ocultarhijos(n)
	{
	for(var i=0; i<n.childs.length; ++i)
		{
		ocultarhijos(n.childs[i]);
		setDisplay(n.childs[i],false);
		n.childs[i].visible=false;
		}
	n.expanded=false;
	}

//muestra los hijos de un nodo, no los nietos
function mostrarhijos(n)
	{
	for(var i=0; i<n.childs.length; ++i)
		{
		setDisplay(n.childs[i],true);
		n.childs[i].visible=true;
		}
	n.expanded=true;
	}

//Busca el elemento HTML con id==identifier
function searchItem(identifier)
{
	for(var k=0; k<document.all.length; ++k)
		{
		if(document.all(k).id==identifier)
			return document.all(k);
		}

return null;
}

//hace visible o invisible un elemento
function setDisplay(n,val)
	{
	if(val)
		n.htmlNode.style.visibility="visible";
	else
		n.htmlNode.style.visibility="hidden";
	}

//hace una búsqueda lineal en el arreglo asociativo y devuelve el nodo
function searchnode(identifier)
	{
	for(var j=0; j<menus.length; ++j) //por ahora así, buscando en todos los menús, luego se mejorará
		{

	for(var i=0; i<menus[j].contenedor.length; ++i)
		{
		if(menus[j].contenedor[i][0]==identifier)
			return menus[j].contenedor[i][1];
		}

		}
	}

//Asocia el nodo del árbol con su nodo correspondiente html
function bindHTMLnodes()
	{
	if(ns>0)
		{
		for(var j=0; j<menus.length; ++j)
			{
			for(var i=0; i<menus[j].contenedor.length; ++i)
				{
				var n=document.getElementById(menus[j].contenedor[i][0]);
				menus[j].contenedor[i][1].htmlNode=n;
				}
			}
		}
	else if(ie>0)
		{
		for(var j=0; j<menus.length; ++j)
			{
			for(var i=0; i<menus[j].contenedor.length; ++i)
				{
				var n=searchItem(menus[j].contenedor[i][0]);
				if(n!=null)
					{
					menus[j].contenedor[i][1].htmlNode=n;
					}
				}
			}

		}
	}


// Se llama a la función bindHTMLnodes() cuando el documento se haya cargado, si sobreescribes el evento onLoad,
// fíjate que la nueva función incluya una llamada a bindHTMLnodes();
window.onload=bindHTMLnodes;

//Si el elemento que vamos a insertar tiene hijos, es necesario crear una variable para
//guardar la referencia al nuevo elemento insertado. Si no tiene hijos, sólo insértelo

/* Ej. Para ser colocado en la página HTML
*/