//window.onload = inicializarEventos;
baseurl="/actualidad/fotoSrc.php";

function ver_foto_anterior(){
    url = baseurl + "?fg_id=" + getFgid() + "&fg_orden=" + getFotoOrden() + "&accion=anteriorSrc";
    traerAjax(url, ver_foto_anterior_callback);
}

function ver_foto_siguiente(){
    url = baseurl + "?fg_id=" + getFgid() + "&fg_orden=" + getFotoOrden() + "&accion=siguienteSrc";
    traerAjax(url, ver_foto_siguiente_callback);
}

function ver_foto(f){
    url = baseurl + "?fg_id=" + getFgid() + "&fg_orden=" + f + "&accion=src";
    traerAjax(url, ver_foto_callback);
}

function ver_foto_anterior_callback(s){
    setFoto(s);
}
function ver_foto_siguiente_callback(s){
    setFoto(s);
}
function ver_foto_callback(s){
    setFoto(s);
}
function setFoto(s){
    var elems;
    var orden;
    var N;
    var src;
    var total;
    var pie;

    elems = s.split("\n");
    ok = elems[0];
    if (ok != "ok") {
        alert("Error buscando foto:\n"+s);
        return;
    }
    orden = elems[1];
    N = elems[2];
    total = elems[3];
    src = elems[4];
    pie = elems[5];

    setCuenta(N,total);
    setFotoOrden(orden);
    setFotoSrc(src);
    setPie(pie);
    
    if (parseInt(N)<=1) { anteriorOculto(); }
    else { anteriorVisible(); }
    if (parseInt(N)>=parseInt(total)) { siguienteOculto(); }
    else { siguienteVisible(); }
}

var conexion1;
var RTeSOMqxHw_callback;
function traerAjax(url, f_callback) 
{
    conexion1=crearXMLHttpRequest();
    RTeSOMqxHw_callback=f_callback;
    conexion1.onreadystatechange = ajax_callback;
    conexion1.open("GET", url, true);
    conexion1.send(null);
}

function ajax_callback(){
  if(conexion1.readyState == 4)
  {
      RTeSOMqxHw_callback(conexion1.responseText);
  } 
  else 
  {
      // Cargando...
  }
}

function crearXMLHttpRequest() 
{
  var xmlHttp=null;
  if (window.ActiveXObject) 
  {
    try {
	  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
  }
  else 
    if (window.XMLHttpRequest) 
      xmlHttp = new XMLHttpRequest();
  return xmlHttp;
}

var global_fotoOrder;
function getFotoOrden(){
    if (global_fotoOrder === undefined) {
        global_fotoOrder = document.forms["formFoto"].elements['foto_orden'].value;
    }
    return global_fotoOrder;
}
function setFotoOrden(v){
    global_fotoOrder = v;
}
function getFgid(){
    return document.forms["formFoto"].elements['fg_id'].value
}
function setFotoSrc(src){
    document.getElementById("img_foto").src=src;
}
function setCuenta(N, total){
    document.getElementById("li_cuenta").innerHTML =
    '<span>'+N+'</span> de '+total;
}

function anteriorVisible(){
    document.getElementById("li_foto_anterior").style.display="";
}
function anteriorOculto(){
    document.getElementById("li_foto_anterior").style.display="none";
}

function siguienteVisible(){
    document.getElementById("li_foto_siguiente").style.display="";
}
function siguienteOculto(){
    document.getElementById("li_foto_siguiente").style.display="none";
}

function setPie(pie){
    document.getElementById("p_pie").innerHTML=pie;    
}
