// Funciones javascript dependientes del site
function galeriaSiguiente(){
    if(imagenesGaleria.length==0)
        return;
    if(indiceGaleria==imagenesGaleria.length-1)
        indiceGaleria=0;    
    else
        indiceGaleria++;
    document.getElementById("fotoGaleria").src = imagenesGaleria[indiceGaleria][1];
    document.getElementById("fotoGaleria").alt = imagenesGaleria[indiceGaleria][0]; 
    document.getElementById("fotoGaleria").title = imagenesGaleria[indiceGaleria][0]; 
    document.getElementById("fotoActual").innerHTML = indiceGaleria+1;
}

function galeriaAnterior(){
    if(imagenesGaleria.length==0)
        return;
    if(indiceGaleria==0)
        indiceGaleria=imagenesGaleria.length-1;    
    else
        indiceGaleria--;
    document.getElementById("fotoGaleria").src = imagenesGaleria[indiceGaleria][1];
    document.getElementById("fotoGaleria").alt = imagenesGaleria[indiceGaleria][0]; 
    document.getElementById("fotoGaleria").title = imagenesGaleria[indiceGaleria][0]; 
    document.getElementById("fotoActual").innerHTML = indiceGaleria+1;
}

function galeriaAbrirFoto(){
    if(imagenesGaleria[indiceGaleria][2].length>0){  
        var url = imagenesGaleria[indiceGaleria][2];
        var ancho = imagenesGaleria[indiceGaleria][3]; 
        var alto = imagenesGaleria[indiceGaleria][4];  
        var title = imagenesGaleria[indiceGaleria][0]; 
        mostrarPopupImagen(url,ancho,alto,title); 
    }      
}

function mostrarPopupImagen(url,ancho,alto,title){
    var imagen = document.createElement("img");
    imagen.src = url;
    imagen.title = title;
    imagen.alt = title;
    imagen.style.width = ancho+"px";    
    imagen.style.height = alto+"px";   
    imagen.style.border = "2px inset #900";
    imagen.style.cursor = "pointer";
    imagen.onclick = function(){
        cerrarVentana();
    } 
    abrirVentana(imagen);

}

var map;
var gdir;
var traducciones = new Array();
function load(){
    if (GBrowserIsCompatible()) {
        var posPalacio = new GLatLng(38.836262,-3.942983);
        var direccion;
        direccion  = "<div id='popupMapa'>";
        direccion +=    traducciones['IDIOMA_DIRECCION_PALACIO_MAPS'];
        direccion += "</div>";
        map = new GMap2(document.getElementById("mapa"));
        map.addControl(new GScaleControl());
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl()); 
        map.setCenter(posPalacio, 6); 
        map.addOverlay(new GMarker(posPalacio));
        map.openInfoWindow(map.getCenter(),direccion);              
    }
}

//ejecuta sobre el mapa el como llegar desde un punto a otro
function ejecutarComoLlegar(origen, destino, div_resumen, div_indicaciones){
    function comoLlegarError(){
        if(origen.indexOf("Spain")==-1){ //si no encontramos resultados probamos a hacerlo con Spain detras
            origen = origen+", Spain";
            gdir.load("from: "+origen+" to: "+destino,{ "locale": traducciones['IDIOMA_LOCALE_MAPS']});
        }else{
            //quitamos la imagen de "cargando"
            var resumen = document.getElementById('div_resumen');
            if(resumen)
                resumen.innerHTML = "";
            alert(traducciones['IDIOMA_MAPA_ORIGEN_NO_ENCONTRADO']);    
        }
    }
    
    //ponemos una imagen de "cargando"
    var resumen = document.getElementById(div_resumen);
    if(resumen)
        resumen.innerHTML = "<img src='/img/cargando.gif'>";
    var indicaciones = document.getElementById(div_indicaciones);
    //borramos posibles resultados anteriores
    if(gdir)
        gdir.clear();
    
    var descripcionOrigen = origen;
    var descripcionDestino = "Palacio de la Serna";    
    gdir = new GDirections(map);
    GEvent.addListener(gdir, "load", function(){ 
        comoLlegarRecibido(descripcionOrigen,descripcionDestino,div_resumen,div_indicaciones); 
    });
    GEvent.addListener(gdir, "error", comoLlegarError);
    gdir.load("from: "+origen+" to: "+destino,{ "locale": traducciones['IDIOMA_LOCALE_MAPS'], "getSteps": true});
}

//procesamiento al recibir el como llegar
function comoLlegarRecibido(origen,destino,div_resumen,div_indicaciones){
    //procesamiento al recibir el como llegar    
    var resumen = document.getElementById(div_resumen);
    var indicaciones = document.getElementById(div_indicaciones);
    //rellenamos los datos del resumen de ruta
    if(resumen){
       
        var html;
        html  = '<h2>'+traducciones['IDIOMA_MAPA_RESUMEN_RUTA']+'</h2>';
        html += traducciones['IDIOMA_MAPA_DISTANCIA']+": <strong>" + gdir.getDistance().html.toString() + "</strong><br>";
        html += traducciones['IDIOMA_MAPA_DURACION']+": <strong>" + gdir.getDuration().html.toString() + "</strong>";
        resumen.innerHTML = html;
    }
    
    //rellenamos las indicaciones
    if(indicaciones){
        html  = '<h2>'+traducciones['IDIOMA_MAPA_INDICACIONES_RUTA']+'</h2>';
        html += "<table class='indicaciones'>";
        html +=     "<tr class='extremoRuta'><td colspan='3'>"+traducciones['IDIOMA_MAPA_SALIDA']+": "+origen.toUpperCase()+"</td></tr>";
        var n=1;
        for(var i=0;i<gdir.getNumRoutes();i++){
            var ruta = gdir.getRoute(i);
            for(var j=0;j<ruta.getNumSteps();j++){
                html += '<tr onclick="map.showMapBlowup(gdir.getRoute('+i+').getStep('+j+').getLatLng())" class="fila'+(n%2)+'">';
                html +=     "<td class='num'>";
                html +=         "<a href='javascript:void(0)'>"+n+"</a>";
                html +=     "</td>";
                html +=     "<td>";
                html +=         ruta.getStep(j).getDescriptionHtml();
                html +=     "</td>";
                html +=     "<td>";
                html +=         ruta.getStep(j).getDistance().html;
                html +=     "</td>";
                html += "</tr>";    
                n++;
            }
        }
        html +=        "<tr class='extremoRuta'><td colspan='3'>"+traducciones['IDIOMA_MAPA_LLEGADA']+": "+destino.toUpperCase()+"</td></tr>";
        html +=    "</table>";
        indicaciones.innerHTML = html;
    }
    map.zoomOut();
}

//siguiente paso en el formulario de reservas
function siguientePaso(mensajeAceptarPoliticas){        
    var chkAcepto = document.getElementById('aceptoPoliticas');
    var formu = document.getElementById('formulario');
    
    if(!chkAcepto || chkAcepto.checked)
        formu.submit();
    else
        alert(mensajeAceptarPoliticas);
}

//siguiente paso en el formulario de reservas pero para privacidad
function siguientePasoPrivacidad(mensajeAceptoPrivacidad){        
    var chkAcepto = document.getElementById('aceptoPrivacidad');
    var formu = document.getElementById('formulario');
    
    if(!chkAcepto || chkAcepto.checked)
        formu.submit();
    else
        alert(mensajeAceptoPrivacidad);
        return false;
}

//ver ayuda en la central de reservas
function verAyudaReservas(idAyuda){
    window.open("/reservas/ver-ayuda.php?n="+idAyuda,'','width=600,height=400,scrollbars=yes');
}
// ver textos
function verTextoLegal(idioma){
    window.open('/textos/'+idioma+'-avisolegal.php','','width=640,height=480,scrollbars=yes');
} 
function verTexPrivacidad(idioma){
    window.open('/textos/'+idioma+'-privacidad.php','','width=640,height=480,scrollbars=yes');
} 
function verTextoCondiciones(idioma){
    window.open('/textos/'+idioma+'-condiciones.php','','width=640,height=480,scrollbars=yes');
}
 
function verTextoPrivacidad(idioma){
    window.open('/textos/'+idioma+'-privacidad.php','','width=640,height=480,scrollbars=yes');
}