﻿// NameSpaces
if (!GuiaRepsol) var GuiaRepsol = {};
if (!GuiaRepsol.Alrededores) GuiaRepsol.Alrededores = {};

var kec2_pest_vist = 1;

var MAXUTMX = 5807699;
var MINUTMX = -1968377;
var MAXUTMY = 8385466;
var MINUTMY = 2570830;

var alrededoresJSON = "";


function kec2_clica(n) {

    if (kec2_pest_vist != n) {
        //Definimos nuevos estilos sobre la pestaña anterior

        var a = document.getElementById('kec2_pest_' + kec2_pest_vist);
        a.style.borderBottomColor = "#1D3063";
        a.style.backgroundColor = "#1D3063";
        a = document.getElementById('kec2_pest_' + kec2_pest_vist).getElementsByTagName('A');

        if (a != null) {
            for (var i = 0; i < a.length; i++) {
                a[i].style.color = '#FEEFDC';
                a[i].style.fontWeight = 'normal';
            }
        }


        //Definimos nuevos estilos sobre la pestaña actual
        var a2 = document.getElementById('kec2_pest_' + n);
        a2.style.borderBottomColor = "#FEEFDC";
        a2.style.backgroundColor = "#FEEFDC";
        a2 = document.getElementById('kec2_pest_' + n).getElementsByTagName('A');

        if (a2 != null) {
            for (var i2 = 0; i2 < a2.length; i2++) {
                a2[i2].style.color = '#1D3063';
                a2[i2].style.fontWeight = 'bold';
            }
        }

        //Cambiamos el contenido
        document.getElementById('kec2_cont_' + kec2_pest_vist).style.display = "none";
        document.getElementById('kec2_cont_' + n).style.display = "block";

        kec2_pest_vist = n;
    }
};

function CargarAlrededores() {

    try
    {
        grAjax.asincrono = false;
        grAjax.funcionRetorno = "";
        grAjax.cache = false;

        var x;
        var y;
        var id;
        var tipo;
        var codIdioma;
        var nbObjMapa;
        var idLocalidad;

        kec2_pest_vist = 1;

        if (typeof arguments[0] == "number" && typeof arguments[1] == "number") {
            if (checkCoord(arguments[0], arguments[1])) {
                x = arguments[0];
                y = arguments[1];
                id = arguments[2];
                tipo = arguments[3];
                codIdioma = arguments[4];
                nbObjMapa = arguments[5];
                idLocalidad = arguments[6];
            }
            else {
                return "";
            }
        }
        else {
            if (typeof arguments[0] == "object") {
                if (arguments[0].latitude)
                {
                    var pos;
                    if (MERCATOR)
                        pos = arguments[0].toMercator();
                    else
                        pos = arguments[0].toUTM(30,true);
                    x = pos.x;
                    y = pos.y;
                }
                else
                {
                    x = arguments[0].x;
                    y = arguments[0].y;
                }
                id = arguments[1];
                tipo = arguments[2];
                codIdioma = arguments[3];
                nbObjMapa = arguments[4];
                idLocalidad = arguments[5];
            }
        }

        if (tipo.toString().toUpperCase() == "RUTA") {
            if ($('kec2_cont_2') != undefined) {
                eval(nbObjMapa + ".addIcon(new grMapIcon('Alr555', new positionUTM(" + x + "," + y + "), 'A', grMapImage.tackOrange))");
                var texto = grAjax.getUrl("/Modulos/Alrededores/MostrarResultado/MostrarRuta.aspx?x=" + x + "&y=" + y + "&id=" + id + "&tipo=" + tipo + "&codIdioma=" + codIdioma + "&objMapa=" + nbObjMapa + "&idLocalidad=" + idLocalidad);
                if (texto.indexOf('rutaRestaurantes') != -1) {
                    $('kec2_cont_2').innerHTML = texto;
                    $('kec2_pest_2').style.display = "block";
                    kec2_clica(2);
                }
                else
                    $('kec2_cont_2').innerHTML = "";
                
            }
        }
        else {
            if (tipo.toString().toUpperCase() == "MAPAH") {
                eval(nbObjMapa + ".addIcon(new grMapIcon('Alr555', new positionUTM(" + x + "," + y + "), 'A', grMapImage.tackOrange))");
            }
            var texto = grAjax.getUrl("/Modulos/Alrededores/MostrarResultado/MostrarResultados.aspx?x=" + x + "&y=" + y + "&id=" + id + "&tipo=" + tipo + "&codIdioma=" + codIdioma + "&objMapa=" + nbObjMapa + "&idLocalidad=" + idLocalidad);
            if (texto.indexOf('frmAlrededores')!= -1 )
                return texto;
            else
                return "";
        }
    
    }
    catch(ex) {
        return "";
    }
};

function MarcarAlrededor(nbObjMapa, xPto, yPto, titulo, x, y, radio, globo, tipo) {
    eval(nbObjMapa + ".addIcon(new grMapIcon('Alr655',new positionUTM(" + xPto + "," + yPto + "),'" + titulo + "'," + globo + "))");
    var posMin = new positionUTM(xPto, yPto);
    var posMax = new positionUTM(x, y);
    eval(nbObjMapa + ".boundingBox(new positionUTM(" + xPto + "," + yPto + "),new positionUTM(" + x + "," + y + "), 30, 30, 30, 400)");
    objCounter.addCounterV2(53);
    objCounter.count();
    window.scroll(0, 30);
};

function checkCoord() {
    if (typeof arguments[0] == "number" && typeof arguments[1] == "number") {

        var x = arguments[0];
        var y = arguments[1];

        if (MINUTMX < x && x < MAXUTMX && MINUTMY < y && y < MAXUTMY) {

            return true;
        }
        else if (-90 < x && x < 90 && -180 < y && y < 180) {

            return true;
        }
        return false;
    }
    else {

        return false;
    }
};

function IrAObjAlrededor(url) {
    objCounter.addCounterV2(53);
    objCounter.addCounterV2(54);
    objCounter.count();
    document.location.href = url;
};


GuiaRepsol.Alrededores.ObtenerAlrededoresJSON = function() {
    try
    {
        return GuiaRepsol.Alrededores.ObtenerAlrJSON(arguments);
    }
    catch (ex) {
        return { "Hoteles": null, "Restaurantes": null, "Reportajes": null, "Municipios": null };
    }
};

GuiaRepsol.Alrededores.ObtenerAlrJSON = function(arguments) {

    grAjax.asincrono = false;
    grAjax.funcionRetorno = "";
    grAjax.cache = false;

    var x;
    var y;
    var codIdioma;
    var nbObjMapa;
    var idLocalidad;
    var numResultados;

    if (checkCoord(arguments[0], arguments[1])) {

        if (typeof arguments[0] == "number" && typeof arguments[1] == "number") {
            x = arguments[0];
            y = arguments[1];
            if (GuiaRepsol.Alrededores.isGeographic(x, y)) {
                var pos = new positionGeographic(x, y);
                var posMercator = pos.toMercator();
                x = posMercator.x;
                y = posMercator.y;
            }
            codIdioma = arguments[2];
            nbObjMapa = arguments[3];
            numResultados = arguments.length > 4 ? arguments[4] : "";
            idLocalidad = arguments.length > 5 ? arguments[5] : "";
        }
        else {
            if (typeof arguments[0] == "object") {
                if (arguments[0].utm) {
                    x = arguments[0].x;
                    y = arguments[0].y;
                }
                else {
                    var pos;
                    if (MERCATOR)
                        pos = arguments[0].toMercator();
                    else
                        pos = arguments[0].toUTM(30, true);
                    x = pos.x;
                    y = pos.y;

                }
                codIdioma = arguments[1];
                nbObjMapa = arguments[2];
                numResultados = arguments.length > 3 ? arguments[3] : "";
                idLocalidad = arguments.length > 4 ? arguments[4] : "";

            }
        }
        //Realizo la llamada síncrona
        var objeto = eval("(" + HttpRequest("/Modulos/Alrededores/MostrarResultado/GetAlrededores.aspx?x=" + x + "&y=" + y + "&id=&tipo=&codIdioma=" + codIdioma + "&objMapa=" + nbObjMapa + "&idLocalidad=" + idLocalidad + "&numResultados=" + numResultados + "&rndAjax=" + Math.random(), "", null, "TXT", "GET", false, this, null, true) + ")");
        if (objeto)
            return objeto;
        else
            return "";
    }
    else
        return "";

};

GuiaRepsol.Alrededores.PintarGloboAlrededores = function() {

    var nbObjMapa;
    var x;
    var y;

    if (typeof arguments[0] == "number" && typeof arguments[1] == "number") {
        x = arguments[0];
        y = arguments[1];
        nbObjMapa = arguments[3];
    }
    else {
        if (typeof arguments[0] == "object") {
            if (arguments[0].utm) {
                x = arguments[0].x;
                y = arguments[0].y;
                nbObjMapa = arguments[2];
            }
            else {
                var pos = arguments[0].toUTM(30, true);
                x = pos.x;
                y = pos.y;
                nbObjMapa = arguments[2];
            }
        }
    }
    var objAlrededores = GuiaRepsol.Alrededores.ObtenerAlrJSON(arguments);
    
    var icon = new grMapIcon('AlrGlb55', new positionUTM(x, y), "A", grMapImage.tackOrange, 'Alrededores', "", GuiaRepsol.Alrededores.formatearObjHtml(objAlrededores,nbObjMapa,x,y));
    icon.fixed = true;
    eval(nbObjMapa + ".addIcon(icon)");
    

};

GuiaRepsol.Alrededores.formatearObjHtml = function(objAlrededores, mapa, x, y) {
    var texto = "<table border='1'>";
    texto += "<tr><td>Hoteles<ul>";
    if (objAlrededores.Hoteles != null)
        for (var i = 0; i < objAlrededores.Hoteles.length; i++)
        texto += "<li><a onclick='Javascript:IrAObjAlrededor(\"/Turismo/Hoteles/FichaHoteles.aspx?idHotel=" + objAlrededores.Hoteles[i].IdHotel + "\");' href='#'>" + objAlrededores.Hoteles[i].Nombre + "</a><img style='cursor:pointer;' src='/Modulos/Alrededores/images/ico_13x13_mapar.gif' onclick=\"javascript:MarcarAlrededor('" + mapa + "'," + objAlrededores.Hoteles[i].X + "," + objAlrededores.Hoteles[i].Y + ",\"R" + (i + 1) + "\"," + x + "," + y + ", " + (objAlrededores.Hoteles[i].Distancia * 1000) + ",'grMapImage.tackGreen', 'HOTEL');\"> ";

    texto += "</td></tr><tr><td>Restaurantes<ul>";
    if (objAlrededores.Restaurantes != null)
        for (var i = 0; i < objAlrededores.Restaurantes.length; i++)
        texto += "<li><a onclick='Javascript:IrAObjAlrededor(\"/Gastronomia/Restaurantes/FichaRestaurante.aspx?idRestaurante=" + objAlrededores.Restaurantes[i].IdRestaurante + "\");' href='#'>" + objAlrededores.Restaurantes[i].Nombre;

    texto += "</td></tr><tr><td>Reportajes<ul>";
    if (objAlrededores.Reportajes != null)
        for (var i = 0; i < objAlrededores.Reportajes.length; i++)
        texto += "<li><a onclick='Javascript:IrAObjAlrededor(\"" + objAlrededores.Reportajes[i].URL + "\");' href='#'>" + objAlrededores.Reportajes[i].Nombre;

    texto += "</td></tr></table>";
    return texto;
};

GuiaRepsol.Alrededores.isGeographic = function(x, y) {
    return ((-180 < x && x < 180) && (0< y && y < 90));
};