var referenciaEstilo;
var capaVisible;
var navegador;
var ocultar = true;
var hePinchado = false;
var nombre = "menu";
var bSonido = true;
var bSonidoTeclas = true;

var isNav4, isNav6, isIE4;
var Resituar = true;      

setBrowser();

//window.onresize = resetMenus;

function setBrowser()
{
    if (navigator.appVersion.charAt(0) == "4")
    {
        if (navigator.appName.indexOf("Explorer") >= 0)
        {
            isIE4 = true;
        }
        else
        {
            isNav4 = true;
        }
    }
    else if (navigator.appVersion.charAt(0) > "4")
    {
        isNav6 = true;
    }
} 

if (navigator.appName == "Netscape") {
       if (navigator.appCodeName == "Mozilla") {
	referenciaEstilo = "style.";
	capaVisible = "visible";
	navegador="Explorer";
      } else {
	referenciaEstilo = "";
	capaVisible="show";
	navegador= "Netscape";
      }
} else {
	referenciaEstilo = "style.";
	capaVisible = "visible";
	navegador="Explorer";
}

function Left(s, n){
	// Devuelve los n primeros caracteres de la cadena
	if(n>s.length)
		n=s.length;
		
	return s.substring(0, n);
}

function ocultarTodo() {   
   if (ocultar && !hePinchado)
      for (i=0; ele=document.getElementsByTagName('div')[i]; i++)
         if (Left(ele.id,getNombre().length) == getNombre())
            ele.style.visibility = 'hidden';
}

function ocultarCapas(nExcepto, nExcepto2) {   
   for (i=0; ele=document.getElementsByTagName('div')[i]; i++)
      if (nExcepto != ele.id)
         if (nExcepto2 != ele.id)
            if (ele.id != getNombre() + '0')
               ele.style.visibility = 'hidden';
}

function ocultarPrincipal (nombreCapa) {
   eval (referenciaCapa(nombreCapa)+referenciaEstilo+'visibility="hidden"');         	
}

function ocultarRetardoTodo(delay){
   setTimeout("ocultarTodo()",delay)
}

function ocultarRetardo(nombreCapa){
   setTimeout("ocultarCapa('" + nombreCapa + "')",2000)
}

function ocultarCapa (nombreCapa) {
   if (document.getElementById(nombreCapa)!=null)
	   eval (referenciaCapa(nombreCapa)+referenciaEstilo+'visibility="hidden"');         	
}

function mostrarCapa (nombreCapa) {
	eval (referenciaCapa(nombreCapa)+referenciaEstilo+'visibility="' + capaVisible + '"');	
}

function referenciaCapa (nombreCapa) {
	if (navegador=="Netscape") 
		return "document.layers['"+nombreCapa+"'].";
	else
		return "document.getElementById('"+nombreCapa+"').";
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function P7_autoLayers() { //v1.4 by PVII
 var g,b,k,f,args=P7_autoLayers.arguments;a=parseInt(args[0]);if(isNaN(a))a=0;
 if(!document.p7setc){p7c=new Array();document.p7setc=true;for(var u=0;u<10;u++){
 p7c[u]=new Array();}}for(k=0;k<p7c[a].length;k++){if((g=MM_findObj(p7c[a][k]))!=null){
 b=(document.layers)?g:g.style;b.visibility="hidden";}}for(k=1;k<args.length;k++){
 if((g=MM_findObj(args[k]))!=null){b=(document.layers)?g:g.style;b.visibility="visible";f=false;
 for(var j=0;j<p7c[a].length;j++){if(args[k]==p7c[a][j]) {f=true;}}
 if(!f){p7c[a][p7c[a].length++]=args[k];}}}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function getAbsoluteLeft(objectId) {
	// Get an object left position from the upper left viewport corner
	// Tested with relative and nested objects
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	// Return left postion
	return oLeft
}

function getAbsoluteTop(objectId) {
	// Get an object top position from the upper left viewport corner
	// Tested with relative and nested objects
	o = document.getElementById(objectId)
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	// Return top position
	return oTop
}

function resetMenus(){
   if (Resituar == false)
      ocultarTodo();
   Resituar = true;
}

function ResituarMenus(x, y, baseX, baseY, desplazamiento) {
   if (Resituar == true) {            
      for (i=0; ele=document.getElementsByTagName('div')[i]; i++) {         
         if (Left(ele.id,getNombre().length) == getNombre()) {
            if (ele.id == getNombre() + '0') {
               moveIdTo( ele.id, x + baseX, getAbsoluteTop(ele.id))
            } else {
               moveIdTo( ele.id, x + baseX + desplazamiento, getAbsoluteTop(ele.id) );
            }
         }
      }
      Resituar = false;      
   }
}

function moveIdTo( id, x, y )
{
    generic_move( id, x, y, false );
}

function moveIdBy( id, x, y)
{
    generic_move( id, x, y, true );
} 

function getIdProperty( id, property )
{
    if (isNav6)
    {
        var styleObject = document.getElementById( id );
        if (styleObject != null)
        {
            styleObject = styleObject.style;
            if (styleObject[property])
            {
                return styleObject[ property ];
            }
        }
        styleObject = getStyleBySelector( "#" + id );
        return (styleObject != null) ?
            styleObject[property] :
            null;
    }
    else if (isNav4)
    {
        return document[id][property];
    }
    else
    {
        return document.all[id].style[property];
    }
}

function setIdProperty( id, property, value )
{
    if (isNav6)
    {
        var styleObject = document.getElementById( id );
        if (styleObject != null)
        {
            styleObject = styleObject.style;
            styleObject[ property ] = value;
        }
        
        /*
		styleObject = getStyleBySelector( "#" + id );
        if (styleObject != null)
        {
            styleObject[property] = value;
        }
		*/
    }
    else if (isNav4)
    {
        document[id][property] = value;
    }
    else if (isIE4)
    {
         document.all[id].style[property] = value;
    }
} 

function generic_move( id, xValue, yValue, additive )
{
    var left = getIdProperty(id, "left");
    var top = getIdProperty(id, "top");
    var leftMatch, topMatch;

    if (isNav4)
    {
        leftMatch = new Array( 0, left, "");
        topMatch = new Array( 0, top, "");
    }
    else if (isNav6 || isIE4 )
    {
        var splitexp = /([-0-9.]+)(\w+)/;
        leftMatch = splitexp.exec( left );
        topMatch = splitexp.exec( top );
        if (leftMatch == null || topMatch == null)
        {
            leftMatch = new Array(0, 0, "px");
            topMatch = new Array(0, 0, "px");
        }
    }
    left = ((additive) ? parseFloat( leftMatch[1] ) : 0) + xValue;
    top = ((additive) ? parseFloat( topMatch[1] ) : 0) + yValue;
    setIdProperty( id, "left", left + leftMatch[2] );
    setIdProperty( id, "top", top + topMatch[2] );
} 

function disableAnchor(obj, disable){
  if(disable){
    var href = obj.getAttribute("href");
    if(href && href != "" && href != null){
       obj.setAttribute('href_bak', href);
    }
    obj.removeAttribute('href');
    obj.style.color="gray";
  }
  else{
    obj.setAttribute('href', obj.attributes['href_bak'].nodeValue);
    obj.style.color="blue";
  }
}

function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}

function resize() {
   var i=0;
   if (document.layers) i=40;
   if (document.images[0]) window.resizeTo(document.images[0].width +30, document.images[0].height+115-i);
   self.focus();
}

function setNombre(sNombre) {
   nombre = sNombre;
}

function getNombre() {
   return nombre;
}

var arAjx = new Array(1);
var arStops = new Array;

function setIdioma(sIdioma,bMessage) {
   idioma = sIdioma;
   document.getElementById('idioma_activo').src = "images/idiomas/" + sIdioma + ".gif";
   ajFetch('setVariables.php?idioma=' + sIdioma, 'processReceivedData', false, 0);

   if (bMessage) {
      document.getElementById('msg_idioma_recarga').style.visibility = 'visible';
      setTimeout("ocultarCapa('msg_idioma_recarga');",7000);
   }
}

function getIdioma() {
   return idioma;
}

function toggleSonido() {
   bSonido = !bSonido;
   setSonido(bSonido);
}

function toggleSonidoTeclas() {
   bSonidoTeclas = !bSonidoTeclas;
   tecleo_ordenador(bSonidoTeclas);
   setSonidoTeclas(bSonidoTeclas);
}

function setSonido(xSonido) {
   bSonido = xSonido;
   if (xSonido) 
      document.getElementById('estado_sonido').src = "images/sound_on.gif";
   else
      document.getElementById('estado_sonido').src = "images/sound_off.gif";
   ajFetch('setVariables.php?sonido=' + xSonido, 'processReceivedData', false, 0);
}

function getSonido() {   
   ret = 0;
   if (bSonido)
      ret = '1';
   else
      ret = '0';
   if (bSonidoTeclas)
      ret += '1';
   else
      ret += '0';
   return ret;
}

function setSonidoTeclas(xSonidoTeclas) {
   bSonidoTeclas = xSonidoTeclas;
   if (xSonidoTeclas) 
      document.getElementById('estado_sonido_teclas').src = "images/key_on.gif";
   else
      document.getElementById('estado_sonido_teclas').src = "images/key_off.gif";
   ajFetch('setVariables.php?sonidoTeclas=' + xSonidoTeclas, 'processReceivedData', false, 0);
}

function getSonidoTeclas() {   
   ret = 0;
   if (bSonidoTeclas)
      ret = 1;
   else
      ret = 0;
   return ret;
}

function findPosX(obj)
  {
    if (obj != null) {
       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)
  {
    if (obj != null) {
       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 posicionHelp() {
   try {
      posX = findPosX(document.getElementById('vdactil_text_type'));
      document.getElementById('help1').style.left = posX - 20 + 'px';
      document.getElementById('help2').style.left = posX - 20 + 'px';
      document.getElementById('help3').style.left = posX - 20 + 'px';
      document.getElementById('help4').style.left = posX - 20 + 'px';
      document.getElementById('help5').style.left = posX - 20 + 'px';
   
      posY = findPosY(document.getElementById('vdactil_cabecera'));
      document.getElementById('help1').style.top = posY + 'px';
      posY = findPosY(document.getElementById('vdactil_data_result'));
      document.getElementById('help2').style.top = posY + 'px';
      posY = findPosY(document.getElementById('vdactil_text_type'));
      document.getElementById('help3').style.top = posY + 'px';
      posY = findPosY(document.getElementById('vdactil_textbox'));
      document.getElementById('help4').style.top = posY + 'px';
      posY = findPosY(document.getElementById('vdactil_wrapper'));
      document.getElementById('help5').style.top = posY + 'px';
   
   } catch(err) {
      //Handle errors here
   }

   posX_users = findPosX(document.getElementById('salas_y_users'))+40;
   posY_users = findPosY(document.getElementById('salas_y_users'))+20;
   if (document.getElementById('menu0') != null) {
      document.getElementById('menu0').style.left = posX_users + 'px';
      document.getElementById('menu0').style.top = posY_users + 'px';
   }
   
   resizePaneles();
}

unaVez = true;
function resizePaneles() {
   try {
      if (unaVez) {
         unaVez = false;
         if (document.body.clientWidth < 850) {
            ancho = Math.min(document.body.clientWidth - 20, document.getElementById('vdactil_cabecera').scrollWidth - 16 );
            document.getElementById('vdactil_text_type').style.width = ancho + "px";
            document.getElementById('vdactil_textbox').style.width = ancho + "px";
            if (document.getElementById('textInput') != null)
               document.getElementById('textInput').style.width = ancho + "px";
         } else {
            document.getElementById('vdactil_text_type').style.width = "";
            document.getElementById('vdactil_textbox').style.width = "";
            if (document.getElementById('textInput') != null)
               document.getElementById('textInput').style.width = "";
         }
      }
      unaVez = true;
   } catch(err) {
      //Handle errors here
   }
}

function posicionIdioma() {
   posX_users = findPosX(document.getElementById('capa_idioma'))-90;
   posY_users = findPosY(document.getElementById('capa_idioma'))+50;
   if (document.getElementById('idioma0') != null) {
      document.getElementById('idioma0').style.left = posX_users + 'px';
      document.getElementById('idioma0').style.top = posY_users + 'px';
   }
   
   posX_users = findPosX(document.getElementById('capa_idioma'))-450;
   posY_users = findPosY(document.getElementById('capa_idioma'))+50;
   if (document.getElementById('msg_idioma_recarga') != null) {
      document.getElementById('msg_idioma_recarga').style.left = posX_users + 'px';
      document.getElementById('msg_idioma_recarga').style.top = posY_users + 'px';
   }
}

function toggleHelp(interruptor) {
   try {
      if (interruptor == "cambia")
         if (document.getElementById('help1').style.visibility == 'visible')
            interruptor = 'hidden';
         else
            interruptor = 'visible';
      document.getElementById('help1').style.visibility = interruptor;
      document.getElementById('help2').style.visibility = interruptor;
      document.getElementById('help3').style.visibility = interruptor;
      document.getElementById('help4').style.visibility = interruptor;
      document.getElementById('help5').style.visibility = interruptor;
      //document.getElementById('textInput').focus();
   } catch(err) {
      //Handle errors here
      setTimeout("window.location='?L=info.help'", 0);      
   }
}
