var moviendo=false;
var inix,iniy,divMov;
var ventanasDrag = new Array(); // ventanas a ser manejadas con el drag (todas, incluso las sub ventanas)
var ventanas = new Array(); // ventanas que tapan selects
var _frame="_frame";
var _div="_div";
menu_is_ie = ( /msie/i.test(navigator.userAgent) &&
                !/opera/i.test(navigator.userAgent) );
                
                
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findRelPosX(e)
{
   var posx = 0;
   if (e.pageX || e.pageY)
      posx = e.pageX;
   else if (e.clientX || e.clientY)
      posx = e.clientX + document.body.scrollLeft;
   return posx;
}

function findRelPosY(e)
{
   var posy = 0;
   if (e.pageX || e.pageY)
      posy = e.pageY;
   else if (e.clientX || e.clientY)
      posy = e.clientY + document.body.scrollTop;
   return posy;
}


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

function findPageHeight()
{
   var y;
   var test1 = document.body.scrollHeight;
   var test2 = document.body.offsetHeight
   if (test1 > test2) // all but Explorer Mac
   {
      y = document.body.scrollHeight;
   }
   else // Explorer Mac;
        //would also work in Explorer 6 Strict, Mozilla and Safari
   {
      y = document.body.offsetHeight;
   }
   return y;
}

function findPageWidth()
{
   var y;
   var test1 = document.body.scrollWidth;
   var test2 = document.body.offsetWidth
   if (test1 > test2) // all but Explorer Mac
   {
      y = document.body.scrollWidth;
   }
   else // Explorer Mac;
        //would also work in Explorer 6 Strict, Mozilla and Safari
   {
      y = document.body.offsetWidth;
   }
   return y;
}

function findInnerWindowHeight()
{
   var x,y;
   if (self.innerHeight) // all except Explorer
   {
      x = self.innerWidth;
      y = self.innerHeight;
   }
   else if (document.documentElement && document.documentElement.clientHeight)
      // Explorer 6 Strict Mode
   {
      x = document.documentElement.clientWidth;
      y = document.documentElement.clientHeight;
   }
   else if (document.body) // other Explorers
   {
      x = document.body.clientWidth;
      y = document.body.clientHeight;
   }
   return y;
}

function findInnerWindowWidth()
{
   var x,y;
   if (self.innerHeight) // all except Explorer
   {
      x = self.innerWidth;
      y = self.innerHeight;
   }
   else if (document.documentElement && document.documentElement.clientHeight)
      // Explorer 6 Strict Mode
   {
      x = document.documentElement.clientWidth;
      y = document.documentElement.clientHeight;
   }
   else if (document.body) // other Explorers
   {
      x = document.body.clientWidth;
      y = document.body.clientHeight;
   }
   return x;
}

function findScrollHeight()
{
   var y;
   if (self.pageYOffset) // all except Explorer
   {
      y = self.pageYOffset;
   }
   else if (document.documentElement && document.documentElement.scrollTop)
      // Explorer 6 Strict
   {
      y = document.documentElement.scrollTop;
   }
   else if (document.body) // all other Explorers
   {
      y = document.body.scrollTop;
   }
   return y;
}

function findScrollWidth()
{
   var x;
   if (self.pageYOffset) // all except Explorer
   {
      x = self.pageXOffset;
   }
   else if (document.documentElement && document.documentElement.scrollTop)
      // Explorer 6 Strict
   {
      x = document.documentElement.scrollLeft;
   }
   else if (document.body) // all other Explorers
   {
      x = document.body.scrollLeft;
   }
   return x;
}

function inicioDrag(e,idDivMov){
   document.onmousemove=drag;
   document.onmouseup=finDrag;
   document.onclick=finDrag;
   abrirVentana(idDivMov);
   divMov=document.getElementById(idDivMov);
   var posx = findRelPosX(e);
   var posy = findRelPosY(e);
   inix= posx - findPosX(divMov);
   iniy= posy - findPosY(divMov); //offsetTop;
   moviendo=true;
	 e.cancelBubble = true;
   if (e.stopPropagation) e.stopPropagation();
}
function drag(e){
   if (!e) var e = window.event;
   if(moviendo){
      var posx = findRelPosX(e);
      var posy = findRelPosY(e);
      var divMov_div=document.getElementById(divMov.id+"_div");
      var browserWidth;
      var browserHeight;

      //Calcula el ancho y largo de la pagina
      browserWidth=Math.max(findInnerWindowWidth(), findPageWidth());
      browserHeight=Math.max(findInnerWindowHeight(), findPageHeight());
      	
      if (posx - inix>=0){
      	if (posx - inix + divMov_div.offsetWidth < browserWidth-35)
      		divMov.style.left = (posx - inix) +'px';
      	else
      		divMov.style.left =browserWidth-divMov_div.offsetWidth-25+	'px'
      }else
      	divMov.style.left ='0px'
      	
    	if (posy - iniy>=0){
      	if (posy - iniy + divMov_div.offsetHeight < browserHeight-10)
      		divMov.style.top = (posy - iniy)+'px';
      	else
      		divMov.style.top =browserHeight-divMov_div.offsetHeight-5+	'px'
      }else
      	divMov.style.top ='0px'
	    
   }
	 e.cancelBubble = true;
   //ocultaSelects();
   if (e.stopPropagation) e.stopPropagation();
   return false;
}
function finDrag(e,idDivMov){
   if (!e) var e = window.event;
   document.onmousedown=null;
   document.onmousemove=null;
   document.onmouseup=null;
   document.onclick=null;
   moviendo=false;
	 e.cancelBubble = true;
	 if (e.stopPropagation) e.stopPropagation();
}

function abrirVentana(id){
   iniciaVentanas();
   var ETG = document.getElementById(id);
   var ETG_div = document.getElementById(id+'_div');
   var top = findScrollHeight() + (findInnerWindowHeight() - parseInt(ETG_div.offsetHeight,10)) /2;
   ETG.style.top = top + 'px';
   var left = findScrollWidth() + (findInnerWindowWidth() - parseInt(ETG_div.offsetWidth,10))/2
   ETG.style.left = left + 'px';
   
   document.getElementById(id).style.visibility = 'visible';
   //hay que dejarle el maximo de los z-index+1 para que quede encima
   var maxIndex=0;
   var nVent;
   for(var i=0;i<ventanas.length;i++){
      var zIndex = parseInt(document.getElementById(ventanas[i]).style.zIndex);
      if (zIndex>maxIndex){
         nVent=ventanas[i];
         maxIndex=zIndex;
      }
   }
   document.getElementById(id).style.zIndex=maxIndex+1;  
   
}
function cerrarVentana(id){
   document.getElementById(id).style.visibility='hidden';
}

function agregarVentana(ventana){
   ventanas.push(ventana);
   var EV = document.getElementById(ventana);
   EV.style.left = '0px';
   EV.style.top = '0px';
}
function iniciaVentanas(){
    for (var i=0;i<ventanas.length;i++){
			var div = document.getElementById(ventanas[i]);
			var iFrame= document.getElementById(ventanas[i]+_frame);
			var div_div = document.getElementById(ventanas[i]+_div);
			if (iFrame!=null){
            if (!menu_is_ie)
               iFrame.style.display="none";
            else
            {
               iFrame.style.width=div_div.offsetWidth+"px";
               iFrame.style.height=div_div.offsetHeight+"px";
            }
			}		
		}
}

function keyPressHandler(key) {
   if (!key) {
      key = event;
      key.which = key.keyCode;
   }
   var indexVisible=-1;
   var nVent;
   if (key.which==27) {
      for(var i=0;i<ventanas.length;i++){
         var zIndex = parseInt(document.getElementById(ventanas[i]).style.zIndex);
         if (document.getElementById(ventanas[i]).style.visibility == 'visible' 
               && zIndex > indexVisible){
            nVent=ventanas[i];
            indexVisible=zIndex;
         }
      }
      if (indexVisible>-1){
         cerrarVentana(nVent);
         if (key.stopPropagation) key.stopPropagation();
      }
   }
}
   
function definirVentana(id,ancho,top){
		var div = document.getElementById(id+_div);
		var divFrame = document.getElementById(id+_frame);
		var vent = document.getElementById(id);
		div.style.width= ancho+"px";
		divFrame.style.width= ancho+"px";
		if ( top != 0)
			vent.style.top = top+"px";
		else
			vent.style.top = (document.body.clientHeight - 200)/2 +"px";
		vent.style.left = ((document.body.clientWidth - parseInt(div.style.width,10))/2 )+"px";
	}
   
function setOpacity(/* HTMLElement */node, /* float */opacity){
	//	summary
	//	Sets the opacity of node in a cross-browser way.
	//	float between 0.0 (transparent) and 1.0 (opaque)
        node = document.getElementById(node);
	if(menu_is_ie){
		try {
				node.style.filter = ""; 
		} catch(e) {
		}
		node.style.filter = "Alpha(Opacity="+opacity*100+")";
	}else {
		node.style.opacity = opacity; // ffox 1.0 directly supports "opacity"
		node.style.MozOpacity = opacity;
	}//else if(h.safari){
	//	node.style.opacity = opacity; // 1.3 directly supports "opacity"
	//	node.style.KhtmlOpacity = opacity;
	//}else{
	//	node.style.opacity = opacity;
	//}
}

   
function disableBackground(id){
   var node = document.getElementById(id);
   var inputs = node.getElementsByTagName("input");
   for(var x=0;x<inputs.length;x++){
      inputs[x]._disabled_disable_background = inputs[x].disabled;
      inputs[x].disabled = true;
   }
   var selects = node.getElementsByTagName("select");
   for(var x=0;x<selects.length;x++){
      selects[x]._disabled_disable_background = selects[x].disabled;
      selects[x].disabled = true;
   }
   DisableEnableLinks(id,true);
}
function DisableEnableLinks(id, xHow)
{
   _DisableEnableLinks(id, xHow, "a");
   //_DisableEnableLinks(id, xHow, "span");
}

function _DisableEnableLinks(id, xHow, tagName){
  objLinks = document.getElementById(id).getElementsByTagName(tagName);
  for(i=0;i<objLinks.length;i++){
    objLinks[i].disabled = xHow;
    //link with onclick
    if(objLinks[i].onclick && xHow){  
        objLinks[i].onclick = new Function("return false;" + objLinks[i].onclick.toString().getFuncBody());
    }
    //link without onclick
    else if(xHow){  
      objLinks[i].onclick = "function(){return false;}"
      objLinks[i].oldHref = objLinks[i].href;
      objLinks[i].oldTarget = objLinks[i].target;
      objLinks[i].href = '#';
      objLinks[i].target = '';
    }
    //remove return false with link without onclick
    else if(!xHow && objLinks[i].onclick && objLinks[i].onclick.toString().indexOf("function(){return false;}") != -1){   
      objLinks[i].href = objLinks[i].oldHref;
      if( objLinks[i].oldTarget != null ) objLinks[i].target = objLinks[i].oldTarget;
      objLinks[i].onclick = null;
    }
    //remove return false link with onclick
    else if(!xHow && objLinks[i].onclick && objLinks[i].onclick.toString().indexOf("return false;") != -1){  
      strClick = objLinks[i].onclick.toString().getFuncBody().replace("return false;","")
      objLinks[i].onclick = new Function(strClick);
    }
  }
}

String.prototype.getFuncBody = function(){ 
  var str=this.toString(); 
  str=str.replace(/[^{]+{/,"");
  str=str.substring(0,str.length-1);   
  str = str.replace(/\n/gi,"");
  if(!str.match(/\(.*\)/gi))str += ")";
  return str; 
} 

function enableBackground(id){
   var node = document.getElementById(id);
   var inputs = node.getElementsByTagName("input");
   for(var x=0;x<inputs.length;x++){
      inputs[x].disabled = inputs[x]._disabled_disable_background;
   }
   var selects = node.getElementsByTagName("select");
   for(var x=0;x<selects.length;x++){
      selects[x].disabled = selects[x]._disabled_disable_background;
   }
   DisableEnableLinks(id,false);
}

function _cerrar(id)
{
   setOpacity("_pagina_body", 1.0);
   //enableBackground("_pagina_body");
   cerrarVentana(id);
}

function _abrir(id)
{
   setOpacity("_pagina_body", 0.5);
   //disableBackground("_pagina_body");
   
   abrirVentana(id);
}

						
