function Menu ()
{
	
	this.width = 100;
	this.height = 30;
	this.align = "center";
	this.sentido = "horizontal";

	
	this.Subwidth = 200;
	
	this.id = new Array();
	this.idpadre = new Array();
	this.nombre = new Array();
	this.link = new Array();
	this.desp = new Array();

	this.NewItem = function (id, idpadre, nombre, link)
	{
		this.id[this.id.length] = id;
		this.idpadre[this.idpadre.length] = idpadre;
		this.nombre[this.nombre.length] = nombre;
		
		if (link != "")
			this.link[this.link.length] = "location.href=\'"+link+"\'";
		else
			this.link[this.link.length] = "";
			
		
		
		if (idpadre == 0)
		{
			if (this.sentido == 'horizontal')
				this.desp[this.desp.length] = 'abajo';
				
			if (this.sentido == 'vertical')
				this.desp[this.desp.length] = 'derecha';
		}
		else
			this.desp[this.desp.length] = 'derecha';
	}
	
	this.Crear = function()
	{
		var hijos = false;

		this.txt = '<table class="Principal" width="'+this.width+'" height="'+this.height+'" border="0" align="'+this.align+'" cellpadding="0" cellspacing="0" id="MenuPrincipal">';
		
		if (this.sentido == 'horizontal')
		this.txt = this.txt + '<tr>';

		
		
		for (i=0; i < this.id.length; i++)
		{
			//Crea el Menu Principal
			if (this.idpadre[i] == 0)
			{
				hijos = false;
				for (j=0; j < this.id.length; j++)
				{
					if (this.id[i] == this.idpadre[j])
					{
						hijos = true;
						j = this.id.length;
						
					}
					
				}
				
				if (this.sentido == 'vertical')
				{
					this.txt = this.txt + '<tr>';
					aux = ' height="'+this.height+'" width="'+this.width+'" ';
				}
				else
				{
					aux = "";	
				}
				
				if (hijos)
				
				this.txt = this.txt + '<td '+aux+' class="ItemPrincipal" onmouseover="VerMenu(\'Sub'+this.id[i]+'\',\'Sec'+this.id[i]+'\',\''+this.desp[i]+'\'); this.className=\'ItemPrincipalOver\'" onmouseout="OcultaMenu(\'Sub'+this.id[i]+'\',\'Sub'+this.id[i]+'\'); this.className=\'ItemPrincipal\'" id="Sec'+this.id[i]+'" OnClick="'+this.link[i]+'">'+this.nombre[i]+'</td>';
				
				else
				
				this.txt = this.txt + '<td '+aux+' class="ItemPrincipal" id="Sec'+this.id[i]+'" onmouseover="this.className=\'ItemPrincipalOver\'" onmouseout="this.className=\'ItemPrincipal\'" OnClick="'+this.link[i]+'">'+this.nombre[i]+'</td>';
				
			
				if (this.sentido == 'vertical')
				this.txt = this.txt + '<tr>';
			
			}
			

		
		}		
		
		if (this.sentido == 'horizontal')
		this.txt = this.txt + '<tr>';
		
		this.txt = this.txt + '</table>';


		//CREA LOS SUBMENUES
		var band = 0;
		
		for (i=0; i < this.id.length; i++)
		{
			if (this.idpadre[i] != 0)
			{

				for (j=0; j < this.id.length; j++)
				{
					if (this.idpadre[i] == this.idpadre[j])
					{
						if (band == 0)
						{
							this.txt = this.txt + '<table width="'+this.Subwidth+'" border="0" style="position:absolute; display:none" id="Sub'+this.idpadre[i]+'" cellpadding="0" cellspacing="0" class="SubMenu">';
							band = 1;
						}
						
							hijos = false;
							for (x=0; x < this.id.length; x++)
							{
								if (this.id[j] == this.idpadre[x])
								{
									hijos = true;
									x = this.id.length;
									
								}
								
							}
							
							if (hijos)
						
							this.txt = this.txt + '<tr><td class="ItemSubMenu ItemConSubmenu" width="'+this.Subwidth+'" onmouseover="VerMenu(\'Sub'+this.id[j]+'\',\'Sec'+this.id[j]+'\',\''+this.desp[j]+'\');this.className=\'ItemSubMenuOver ItemConSubmenu\'" onmouseout="OcultaMenu(\'Sub'+this.id[j]+'\',\'Sub'+this.idpadre[i]+'\');this.className=\'ItemSubMenu ItemConSubmenu\'" id="Sec'+this.id[j]+'" OnClick="'+this.link[j]+'">'+this.nombre[j]+'</td></tr>';
							
							else
							
							this.txt = this.txt + '<tr><td class="ItemSubMenu" width="'+this.Subwidth+'" onmouseover="this.className=\'ItemSubMenuOver\'" onmouseout="OcultaMenu(\'Sub'+this.idpadre[j]+'\',\'Sub'+this.idpadre[i]+'\');this.className=\'ItemSubMenu\'" id="Sec'+this.id[j]+'" OnClick="'+this.link[j]+'">'+this.nombre[j]+'</td></tr>';
							
								
					}
					
				}
				
				for (p=0; p < this.id.length; p++)
				{
					if (this.idpadre[i] == this.idpadre[p])
					{
						this.idpadre[p] = 0;
					}
				}
				
				if (band == 1)
				this.txt = this.txt + '</table>';
				
				band = 0;
			
			}
		
		}


		document.write(this.txt);
	
	}
}

var object = false;
var Abiertos = "";
var proce = false;

function VerMenu (id, idpadre,acc)
{
	setTimeout('MuestraMenu("'+id+'","'+idpadre+'","'+acc+'")',10);

}

function MuestraMenu (id, idpadre,acc)
{
var top;
var left;

	if (Abiertos.search(id+",")==-1)
	Abiertos = Abiertos+id+",";
	
	if (acc == 'abajo')
	{
			//calcula el top y left del menu padre
			top = getElementPositionT(idpadre);
			left = getElementPositionL(idpadre);
	
			document.getElementById(id).style.left = left;
			document.getElementById(id).style.top = top+(document.getElementById("MenuPrincipal").height*1)-2;
			document.getElementById(id).style.display = "block";
			
	}

	if (acc == 'derecha')
	{
			//calcula el top y left del menu padre
			top = getElementPositionT(idpadre);
			left = getElementPositionL(idpadre);
			document.getElementById(id).style.left = left+(document.getElementById(idpadre).width*1);
			document.getElementById(id).style.top = top;
			document.getElementById(id).style.display = "block";
			
	}

}

function OcultaMenu (id, idpadre)
{
	setTimeout('Oculta("'+id+'","'+idpadre+'")',10);
}
function Oculta (id,idpadre)
{
	var idnow = object.id
	var idabierto;
	
	if (object.tagName.toUpperCase() == 'TD')
	object = object.parentNode;
	
	if (object.tagName.toUpperCase() == 'TR')
	object = object.parentNode;
	
	if (object.tagName.toUpperCase() == 'TBODY')
	object = object.parentNode;
	
	if (object.id != id)
	{
		document.getElementById(id).style.display = "none";
		//Abiertos = Abiertos.replace(id+",","");
		
		if (object.id != idpadre)
		{
			document.getElementById(idpadre).style.display = "none";
			//Abiertos = Abiertos.replace(idpadre+",","");
			OcultaTodos ();
		}
		
	}

}

function OcultaTodos ()
{
	var idabierto;
	var cierra = true;
	idabierto = Abiertos.split(',');
	
	object = objTabla(object);
	
	for (i=0; i < idabierto.length-1; i++)
	{
		if (object.id == idabierto[i])
		cierra = false;
	}
	
	if (cierra)
	{
		Abiertos = "";


			for (i=0; i < idabierto.length-1; i++)
			{
				document.getElementById(idabierto[i]).style.display = "none";
			}
	}

}
function objTabla (elem)
{
	var objeto = elem;

	if (objeto.tagName.toUpperCase() == 'TD')
	objeto = objeto.parentNode;
	
	if (objeto.tagName.toUpperCase() == 'TR')
	objeto = objeto.parentNode;
	
	if (objeto.tagName.toUpperCase() == 'TBODY')
	objeto = objeto.parentNode;
	
	return objeto;

}

//DEVUELVE EL LEFT DE UN OBJETO
function getElementPositionL(elemID) {

	var offsetTrail = document.getElementById(elemID);
	var offsetLeft = 0;
	while (offsetTrail) {
		offsetLeft += offsetTrail.offsetLeft;
		offsetTrail = offsetTrail.offsetParent;
	}
	if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined" && navigator.appName=="Microsoft Internet Explorer" ) {
		offsetLeft += parseInt(document.body.leftMargin);
	}
	return offsetLeft ;
}

//DEVUELVE EL TOP DE UN OBJETO
function getElementPositionT(elemID) {

	var offsetTrail = document.getElementById(elemID);
	var offsetTop = 0;
	while (offsetTrail) {
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined" && navigator.appName=="Microsoft Internet Explorer" ) {
		offsetTop += parseInt(document.body.topMargin);
	}
	return offsetTop;
}

//Guarda en la variale object el elemento en el cual estoy posicionado
function onMouseOver ()
{
	 object = window.event.srcElement;
}
document.onmouseover = onMouseOver;

