//DECLARACION DE VARIABLES GLOBALES
var estadoPhotoViewer = "off";
var imagenParaMostrar = new Image();

//variables para el control de setTimeOut
var referencia_carga = -1;
var referencia_precarga = -1;

//Variables para almacezar las imagenes de la pagina
var lista_de_imagenes = new Object();
var lista_de_imagenes_informacion_adicional = new Object();
var pLista_de_imagenes;
var numero_de_imagenes = 0;

var imgEspera = new Image();
imgEspera.src = "./img/cargando.gif";
//-------------------------------------------------------------------------------------------------------




//DECLARACION DE EVENTOS
imagenParaMostrar.onerror = error_cargar_imagen;
window.onresize = function() {

    if (estadoPhotoViewer == "on"){
        setDivSize(document.getElementById("capa_opacidad_photoViewer"));
        setDivSize(document.getElementById("capa_photoViewer"));
    }

}
//-------------------------------------------------------------------------------------------------------





//EVENTOS
function setDivSize(oDiv)
{

    var myHeight = "830";
    var myWidth = "1024";

    if( typeof(window.innerWidth) == 'number' && typeof(window.innerHeight) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    }
    else if( document.documentElement && document.documentElement.clientWidth && document.documentElement.clientHeight ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    }
    else if( document.body && document.body.clientWidth && document.body.clientHeight ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    
    oDiv.style.width = myWidth + "px";
    oDiv.style.height = myHeight + "px";    
}

function error_cargar_imagen()
{
    alert('An error has occurred while image loading process...\n\rPhotoViewer will be closed.')
    cerrar_photoViewer();
}
//-------------------------------------------------------------------------------------------------------


//FUNCIONES
function rellenar_lista_de_imagenes(id, ruta, titulo_copyright)
{
    numero_de_imagenes++;
    lista_de_imagenes[id.toString()] = ruta;
    lista_de_imagenes_informacion_adicional[id.toString()] = titulo_copyright;
}

function activar_photoViewer(version)
{
    estadoPhotoViewer = "on";
    
    document.getElementById("capa_opacidad_photoViewer").className = "opacidad50 photoViewer_Opacidad_Habilitado";
    document.getElementById("capa_opacidad_photoViewer").style.backgroundColor = "Black";
    setDivSize(document.getElementById("capa_opacidad_photoViewer"));

    document.getElementById("capa_photoViewer").className = "photoViewer_Habilitado";
    setDivSize(document.getElementById("capa_photoViewer"));
    
    cargar_gif_loading();
    
    //Si la version que abrimos es la 2, cada cierto tiempo precargamos el resto de imagenes que se encuentran en la pagina
    //en la que nos encontramos.
    if (version == 2)
    {
        precarga_foto("0");
    }
}

function precarga_foto(id)
{
    try
    {
        var oImage = new Image();
        oImage.src = lista_de_imagenes[id.toString()];
        
        //Cargamos la siguiente imagen si existe
        if (lista_de_imagenes[(parseInt(id)+1).toString()] != null)
        {
            funcionJS = "precarga_foto(" + (parseInt(id)+1).toString() + ")";
            referencia_precarga = setTimeout(funcionJS,1500);
        }
    }
    catch (e)
    {
        //Dejamos de pregargar las imagenes y nos aseguramos que no hay llamadas en espera
        //Si las hubiera, la eliminamos de la cola con la funcion clearTimeout
        if (referencia_precarga != -1)
        {
            clearTimeout(referencia_precarga);
            referencia_precarga = -1;
        }        
    }
}


function photoViewer_v2(id)
{
    try
    {
        if (estadoPhotoViewer == "off") activar_photoViewer(2);
        
        var image = lista_de_imagenes[id.toString()];
        var titulo_copyright = lista_de_imagenes_informacion_adicional[id.toString()];
        
        pLista_de_imagenes = parseInt(id);
        imagenParaMostrar.src = image;
        
        var partesInfo = titulo_copyright.split("|");
        var titulo = (partesInfo[0]!="")?partesInfo[0]:"&nbsp;";
        var copyright = (partesInfo.length > 1 && partesInfo[1] != "")?partesInfo[1]:"";
        
        var funcionJS = "cargar_foto('" + titulo + "','" + copyright + "');"
        referencia_carga = setTimeout(funcionJS,1000); 
    }
    catch(e)
    {
        cerrar_photoViewer();
    }
}


function photoViewer_v1(image, titulo_copyright)
{
    try
    {
        if (estadoPhotoViewer == "off") activar_photoViewer(1);
        
        imagenParaMostrar.src = image;
        
        var partesInfo = titulo_copyright.split("|");
        var titulo = (partesInfo[0]!="")?partesInfo[0]:"&nbsp;";
        var copyright = (partesInfo.length > 1 && partesInfo[1] != "")?partesInfo[1]:"";
        
        var funcionJS = "cargar_foto('" + titulo + "','" + copyright + "');"
        referencia_carga = setTimeout(funcionJS,1000);
        }
    catch(e)
    {
        cerrar_photoViewer();
    }

}


function cargar_gif_loading()
{
    //Cuando esta la barra de proceso, encuadramos el boton y el texto de Loading a la barra de proceso
    //Esto se deshace cuando la imagen se termina de cargar o cuando se cancela la carga.
    document.getElementById("bCerrar_photoViewer").style.paddingRight = "5px";
    document.getElementById("titulo_photoViewer").style.paddingLeft = "5px";
    document.getElementById("titulo_photoViewer").innerHTML = "<b>Loading...</b><br />";
    document.getElementById("copyright_photoViewer").innerHTML = "";
    document.getElementById("image_photoViewer").src = imgEspera.src;
    document.getElementById("image_photoViewer").className = "photoViewer_image_loading";
    
    //Siempre que se cargue/muestre la imagen de "loading..." no aseguramos que los botoes de seguimiento
    //de "NEXT" y "PREVIOUS" estan deshabilitados.
    anular_botones_de_segimiento();    
}

function cerrar_photoViewer()
{
    estadoPhotoViewer = "off";
    cargar_gif_loading();
    document.getElementById("capa_photoViewer").className = "photoViewer_Deshabilitado";
    document.getElementById("capa_opacidad_photoViewer").className = "opacidad50 photoViewer_Opacidad_Deshabilitado";
    ocultar_botones_seguimiento();
    if (referencia_carga != -1){
        clearTimeout(referencia_carga);
        referencia_carga = -1;
    }
    if (referencia_precarga != -1)
    {
        clearTimeout(referencia_precarga);
        referencia_precarga = -1;
    }
}

function activar_botones_de_segimiento()
{
    //Establecemos los eventos necesarios para que aparezcan/oculten los botones de "NEXT" y "PREVIOUS"
    //al posicionar el raton dentro/fuera de la foto que se esta mostrando en ese momento
    document.getElementById("image_photoViewer").onmouseover = mostrar_botones_seguimiento;
    document.getElementById("image_photoViewer").onmouseout = ocultar_botones_seguimiento;
    document.getElementById("botones_seguimiento_photoViewer").onmouseover = mostrar_botones_seguimiento;
    document.getElementById("botones_seguimiento_photoViewer").onmouseout = ocultar_botones_seguimiento;
}

function anular_botones_de_segimiento()
{
    //Quitamos los eventos para que no aparezcan los botones de "NEXT" y "PREVIOUS" y nos aseguramos
    //de que se quedan ocultos
    document.getElementById("image_photoViewer").onmouseover = null;
    document.getElementById("image_photoViewer").onmouseout = null;
    document.getElementById("botones_seguimiento_photoViewer").onmouseover = null;
    document.getElementById("botones_seguimiento_photoViewer").onmouseout = null;
    ocultar_botones_seguimiento();
}

function mostrar_botones_seguimiento()
{
    //Solo mostramos los botones de Next y Previous si se ha cargado una lista de imagenes
    if (numero_de_imagenes > 0)
    {
        document.getElementById("botones_seguimiento_photoViewer").style.visibility = "visible";
    }
}

function ocultar_botones_seguimiento()
{
    document.getElementById("botones_seguimiento_photoViewer").style.visibility = "hidden";
}


//Precarga la imagen en un objeto de tipo Image
function cargar_foto(titulo,copyright)
{
    if (!imagenParaMostrar.complete){
        var funcionJS = "cargar_foto('" + titulo + "','" + copyright + "');"
        referencia_carga = setTimeout(funcionJS,500);
    }
    else
    {
        referencia_carga = -1;
        document.getElementById("image_photoViewer").src = imagenParaMostrar.src;
        mostrar_foto_cargada(titulo,copyright);
    }
}

//carga la imagen en el objeto img de html
function mostrar_foto_cargada(titulo,copyright)
{
    if (!document.getElementById("image_photoViewer").complete){
        var funcionJS = "mostrar_foto_cargada('" + titulo + "','" + copyright + "');"
        referencia_carga = setTimeout(funcionJS,100);
    }
    else{
        referencia_carga = -1;
        document.getElementById("bCerrar_photoViewer").style.paddingRight = "0px";
        document.getElementById("titulo_photoViewer").style.paddingLeft = "0px";
        document.getElementById("titulo_photoViewer").innerHTML = "<b>" + titulo + "</b>";
        document.getElementById("copyright_photoViewer").innerHTML = copyright;
        document.getElementById("image_photoViewer").className = "photoViewer_image";
        activar_botones_de_segimiento();
    }
}

function photoViewerNext()
{
    if (pLista_de_imagenes < numero_de_imagenes)
    {
        var id = String(pLista_de_imagenes+1);
        cargar_gif_loading();
        photoViewer_v2(id.toString());
    }
}

function photoViewerPrevious()
{ 
    if (pLista_de_imagenes > 1)
    {
        var id = String(pLista_de_imagenes-1);
        cargar_gif_loading();
        photoViewer_v2(id.toString());
    }
}
//-------------------------------------------------------------------------------------------------------