var isNav = (navigator.appName.indexOf("Netscape") !=-1);
var myWidth = 0;
var myHeight = 0;
var First = true;

var Descripciones = new Array();
var Fotos = new Array();
var Numeros = new Array();
var FotoActual = 0;
var ProyectoActual = 0;


function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();
    try{
      if (sMethod == "GET"){
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }else{
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete){
          bComplete = true;
          fnDone(xmlhttp);
        }
      };
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

function Medidas(){
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  }else if( document.documentElement ){
    if ( document.documentElement.clientWidth || document.documentElement.clientHeight ){
      //IE 6+ in 'standards compliant mode'
      myWidth = document.documentElement.clientWidth;
      myHeight = document.documentElement.clientHeight;
    }else if ( document.body.clientWidth || document.body.clientHeight ){
      //IE 4 compatible
      myWidth = document.body.clientWidth;
      myHeight = document.body.clientHeight;
    }
  }else if( document.body ){
    if( document.body.clientWidth || document.body.clientHeight ){
      //IE 4 compatible
       myWidth = document.body.clientWidth;
       myHeight = document.body.clientHeight;
     }
  }
}

function RecalcularFoto(){
  var Ancho = 0;
  var Alto = 0;
  
  if (Seccion == "Entrada"){
    Ancho = 2000;
    Alto = 1200;
  }else{
    Ancho = 2000;
    Alto = 1200;
  }
  
  var NewH = myHeight - 50;
  var NewW = Math.floor(NewH*Ancho/Alto);
  var Foto = document.getElementById("Foto").style;

  //Foto.position="absolute";
  if (NewW < myWidth){
    NewW = myWidth;
    NewH = Math.floor(NewW * Alto/Ancho);
    
    Foto.height = NewH + "px";
    Foto.width = NewW + "px";  
    Foto.top = Math.floor(myHeight/2 - NewH/2) + "px";
    Foto.left = "0px";
  }else{
    Foto.height = NewH + "px";
    Foto.width = NewW + "px";
    
    Foto.left = Math.floor(myWidth/2 - NewW/2) + "px";
    Foto.top = "0px";
  }
  
}

function Reposicionar(){
  Medidas();
  
  var Contenido = document.getElementById("Contenido").style;
  Contenido.height = Math.floor( myHeight - 50) + "px";
  
  if (First){
    First = false;
    
/*    var L_M = document.getElementById("L_M").style;
    var L_U = document.getElementById("L_U").style;
    var L_C = document.getElementById("L_C").style;
    var L_H = document.getElementById("L_H").style;
    var L_O = document.getElementById("L_O").style;
    
    var Pinicio = Math.floor(myWidth / 2 - (143+86+64+84+83-5-5-5-5)/2);
    L_M.left = Pinicio + "px";
    L_U.left = (Pinicio+143-5) + "px";
    L_C.left = (Pinicio+143+86-5) + "px";
    L_H.left = (Pinicio+143+86+64-5) + "px";
    L_O.left = (Pinicio+143+86+64+84-5) + "px";*/
  }
  
  if (Seccion!="Info" && Seccion!="Motion"){
    RecalcularFoto();
  }
  
  
  Contenido.display = "block";
  Contenido.visibility = "visible";
  
  if (Seccion=="Info"){
    document.getElementById("Contenido").style.width = (myWidth-5-5) + "px";
    document.getElementById("Contenido").style.overflow = "auto";
  }else{
    document.getElementById("Contenido").style.width = "100%";
    document.getElementById("Contenido").style.overflow = "hidden";
  }
  
}

function PosAle(Inf,Sup){
  return Math.floor( Inf + Math.random()*(Sup-Inf+1) );
}

function PosicionLetras(){
    var ajaxConn = new XHConn();
    var URLForm = "NuevaPosicion.asp";
    var Variables = "";
    var Metodo = "POST"; //"GET" //"POST"
  
    Variables = "L_My=" + document.getElementById("L_M").style.top;
    Variables = Variables + "&L_Uy=" + document.getElementById("L_U").style.top;
    Variables = Variables + "&L_Cy=" + document.getElementById("L_C").style.top;
    Variables = Variables + "&L_Hy=" + document.getElementById("L_H").style.top;
    Variables = Variables + "&L_Oy=" + document.getElementById("L_O").style.top;
    Variables = Variables + "&L_Mx=" + document.getElementById("L_M").style.left;
    Variables = Variables + "&L_Ux=" + document.getElementById("L_U").style.left;
    Variables = Variables + "&L_Cx=" + document.getElementById("L_C").style.left;
    Variables = Variables + "&L_Hx=" + document.getElementById("L_H").style.left;
    Variables = Variables + "&L_Ox=" + document.getElementById("L_O").style.left;

    ajaxConn.connect(URLForm,
                    Metodo,
                    Variables,
                    function(XML){
//                      var D = XML.responseText.split("[d]")[1].split("[/d]")[0];
                    });
}

function ReEstructurar(){
/*  window.moveTo(0,0);
  window.resizeTo(screen.availWidth, screen.availHeight);*/
  
  if (document.getElementById("L_M").style.left == "0px" && document.getElementById("L_M").style.top == "0px" && document.getElementById("L_U").style.left == "0px" && document.getElementById("L_U").style.top == "0px" && document.getElementById("L_C").style.left == "0px" && document.getElementById("L_C").style.top == "0px" && document.getElementById("L_H").style.left == "0px" && document.getElementById("L_H").style.top == "0px" && document.getElementById("L_O").style.left == "0px" && document.getElementById("L_O").style.top == "0px"){
    Medidas();
    
	  var L_M = document.getElementById("L_M").style;
    var L_U = document.getElementById("L_U").style;
    var L_C = document.getElementById("L_C").style;
    var L_H = document.getElementById("L_H").style;
    var L_O = document.getElementById("L_O").style;
    
    var Pinicio = 0;
    L_M.left = Pinicio + "px";
    L_U.left = (Pinicio+143-5) + "px";
    L_C.left = (Pinicio+143+86-5) + "px";
    L_H.left = (Pinicio+143+86+64-5) + "px";
    L_O.left = (Pinicio+143+86+64+84-5) + "px";
    
    Pinicio = Math.floor((myHeight - 50) /2 -75);
    L_M.top = Pinicio + "px";
    L_U.top = Pinicio + "px";
    L_C.top = Pinicio + "px";
    L_H.top = Pinicio + "px";
    L_O.top = Pinicio + "px";
    
    
    
/*    document.getElementById("L_M").style.top = PosAle(0,myHeight-125)+"px";
    document.getElementById("L_M").style.left = PosAle(0,myWidth-125)+"px";
    document.getElementById("L_U").style.top = PosAle(0,myHeight-125)+"px";
    document.getElementById("L_U").style.left = PosAle(0,myWidth-125)+"px";
    document.getElementById("L_C").style.top = PosAle(0,myHeight-125)+"px";
    document.getElementById("L_C").style.left = PosAle(0,myWidth-125)+"px";
    document.getElementById("L_H").style.top = PosAle(0,myHeight-125)+"px";
    document.getElementById("L_H").style.left = PosAle(0,myWidth-125)+"px";
    document.getElementById("L_O").style.top = PosAle(0,myHeight-125)+"px";
    document.getElementById("L_O").style.left = PosAle(0,myWidth-125)+"px";*/
    
    PosicionLetras();
  }

  Reposicionar();
}

function Cargar( NuevaSeccion ){
  location.href = '?'+NuevaSeccion;
}




		//Si el navegador del cliente es Mozilla la variable siguiente valdrá true
		var moz = document.getElementById && !document.all;
		//Flag que indica si estamos o no en proceso de arrastrar el ratón
		var estoyArrastrando = false;
		//Variable para almacenar un puntero al objeto que estamos moviendo
		var dobj;
		
		function arrastrarRaton(e){
			if (estoyArrastrando) {
				newLeft = moz ? e.clientX : event.clientX;
				newTop = moz ? e.clientY : event.clientY;
  

    if (dobj.id == "L_M"){
      dobj.style.left = Math.floor(newLeft - 143/2) + "px";
    }else if (dobj.id == "L_U"){
      dobj.style.left = Math.floor(newLeft - 86/2) + "px";
    }else if (dobj.id == "L_C"){
      dobj.style.left = Math.floor(newLeft - 64/2) + "px";
    }else if (dobj.id == "L_H"){
      dobj.style.left = Math.floor(newLeft - 84/2) + "px";
    }else if (dobj.id == "L_O"){
      dobj.style.left = Math.floor(newLeft - 93/2) + "px";
    }

    dobj.style.top = Math.floor(newTop)  + "px";

/*      	dobj.style.left = Math.floor(newLeft - parseInt(dobj.style.width)/2)+"px";
				dobj.style.top = Math.floor(newTop - parseInt(dobj.style.height)/2)+"px";
*/
				return false;
			}
		}


		function soltarBoton(e) {	
			estoyArrastrando = false;
			PosicionLetras();
		}
		
		
		function presionarBoton(e) {
			//Obtenemos el elemento sobre el que se ha presionado el botón del ratón
			var fobj = moz ? e.target : event.srcElement;
			
			// Buscamos el primer elemento en la que esté contenido aquel sobre el que se ha pulsado
			// que pertenezca a la clase objMovible. 
			while (fobj.tagName.toLowerCase() != "html" && fobj.className != "objMovible" && fobj.tagName.toLowerCase()!="a") {
				fobj = moz ? fobj.parentNode : fobj.parentElement;
			}
			
			// Si hemos obtenido un objeto movible...			
			if (fobj.className == "objMovible") {
				// Activamos el flag para indicar que se empieza a arrastrar
				estoyArrastrando = true;
				// Guardamos un puntero al objeto que se está moviendo en la variable global
				dobj = fobj;

				// Devolvemos false para no realizar ninguna acción posterior
				return false;
			}
		}
		
		document.onmousedown = presionarBoton;
		document.onmouseup = soltarBoton;
		document.onmousemove = arrastrarRaton;

		document.oncontextmenu=new Function("return false");
