//browser detect
var isNS = (navigator.appName == "Netscape");
var isMacIE = ( (navigator.userAgent.indexOf("IE 4")  > -1) && (navigator.userAgent.indexOf("Mac")  > -1) );
var layerRef = (isNS) ? "document" : "document.all";
var styleRef = (isNS) ? "" : ".style";
var isDynamic = ( (document.layers || document.all) && !isMacIE );
//var isDynamic = ( (document.layers || document.all));

var activeMenu = 0;
var finalClip;
var yyPos=0;
var xxPos=0;


  function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }


function aMenu(menuLayerRef, offset, dondex, dondey) {	
        var myImg = document.getElementById("pic1")
        xxPos= findPosX (myImg);
        yyPos= findPosY (myImg);
	scrBars = 20;	
	if (isDynamic && activeMenu != menuLayerRef) {

		if (activeMenu) hideMenu("menu" + activeMenu);
			
		menuID = "menu" + menuLayerRef;
		activeMenu = menuLayerRef;
	
		if (isNS) {		
			document[menuID].top = yyPos-20+offset*16;
			document[menuID].left = xxPos+166;		
		} else {
    
			dif=0;	
			mtop=yyPos+dondey+offset*22-dif
			if (document.all[menuID].offsetHeight+mtop>(document.body.scrollTop + document.body.clientHeight - scrBars)) 
			{corr=document.body.scrollTop + document.body.clientHeight - (document.all[menuID].offsetHeight+mtop)- 2}
			else
			{corr=0}
			document.all[menuID].style.pixelTop = mtop+corr;
			document.all[menuID].style.pixelLeft = xxPos+dondex;
		}

		if (!isNS) {showMenu(menuID)}
		
	}
	if (isDynamic && !isNS) window.event.cancelBubble = true;
}

function keepInWindow() {
	scrBars = 20;

	if (NS4) {
		winRight = (window.pageXOffset + window.innerWidth) - scrBars;
		rightPos = this.left + menuWidth;
   
		if (rightPos > winRight) {
			if (this.hasParent) {
				parentLeft = this.parentMenu.left;
				newLeft = ((parentLeft-menuWidth) + childOverlap);
				this.left = newLeft;
			}
			else {
				dif = rightPos - winRight;
				this.left -= dif;
			}
		}

		winBot = (window.pageYOffset + window.innerHeight) - scrBars;
		botPos = this.top + this.fullHeight;

		if (botPos > winBot) {
			dif = botPos - winBot;
			this.top -= dif;
		}
	}
	else {
    	winRight = (document.body.scrollLeft + document.body.clientWidth) - scrBars;
		rightPos = this.style.pixelLeft + menuWidth;
	
		if (rightPos > winRight) {
			if (this.hasParent) {
				parentLeft = this.parentMenu.style.pixelLeft;
				newLeft = ((parentLeft - menuWidth) + childOverlap);
				this.style.pixelLeft = newLeft;
			}
			else {
				dif = rightPos - winRight;
				this.style.pixelLeft -= dif;
			}
		}

		winBot = (document.body.scrollTop + document.body.clientHeight) - scrBars;
		botPos = this.style.pixelTop + this.fullHeight;

		if (botPos > winBot) {
			dif = botPos - winBot;
			this.style.pixelTop -= dif;
		}
	}
}


function showMenu(layerID) {
	eval(layerRef + '["' + layerID + '"]' + styleRef + '.visibility = "visible"');
}

function hideMenu(layerID) {
	eval(layerRef + '["' + layerID + '"]' + styleRef + '.visibility = "hidden"');
}

function killMenu(e) {
	//check if there is a menu active
	if (activeMenu) {
		menuID = "menu" + activeMenu;
		if (isNS) {
			menuX1 = document[menuID].left;
			menuX2 = menuX1 + document[menuID].clip.right;
			menuY1 = document[menuID].top;
			menuY2 = menuY1 + document[menuID].clip.bottom;
			if (e.pageX > menuX2 || e.pageY > menuY2 || e.pageY < menuY1) {		
			//alert(e.pageY + " -  " + menuY1);
				hideMenu(menuID);
				activeMenu = 0;
			}
		} else {
				hideMenu(menuID);
				activeMenu = 0;

		}
	}  

}

function init() {
	//document.getElementById("sub1").style.display = "block";
	if (isDynamic) {
		if (isNS) { 
			//yyPos=document["map"].pageY;		
			//xxPos=document["map"].pageX;		
			//document.captureEvents(Event.MOUSEMOVE); 
			//document.onmousemove = killMenu;
			setTimeout("window.onresize=reDo",500);
		} else {
	//		yyPos = document.all["map"].offsetTop;
	//		xxPos = document.all["map"].offsetLeft;		
			document.onmouseover = killMenu;
		}
		
	}
}

function reDo(){window.location.reload()}



var persistmenu="yes" //"yes" or "no". Make sure each SPAN content contains an incrementing ID starting at 1 (id="sub1", id="sub2", etc)
var persisttype="sitewide" //enter "sitewide" for menu to persist across site, "local" for this page only

if (document.getElementById){ //DynamicDrive.com change
document.write('<style type="text/css">\n')
document.write('.submenu{display: none;}\n')
document.write('</style>\n')
}

function SwitchMenu(obj){
/*
	if(document.getElementById){

	var el = document.getElementById(obj);
	var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
		if(el.style.display != "block"){ //DynamicDrive.com change
//
			for (var i=0; i<ar.length; i++){
				if (ar[i].className=="submenu") //DynamicDrive.com change
				ar[i].style.display = "none";
			}
			el.style.display = "block";
			//
		}else{
			el.style.display = "none";
		}
	}*/
}

function get_cookie(Name) { 
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { 
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}


function onloadfunction(){

if (persistmenu=="yes"){
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=get_cookie(cookiename)

//	document.getElementById("sub1").style.display = "block";
	


if (cookievalue!=""){
document.getElementById(cookievalue).style.display="block"
}
}
}

function savemenustate(){
var inc=1, blockid=""
while (document.getElementById("sub"+inc)){
if (document.getElementById("sub"+inc).style.display=="block"){
blockid="sub"+inc
break
}
inc++
}
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
document.cookie=cookiename+"="+cookievalue
}

if (window.addEventListener)
window.addEventListener("load", onloadfunction, false)
else if (window.attachEvent)
window.attachEvent("onload", onloadfunction)
else if (document.getElementById)
window.onload=onloadfunction

if (persistmenu=="yes" && document.getElementById)
window.onunload=savemenustate


/******Lo de cambio de imagen **/

function change(image, picture) {
document.images[image].src = picture
}

function change2(image, picture) {
document.images[image].src = picture
}
