// JavaScript Document
var ajaxCompatible=true;
function GetXMLHttp(){
  var xmlhttp=false;
  if(!checkIE8()){
    //no es ie8 o es una version anterior
    try{
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }catch(e){
        xmlhttp = false;
      }
    }
    // Mozilla then?
    if (!xmlhttp && typeof XMLHttpRequest!='undefined'){
      xmlhttp = new XMLHttpRequest();
    }
  }
  if(!xmlhttp){
    ajaxCompatible=false;
  }
  return xmlhttp;
}

function getInternetExplorerVersion(){
  // Returns the version of Windows Internet Explorer or a -1
  // (indicating the use of another browser).
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer'){
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null) rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
function checkIE8(){
  var msg = "You're not using Windows Internet Explorer.";
  var ver = getInternetExplorerVersion();
  if (ver>-1){
  if ( ver>= 8.0 )
    return true;
  else
    return false;
  }
  return false
}

function goto(pag){
  //alert(pag)
  if(cache.contenidos.length==0){
  	//llamo a la funcion y agrego el contenido
  	addContenido("home",document.getElementById("contenido").innerHTML);
  }
  if(ajaxCompatible){
      if(!cache.trabajando){
  		//si el cache esta vacio, significa que estamos frente al primer pedido
  		//en este momento creo el primer contenido que es el que actualmente
  		//tiene el div contenedor de las pantallas

  		//si se esta consultando la ultima pagina consultada
  		//significa que se hizo click dos veces sobre el mismo boton
  		//asii que en ese caso no hago nada
  		//si la pagina pedida es distinta a la ultima entonces voy a consultar su contenido
  		if(cache.pagina!=pag){
  			//intento recuperar el contenido del cache
  			var result=getContenido(pag);
  			//si no obtuve ningun contenido entonces lo voy a descargar desde su url con ajax
  			if(!result){
  				//document.getElementById("contenido").innerHTML = "";
  				//inicio el objeto XMLHTTP, encargado de consultar la pagina pedida
  				xmlhttp = GetXMLHttp();
          if(!xmlhttp){
            goto(pag);
            return false;
          }
  				//inicio la consulta url
  				xmlhttp.open("GET","contenidos/"+cache.lengua+pag+".xml",true);
  				cache.trabajando=true;
          //asigno la funcion que evalua la respuesta del servidor
  				xmlhttp.onreadystatechange=cargarpagina;
  				//envio la peticion
  				xmlhttp.send(Math.floor(Math.random()*50000));
  			}else{
  				//si obtuve resultado del cache, entonces directamente lo pongo en el div contenedor
  				document.getElementById("contenido").innerHTML=result;
  			}
    	}
    }
  }else{
    location.href="goto.asp?idioma="+cache.lengua+"&pagina="+pag;
  }
}

var cargarpagina=function(){
  //si readyState es 4 significa que la peticion tuvo exito
  if (xmlhttp.readyState == 4){
    //si status es 200 significa que no tuvimos errores en el servidor
    if (xmlhttp.status == 200){
      //si esta todo ok, la respuesta del servidor va a ser un documento xml
      //como no todos los explorador aceptan xPath, lo que hago es recuperar los datos
      //directamente de los nodos que se crean en el documento xml
      try{
        //esto es lo que funciona para IE
        var respuesta=xmlhttp.responseXML.childNodes[1].childNodes[0].nodeValue;
      }catch(e){
        //y esto para los demas exploradores como FF
        var respuesta=xmlhttp.responseXML.childNodes[0].childNodes[1].nodeValue;
      }
      //lleno el div contenedor con el html devuelto en el documento xml
      document.getElementById("contenido").innerHTML=respuesta;
      //y agrego el mismo html al cache para la proxima vez no tener que consultar la pagina
      addContenido(cache.pagina,respuesta)
    }
    cache.trabajando=false;
  }else{
    if(xmlhttp.readyState==1||xmlhttp.readyState==2||xmlhttp.readyState==3){
      document.getElementById("contenido").innerHTML='Aguarde un instante...';
    }else{
      //alert(xmlhttp.readyState);
      cache.trabajando=false;
      ajaxCompatible=false;
      goto(cache.pagina);
    }
  }
}

//creo la cache
var cache = new createCache();
function createCache(){
	this.contenidos = new Array();
	this.pagina = "home";
	this.trabajando = false;
  this.lengua = "es-";
}
//esta es la funcion para agregar contenidos a la cache
function addContenido(key,html){
  key=cache.lengua+key;
  cache.contenidos[cache.contenidos.length]=new newContenido(key,html);
}
function getContenido(key){
	cache.pagina=key;
  key=cache.lengua+key;
  //alert(key)
	for(var i=0;i<cache.contenidos.length;i++){
		if(cache.contenidos[i].key==key){
			return cache.contenidos[i].html;
		}
	}
  //alert("no")
	return false;
}
function enumContenidos(){
  var mensaje="" ;
	for(var i=0;i<cache.contenidos.length;i++){
	  mensaje+=cache.contenidos[i].key;
	}
  alert(mensaje);
}
function newContenido(key,html){
    this.key=key;
    this.html=html;
}
function cambiarLengua(idioma){
	//con esta funcion podemos cambiar el prefijo de idioma
	cache.lengua=idioma;
  var pag=cache.pagina;
  cache.pagina="";
  goto(pag);
}
function getfecha(){
    switch(cache.lengua){
      case "es-":{
        var dayarray=new Array("Domingo","Lunes","Martes","Mi&eacute;rcoles","Jueves","Viernes","S&aacute;bado");
        var montharray=new Array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
        break;
      }
      case "en-":{
        var dayarray=new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
        var montharray=new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
        break;
      }
    }

    var mydate=new Date();
    var year=mydate.getYear();
    var day=mydate.getDay();
    var month=mydate.getMonth();
    var daym=mydate.getDate();

    switch(Right(daym, 1)){
      case "1":{
        var subfijo="st";
        break;
      }
      case "2":{
        var subfijo="nd";
        break;
      }
      case "3":{
        var subfijo="rd";
        break;
      }
      default:{
        var subfijo="th";
        break;
      }
    }

    if(year<1000){year+=1900;}
    if(daym<10){daym="0"+daym;}
    //return '<a class="normal" href="javascript:" onclick="javascript:cambiarLengua(\'en-\');">English</a> - <a class="normal" href="javascript:" style="margin-right:30px;" onclick="javascript:cambiarLengua(\'es-\');">Espa&ntilde;ol</a>' + dayarray[day] + ' ' + daym + ' de ' + montharray[month] + ' de ' + year;
    switch(cache.lengua){
      case "es-":{
        return dayarray[day] + ' ' + daym + ' de ' + montharray[month] + ' de ' + year;
      }
      case "en-":{
        return dayarray[day] + ' ' + montharray[month] + ' ' + daym + subfijo + ', ' + year;
      }
    }

}
function Right(str, n){
  if (n <= 0)
    return "";
  else if (n > String(str).length)
    return str;
  else {
    var iLen = String(str).length;
  return String(str).substring(iLen, iLen - n);
  }
}

/*var mi_ventana;
function abrirIMG(src,ancho,alto){
	if(mi_ventana){mi_ventana.close()};
	 var opciones="left=100,top=400,width="+ancho+",height="+alto+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,",
	 mi_ventana=window.open("imagenes/"+src,"mi_ventana",opciones);
   mi_ventana.moveTo(400,100);
   //mi_ventana.document.write('<html><body><img src="imagenes/'+src+'" width="'+ancho+'" height="'+alto+'"/></body></html>');
}*/
function unloadIMG(){
	var divimg=document.getElementById("divimg");
  var divswf=document.getElementById("divswf");
	divswf.innerHTML="";
	divimg.style.display="none";
  //document.body.style.overflow="auto";
}
function loadIMG(img){
	var divimg=document.getElementById("divimg");
  var divswf=document.getElementById("divswf");
	divimg.style.display="";
	divswf.innerHTML="<div id='obj_flash' style='padding-top:"+ topDetect() +"px;height:"+ 822 +"px;width:100%;'><object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='800' height='822'><param name='movie' value='flash/load_image.swf' /><param name='quality' value='high' /><param name='wmode' value='transparent' /><param name='bgcolor' value='transparent' /><param name='FlashVars' value='img=imagenes/"+img+"'/><embed wmode='transparent' bgcolor='transparent' src='flash/load_image.swf' FlashVars='img=imagenes/"+img+"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='800' height='822'></embed></object></div>";
  //document.body.style.overflowX="hidden";
}
function resize(){
	if(pageWidth()>780){
		document.getElementById("divimg").style.width=pageWidth();
	}else{
		document.getElementById("divimg").style.width=820;
	}
}

function pageWidth() {return window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;}
function pageHeight() {return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;}
function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement && document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;}
function posTop() {return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;}
function posRight() {return posLeft()+pageWidth();}
function posBottom() {return posTop()+pageHeight();}

function topDetect(){
  if(window.addEventListener){
    //document.getElementById("divswf").style.marginTop = window.pageYOffset+"px";
    //return window.pageYOffset;
    //document.getElementById("divswf").style.marginTop = "320px";
    return 320;
  }else if(document.addEventListener){
    //document.getElementById("divswf").style.marginTop = "320px";
    //return window.pageYOffset;
    return 320;
  }else if(document.all && document.compatMode && document.compatMode == "CSS1Compat"){
    //document.getElementById("divswf").style.marginTop = document.documentElement.scrollTop;
    var t=document.documentElement.scrollTop;
    var pHeight=pageHeight();
    if(pHeight>450){
      t=t+(pHeight/2)-(475/2);
    }else{
      t=t+5;
    }
    return t;
  };
}



