var arrObjMouseOver=new Array(0);
var arrObjPadreMouseOver=new Array(0);
var opcPadreAnterior=null;

var winAncho=0;
var winAncho=0;


//A?ade los menus desplegados antes de aplicar la URL
function menu(strURL) {
	var objMenus = getElementById("menusDesplegados");
	if (objMenus!=null) {
		if (objMenus.value!='') {
			strURL+='?MD='+objMenus.value;
		}
	}
	document.location.href=strURL;
}

//Guarda en un input los menus desplegados
function desplegaMenu(trMENU) {
	var tr = getElementById(trMENU);
	if (tr!=null) {
		//Se muestra u oculta las subopciones y menus
		if (tr.className=='SHOW') tr.className='HIDE';
		else tr.className='SHOW';
		
		//Se guardan los datos sobre los menus desplegados
		//y se eliminan los datos sobre menus no desplegados 
		//en el input menusDeslegados
		var objMenus = getElementById("menusDesplegados");
		var strMenusDesplegados=objMenus.value;
		var strIdMenu=":"+tr.id.substring("trMENU_".length);
		var strEstado=strIdMenu;

		if (tr.className=='HIDE') strEstado="";

		if (strMenusDesplegados=='') {
			if (tr.className=="SHOW") strMenusDesplegados=strEstado;
		} else {
			var posIni = strMenusDesplegados.indexOf(strIdMenu);
			if (posIni!=-1) { 
				var posFin = strMenusDesplegados.substring(posIni).indexOf(";");
				if (posFin==-1) posFin=strMenusDesplegados.length;
				else posFin+=posIni;
				if (tr.className=='HIDE') {
					//Eliminar menu desplegado del texto del input
					strMenusDesplegados=strMenusDesplegados.substring(0,posIni-1)+strMenusDesplegados.substring(posFin);
				} else {
					strMenusDesplegados=strMenusDesplegados.substring(0,posIni-1)+strEstado+";"+strMenusDesplegados.substring(posFin-1);
				}
			}
			if (tr.className=='SHOW') { 
				//A?adir menu desplegado del texto del input
				strMenusDesplegados+=";"+strEstado;
			}
		}
		objMenus.value=strMenusDesplegados;
	}
}


function mouseOverMenuDiv(opc,opcPadre) {
	//Se oculta la flecha del menu actual si ambos son nulos (se ha pulsado fuera del menu)
	if (opc==null && opcPadre==null) {
		getElementById("divMenuActual").style.visibility='hidden';
		getElementById("divMenuActualSombra").style.visibility='hidden';
	}
	//Buscamos si el opcPadre ya existe en el array
	//a partir del elemento que se encuentre igual al padre se eliminan del array y se ocultan
	var blnBorrar=false;
	var intBorrados=0;
	var maxLen=arrObjPadreMouseOver.length;
	for (i=0; i<arrObjPadreMouseOver.length; i++) {
		if (opcPadre==null) {
			blnBorrar=true;
			maxLen=i;
		} else {
			if (opcPadre==arrObjPadreMouseOver[i]) {
				blnBorrar=true;
				maxLen=i;
			}
		}
		if (blnBorrar) {
			var opcAux=arrObjMouseOver[i];
			if (opcAux!=null) {
				opcAux.style.visibility="hidden";
				intBorrados++;
			}
		}
	}
	
	//Copiamos al nuevo array aquelos que no se han ocultado
	var newArr = new Array(maxLen);
	var newArrPadre = new Array(maxLen);
	for (i=0; i<newArr.length; i++) {
		newArr[i]=arrObjMouseOver[i];
		newArrPadre[i]=arrObjPadreMouseOver[i];
	}
	arrObjMouseOver=newArr;
	arrObjPadreMouseOver=newArrPadre;

	//Si no se muestra ning?n submenu se muestran los selects
	//Esto es solo para IE
	if (intBorrados>0) {
		muestraSelects(true);
	}
	
	
	//Si la opc ya existe en el array nos vamos...
	for (i=0; i<newArr.length; i++) {
		if (newArr[i]==opc) {
			return;
		}
	}

	//Se a?ade la opcion
	var newArr2 = new Array(newArr.length+1);
	var newArrPadre2 = new Array(newArrPadre.length+1);
	newArr2[newArr2.length-1]=opc;
	newArrPadre2[newArrPadre2.length-1]=opcPadre;
	for (i=0; i<newArr.length; i++) {
		newArr2[i]=newArr[i];
		newArrPadre2[i]=newArrPadre[i];
	}
	arrObjMouseOver=newArr2;
	arrObjPadreMouseOver=newArrPadre2;
}


function obtenPosXRecursivo(obj) {
	var x = 0;
	x = obj.offsetLeft;
	if (obj.offsetParent!=null) x+=obtenPosXRecursivo(obj.offsetParent);
	return x;
}

function obtenPosYRecursivo(obj) {
	var y = 0;
	y = obj.offsetTop;
	if (obj.offsetParent!=null) y+=obtenPosYRecursivo(obj.offsetParent);
	return y;
}

function getAbsoluteLeft(oNode){
   var oCurrentNode=oNode;
   var iLeft=0;
   while(oCurrentNode!=null && oCurrentNode.tagName!="BODY"){
	  iLeft+=oCurrentNode.offsetLeft;
	  oCurrentNode=oCurrentNode.offsetParent;
   }
   return iLeft;
}

function getAbsoluteTop(oNode){
   var oCurrentNode=oNode;
   var iTop=0;
   while(oCurrentNode!=null && oCurrentNode.tagName!="BODY"){
	  iTop+=oCurrentNode.offsetTop;
	  oCurrentNode=oCurrentNode.offsetParent;
   }
   return iTop;
}

function obtenPosX(opc,blnPrimerMenu) {
  return (blnPrimerMenu?opc.offsetLeft:
						opc.offsetLeft+opc.offsetWidth);
}

function obtenPosY(opc,blnPrimerMenu) {
  return (blnPrimerMenu?opc.offsetTop+opc.offsetHeight-1:
						opc.offsetTop);
}

function menuPrincipalResize() {
	var divMenuPrincipal = getElementById("menuPrincipal");
	var tablaEspaciadoraMenu = getElementById("tablaEspaciadoraMenu");
	tablaEspaciadoraMenu.style.height=divMenuPrincipal.offsetHeight+3;
	divMenuPrincipal.style.top=tablaEspaciadoraMenu.style.top;
}


function reubicaSubMenu(opc,opcPadre) {
	//Reubicaci?n del submenu si no cabe en la pantalla
	if (opc==null || 
		opc.id=="divMenuActual") return;
		
	var d1x1=(blnIE?obtenPosXRecursivo(opc):obtenPosXRecursivo(opc));
	var d1x2=d1x1+opc.offsetWidth;
	var d1y1=(blnIE?obtenPosYRecursivo(opc):obtenPosYRecursivo(opc));
	var d1y2=d1y1+opc.offsetHeight;
		
		/*
		alert("winAncho="+winAncho+
			  "\nwinAlto="+winAlto+
			  "\n\n"+d1x1+"\n"+d1x2+"\n"+d1y1+"\n"+d1y2+
			  "\n\n"+opc.innerHTML.substring(0,500));
		*/

	if (d1x2>winAncho) {
		var leftPadre=getAbsoluteLeft(opcPadre);
		var left=(leftPadre>opc.offsetWidth?
						(blnIE?-opc.offsetWidth:-opc.offsetWidth):
						null);

			/*								
			alert("posiciona en ancho:"+
			"\n opcPadre.style.left="+opcPadre.style.left+
			"\n divPadre.style.left="+divPadre.style.left+
			"\n divPadre.offsetLeft="+divPadre.offsetLeft+
			"\n opcPadre.offsetLeft="+opcPadre.offsetLeft+
			"\n obtenPosX(opcPadre)="+obtenPosX(opcPadre)+
			"\n obtenPosY(opcPadre)="+obtenPosY(opcPadre)+
			"\n left="+left);
			*/
			
			/*
			if ((blnIE &&left<0) ||
				(!blnIE &&left<0)) {
				left=0;
				if (winAncho-opc.offsetWidth>0) {
					left=(winAncho-opc.offsetWidth)/2;
					//alert("posiciona en ancho 2 left="+left);
				}
			}
			*/
			if (left!=null) opc.style.left=left;
		}
	if (d1y2>winAlto && !blnIE) {   
		//No se ajusta en alto para IE porque da problemas con eventos de capas inferiores.
		opc.style.top=-opc.style.height; 
	}
}


function obtenAnchoAltoViewPort() {
	winAncho=window.innerWidth;
	winAlto=window.innerHeight;
	if (document.body) {
		//Ancho
		if (document.body.clientWidth!=null &&
			document.body.clientWidth>0 &&
			(winAncho==0 || winAncho==null)) 
			winAncho=document.body.clientWidth;
		if (document.body.offsetWidth!=null && 
			document.body.offsetWidth>0 &&
			(winAncho==0 || winAncho==null)) 
			winAncho=document.body.offsetWidth;
		//Alto
		if (document.body.clientHeight!=null &&
			document.body.clientHeight>0 &&
			(winAlto==0 || winAlto==null)) 
			winAlto=document.body.clientHeight;
		if (document.body.offsetHeight!=null && 
			document.body.offsetHeight>0 &&
			(winAlto==0 || winAlto==null)) 
			winAlto=document.body.offsetHeight;
	} 
	/*
	if (document.documentElement) {
		if (document.documentElement.offsetWidth!=null && 
			document.documentElement.offsetWidth>0) winAncho=document.documentElement.offsetWidth;
		if (document.documentElement.offsetHeight!=null && 
			document.documentElement.offsetHeight>0) winAlto=document.documentElement.offsetHeight;
	}
	*/
}

				
function muestraSelects() {
	if (!blnIE) return;
	var aSelects=document.getElementsByTagName("select");
	var aSelectsClasses=new Array(aSelects.length);
	var aDivs=document.getElementsByTagName("div");
	var divPrincipal = getElementById("menuPrincipal");
	for (j=0; j<aDivs.length; j++) {
		var div=aDivs[j];
		var objPadre=getElementById(div.id+"_padre");

		if (div.style.visibility=='visible' && 
			div!=divPrincipal && 
			objPadre!=divPrincipal && 
			objPadre!==null) {

			var dx1=getAbsoluteLeft(div);
			var dx2=dx1+div.offsetWidth;
			
			var dy1=getAbsoluteTop(div);
			var dy2=dy1+div.offsetHeight;
			for (i=0; i<aSelects.length; i++) {
				var sx1=getAbsoluteLeft(aSelects[i]);
				var sx2=sx1+aSelects[i].offsetWidth;
				var sy1=getAbsoluteTop(aSelects[i]);
				var sy2=sy1+aSelects[i].offsetHeight;
				var blnHit = interseccionCoordenadas(dx1,dx2,dy1,dy2,
													 sx1,sx2,sy1,sy2);

				if (aSelectsClasses[i]==null || !aSelectsClasses[i])
					aSelectsClasses[i]=blnHit;
			}
		}
	}
	for (i=0; i<aSelectsClasses.length; i++) {
		aSelects[i].style.visibility=(aSelectsClasses[i]?"hidden":"visible");
	}
}


function interseccionCoordenadas(dx1,dx2,dy1,dy2,
								 sx1,sx2,sy1,sy2) {  

	var blnHorizontalHit = interseccionHorizontal(dx1,dx2,dy1,dy2,
												  sx1,sx2,sy1,sy2);						 
							
	var blnVerticalHit = ((dy1>sy1 && dy1<sy2) ||
						  (dy2>sy1 && dy2<sy2) ||
						  (sy1>dy1 && sy1<dy2) ||
						  (sy2>dy1 && sy2<dy2));
						  
	return (blnVerticalHit && blnHorizontalHit);
}

function interseccionHorizontal(dx1,dx2,dy1,dy2,
								sx1,sx2,sy1,sy2) {
	return ((dx1>sx1 && dx1<sx2) ||
			(dx2>sx1 && dx2<sx2) ||
			(sx1>dx1 && sx1<dx2) ||
			(sx2>dx1 && sx2<dx2));
}

