var ns4 = ((navigator.appName == "Netscape") && document.layers)?1:0;
var ns6 = ((navigator.appName == "Netscape") && document.getElementById)?1:0;
var ie4 = ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4) && (navigator.userAgent.indexOf('Opera') == -1))?1:0;
var op6 = (navigator.userAgent.indexOf('Opera') != -1)?1:0;
var ie7 = (navigator.userAgent.indexOf('MSIE 7.0;') != -1)?1:0;

// opera is compatible with the Netscape 6 version of this menu
if (op6 == 1) ns6 = 1; 
 
var intLastTimeout;
var objCurrentMenu;

window.MouseMoveX = 0;
window.MouseMoveY = 0;

function Menu(menuName)
{
	if (!window.Menus) {
		window.Menus = new Array();
        }

	this.ID = window.Menus.length;
	this.Name = menuName;
	this.Type = "Menu";
	this.Width = 0;                //Ancho del menu
	this.fontWidth = 8;            //Ancho de la fuente

	// this gets modified each time an entry is added - modify the add item function to 
	// increment this value by the estimated height of each menu item
	this.Height = 0;
	
	// variables to hold the painted area of the menu
	this.topX = 0;
	this.topY = 0;
	this.botX = 0;
	this.botY = 0;

        //Colores por defecto
        this.BorderColor = 'A2A2A2';
	this.ItemOnColor = '000000';
	this.ItemOnBgColor = 'FDFAD6';
        this.ItemOnBorderColor = this.ItemOnColor;
	this.ItemOffColor = '555555';
	this.ItemOffBgColor = 'F5F5F5';
	this.ItemOffBorderColor = this.ItemOffBgColor;

	// containers for properties
	this.Items = new Array();
	this.Links = new Array();
	this.OnColors = new Array();
	this.OffColors = new Array();
	this.OnBgColors = new Array();
	this.OffBgColors = new Array();
	
	// procedures for Menu object
	this.AddMenuItem = fAddMenuItem;
	this.ShowMenu = fShowMenu;
	this.HideMenu = fHideMenu;
	this.ShowMenuEx = fShowMenuEx;
	this.WriteMenus = fWriteMenus;
	
	// handles usage of menu
	this.Written = 0;
	this.Activated = 0;
	
	window.Menus[window.Menus.length] = this;
}

function fAddMenuItem(sLink, sText, sOnColor, sOffColor, sOnBgColor, sOffBgColor) {
	if(sText!=null) {
		lenText = sText.length * this.fontWidth;	
		if(this.Width < lenText) {
			   this.Width = lenText;
		}
	}
	
	if (sText) this.Items[this.Items.length] = sText; else this.Items[this.Items.length] = '';
	if (sLink) this.Links[this.Links.length] = sLink; else this.Links[this.Links.length] = '';
	if (sOnColor) this.OnColors[this.OnColors.length] = sOnColor; else this.OnColors[this.OnColors.length] = '';
	if (sOffColor) this.OffColors[this.OffColors.length] = sOffColor; else this.OffColors[this.OffColors.length] = '';
	if (sOnBgColor) this.OnBgColors[this.OnBgColors.length] = sOnBgColor; else this.OnBgColors[this.OnBgColors.length] = '';
	if (sOffBgColor) this.OffBgColors[this.OffBgColors.length] = sOffBgColor; else this.OffBgColors[this.OffBgColors.length] = '';
	
	// adjust the height of the menu container by estimated size of each menu item
	this.Height += 22;
}

function fWriteMenus()
{
	for (x = 0; x < window.Menus.length; x++)
	{
		obj = window.Menus[x];
		
		if (obj.Written == 1) continue;
	    obj.Written = 1;
		
		if (ns4 == 1)
		{/*
			strParBeg = "<layer ID=syMenu{ID} width={WIDTH} VISIBILITY=Hidden onmouseover=\"fMarkMenuActivated({ID});\" ONMOUSEOUT=\"fHideCurrentMenu();\">";
			strParBeg += "<table cellspacing=0 cellpadding=1 border=0 width=100% bgcolor=000000>";
			strParBeg += "<tr><td>";
			strParBeg += "<table cellspacing=0 cellpadding=4 border=0 width=100% bgcolor={BGCOLOR}>";
	
			strParEnd = "</table>";
			strParEnd += "</td></tr>";
			strParEnd += "</table>";
			strParEnd += "</layer>";*/
			
		} else if (ns6 == 1 || ie4==1) {
			//<%-- PARA VISUALIZAR EL iframe PONER frameborder a 10 --%>
 			strParBeg  = "<iframe id='IFrmShim{ID}' src='javascript:false;' scrolling='no' frameborder='0' style='position:absolute; top:0px; left:0px; width:{WIDTH}px; display:none;'></iframe>";
			strParBeg += "<div ID='syMenu{ID}' style='position:absolute; z-index:100; top:0px; left:0px; width:{WIDTH}px; background:#{BORDERCOLOR}; padding:1px; visibility:hidden;' onmouseover='fMarkMenuActivated({ID});'>";
			strParBeg += "<div align='left' style='background:#{BGCOLOR}; padding:3px;'>";

			strParEnd = "</div>";
			strParEnd += "</div>";

		} else {
			return;
		}

		strParBeg = strParBeg.replace(/{ID}/g, x);
		strParBeg = strParBeg.replace(/{WIDTH}/g, obj.Width);
		strParBeg = strParBeg.replace(/{BGCOLOR}/g, obj.ItemOffBgColor);
		strParBeg = strParBeg.replace(/{BORDERCOLOR}/g, obj.BorderColor);

                document.writeln(strParBeg);
		
		for (y = 0; y < obj.Items.length; y++)
		{
			if (ns4 == 1)			
			{/*
				strItemBeg = "<tr><td>";
				strItemEnd = "";
			
				if (obj.Links != '')
				{
					strItemBeg += "<A HREF=\"" + obj.Links[y] + "\">";
					strItemEnd += "</A>";
				}
	
				strItemBeg += "<FONT FACE='Verdana, Arial' SIZE=1 COLOR={COLOR}>";
	
				strItemEnd += "</FONT>";
				strItemEnd += "</td></tr>";*/
			}
			else if (ns6 == 1 || ie4==1)
			{
                            strItemBeg = "<div ID='syMenu{ID}SubMenu{SID}1' style='background:#{BORDERCOLOR}; padding:1px;'>";
                            strItemBeg += "<div ID='syMenu{ID}SubMenu{SID}2' style='background:#{BGCOLOR}; color:#{COLOR}; font-family:Verdana, Arial; font-size:{FONTWIDTH}pt; cursor:pointer; padding:2px;' onmouseover='fMenuItemMouseStyle({ID}, {SID}, 1);' ONMOUSEOUT='fMenuItemMouseStyle({ID}, {SID}, 0);' onClick='fMenuItemClick({ID}, {SID});'>";

                            strItemEnd = "</div>";
                            strItemEnd += "</div>";
			}
			
			//determine color settings
			if (obj.OffColors[y] == '') strColor = obj.ItemOffColor; else strColor = obj.OffColors[y];
			if (obj.OffBgColors[y] == '') strBgColor = obj.ItemOffBgColor; else strBgColor = obj.OffBgColors[y];
			strBorderColor = obj.ItemOffBorderColor;

			strItemBeg = strItemBeg.replace(/{ID}/g, x);
			strItemBeg = strItemBeg.replace(/{SID}/g, y);
			strItemBeg = strItemBeg.replace(/{COLOR}/g, strColor);
			strItemBeg = strItemBeg.replace(/{WIDTH}/g, obj.Width - 5);
			strItemBeg = strItemBeg.replace(/{FONTWIDTH}/g, obj.fontWidth);
			strItemBeg = strItemBeg.replace(/{BGCOLOR}/g, strBgColor);
			strItemBeg = strItemBeg.replace(/{BORDERCOLOR}/g, strBorderColor);			
			
			if (obj.Items[y] != '') {
                                //Escritura los items del menu
				document.writeln(strItemBeg);
				document.writeln(obj.Items[y]);
				document.writeln(strItemEnd);
			} else {
				if (ns4 == 1) {
					//document.writeln("<tr><td align='center'>");
					//fMenuSeparator(obj.Width);
					//document.writeln("</td></tr>");
				} else {
					//document.writeln("<center>");
					fMenuSeparator(obj.Width);
					//document.writeln("</center>");
				}
				
			}
		}
		document.writeln(strParEnd);
	}
}

function fMenuSeparator(Width)
{
	document.writeln("<img src='/imagenes/menu/hsep.gif' border='0' height='3' width='"+(Width-6)+"'><br>");
}

function fMenuItemClick(id, sid)
{
	objMenu = window.Menus[id];
	
	if (objMenu.Links[sid] != '')
		document.location = objMenu.Links[sid];
}

function fMenuItemMouseStyle(id, sid, state)
{
	objMenu = window.Menus[id];

	if (ie4 == 1) {
		objItemBorder = document.all('syMenu' + id + 'SubMenu' + sid + '1');
		objItemText = document.all('syMenu' + id + 'SubMenu' + sid + '2');
	} else if (ns6 == 1) {
		objItemBorder = document.getElementById('syMenu' + id + 'SubMenu' + sid + '1');
        objItemText = document.getElementById('syMenu' + id + 'SubMenu' + sid + '2');
	} else {
		return;
	}
	
	if (state == 1) {	
	    // menu is on
		if (objMenu.OnColors[sid] == '') strColor = objMenu.ItemOnColor; else strColor = objMenu.OnColors[sid];
		if (objMenu.OnBgColors[sid] == '') strBgColor = objMenu.ItemOnBgColor; else strBgColor = objMenu.OnBgColors[sid];
		strBorderColor = objMenu.ItemOnBorderColor;
	} else {		
	    // menu is off
		if (objMenu.OffColors[sid] == '') strColor = objMenu.ItemOffColor; else strColor = objMenu.OffColors[sid];
		if (objMenu.OffBgColors[sid] == '') strBgColor = objMenu.ItemOffBgColor; else strBgColor = objMenu.OffBgColors[sid];
		strBorderColor = objMenu.ItemOffBorderColor;
    }

	objItemBorder.style.background = '#'+strBorderColor;
	objItemText.style.color = '#'+strColor;
	objItemText.style.background = '#'+strBgColor;
}

function get_absolute_possition(element, top_or_left) 
{
   var possition = 0;
   var bodyTag0 = document.getElementsByTagName("body")[0]
   do {
         if (top_or_left == "top") {
             possition += element.offsetTop;
         } else {
             possition += element.offsetLeft;
         }
         element = element.offsetParent;
   } while (element!=bodyTag0 && element.offsetParent);
   return possition;
}


function fShowMenu(objLabel)
{
	if (objCurrentMenu) {
		objCurrentMenu.HideMenu();
	}

    var xPos = get_absolute_possition(objLabel, 'left');
    var yPos = get_absolute_possition(objLabel, 'top') + objLabel.offsetHeight + 2;
    //
    //objImg = document.images[imgName];
	//if (ie4 == 1) {
  	  //xPos = moveXbySlicePos(0, objImg);
      //yPos = moveYbySlicePos(objImg.height, objImg);
    //} else {
	  //xPos = objImg.x;
	  //yPos = objImg.y + objImg.height;
	  //xPos = tttX;
	  //yPos = tttY;
    //}
	//yPos += 2;

        if (ie7 == 1) {
                xPos+=10;
                yPos+=10;
                objMenu = document.all('syMenu' + this.ID);
        } else if (ie4 == 1) {
                //xPos+=10;
                //yPos+=10;
		objMenu = document.all('syMenu' + this.ID);
	} else if (ns4 == 1) {
		//objMenu = document.layers['syMenu' + this.ID];
	} else if (ns6 == 1) {
		objMenu = document.getElementById('syMenu' + this.ID);
	} else {
		return;
	}

	objCurrentMenu = window.Menus[this.ID];
	objCurrentMenu.topX = xPos;
	objCurrentMenu.topY = yPos;
	objCurrentMenu.botX = xPos + this.Width;
	objCurrentMenu.botY = yPos + this.Height;

	if (ie4 == 1) {
		objMenu.style.pixelTop = yPos;
		objMenu.style.pixelLeft = xPos;
		objMenu.style.visibility = 'visible';
	} else if (ns4 == 1) {/*
		objMenu.top = yPos;
		objMenu.left = xPos;
		objMenu.visibility = 'visible';*/
	} else if (ns6 == 1) {
		objMenu.style.top = yPos+'px';
		objMenu.style.left = xPos+'px';
		objMenu.style.visibility = 'visible';
	}

    IfrRef = document.getElementById('IFrmShim' + this.ID);	
    objMenu.style.display = "block";
    IfrRef.style.top = yPos+'px';
    IfrRef.style.left = xPos+'px';
    IfrRef.style.width = objMenu.offsetWidth;
    IfrRef.style.height = objMenu.offsetHeight;
    IfrRef.style.zIndex = objMenu.style.zIndex - 1;
    IfrRef.style.display = "block";
   
	fClearTimeout();
	intLastTimeout = setTimeout('fHideMenuIfNotActivated()', 6000);
}

function fShowMenuEx(Align, Offset)
{
	if (Align == 'Right') {
		xPos = window.MouseMoveX - this.Width + Offset;
		yPos = window.MouseMoveY;
	} else {
		xPos = window.MouseMoveX;
		yPos = window.MouseMoveY;
	}

	this.ShowMenu(xPos, yPos);
}

function fHideMenu()
{
	window.Menus[this.ID].Activated = 0;
	
	if (ie4 == 1) {
		objMenu = document.all('syMenu' + this.ID);
		objMenu.style.visibility = 'hidden';
	} else if (ns4 == 1) {/*
		objMenu = document.layers['syMenu' + this.ID];
		objMenu.visibility = 'hide';*/
	} else if (ns6 == 1) {
		objMenu = document.getElementById('syMenu' + this.ID);
		objMenu.style.visibility = 'hidden';
	}

    IfrRef = document.getElementById('IFrmShim' + this.ID);	
    IfrRef.style.display = "none";
	
	objCurrentMenu = null;
}

function fHideCurrentMenu()
{
	if (objCurrentMenu)
		objCurrentMenu.HideMenu();
}

function fMarkMenuActivated(id)
{
	fClearTimeout();
	window.Menus[id].Activated = 1;
}

function fHideMenuIfNotActivated()
{
	if (objCurrentMenu)
		if (!objCurrentMenu.Activated)
			objCurrentMenu.HideMenu();
}

function fShortDelayOnMouseOut()
{
	fClearTimeout();
	intLastTimeout = setTimeout('fHideMenuIfNotActivated()', 100);
}

function fClearTimeout()
{
	if (intLastTimeout != null)
	{
		clearTimeout(intLastTimeout);
		intLastTimeout = null;
	}
}

function eClick(e)
{
	if (e) evt = e;
	if (window.event) evt = event;

	if (objCurrentMenu)
		fHideCurrentMenu();
}

function eMouseMove(e)
{
	if (e) evt = e;
	if (window.event) evt = event;

	if (ie4 == 1 || ns6 == 1) {
		window.MouseMoveX = evt.clientX;
		window.MouseMoveY = evt.clientY;
		
		// get vertical scroll offset
		if (ie4 == 1)
			yScroll = document.body.scrollTop;
		else if (ns6 == 1)
			yScroll = window.pageYOffset;

		window.MouseMoveY = window.MouseMoveY + yScroll;
	} else if (ns4 == 1) {/*
		window.MouseMoveX = evt.pageX;
		window.MouseMoveY = evt.pageY;*/
	}

	if (objCurrentMenu && objCurrentMenu.Activated) {
		mouseX = window.MouseMoveX;
		mouseY = window.MouseMoveY;
		
		if(mouseX < (objCurrentMenu.topX) || mouseX > (objCurrentMenu.botX) || mouseY < (objCurrentMenu.topY) || mouseY > (objCurrentMenu.botY))
			objCurrentMenu.HideMenu();
	}
}

if (ie4 == 1 || ns6 == 1)
	document.onclick = eClick;

if (ie4 == 1 || ns6 == 1)
	document.onmousemove=eMouseMove;

if (document.captureEvents)
{
	if (Event.CLICK)
	{
		document.captureEvents(Event.CLICK);
		document.onclick = eClick;
	}
	if (Event.MOUSEMOVE)
	{
		document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove = eMouseMove;
	}
}

