﻿
function inicializarMapa() {
    Proyectos.GetMarcadoresProyectos(GetMarcadoresProyectos_CallBack);
}

function GetMarcadoresProyectos_CallBack(response) {
    jsonData = eval("(" + response.value + ")");

    var script = document.getElementById("scriptGoogle");
    if (script != null) {
        try {
            GUnload();
            document.getElementsByTagName("head")[0].removeChild(script);
        }
        catch (err) { }
    }
    script = document.createElement("script");
    script.src = "http://www.google.com/jsapi?key=ABQIAAAAMR3SRAIylivW67e9uTtQTxSPvkcGk8nGSiBOYv1MbjUySVzzVRQtmHgQ8746osKgk9t0fPa4HEg95w&callback=loadMaps";
    script.type = "text/javascript";
    script.id = "scriptGoogle";
    document.getElementsByTagName("head")[0].appendChild(script);
}

function loadMaps() {
    var idioma = IdiomaSelec.toLowerCase();
    google.load("maps", "2", { "callback": mapsLoaded, language: idioma });
}

var jsonData;
var map;
//var icono;
function mapsLoaded() {
    //    icono = new GIcon();
    //    icono.image = 'Imagenes/Marcadores/image.png';
    //    icono.shadow = 'Imagenes/Marcadores/shadow.png';
    //    icono.iconSize = new GSize(36, 36);
    //    icono.shadowSize = new GSize(54, 36);
    //    icono.iconAnchor = new GPoint(18, 36);
    //    icono.infoWindowAnchor = new GPoint(18, 0);
    //    icono.printImage = 'Imagenes/Marcadores/printImage.gif';
    //    icono.mozPrintImage = 'Imagenes/Marcadores/mozPrintImage.gif';
    //    icono.printShadow = 'Imagenes/Marcadores/printShadow.gif';
    //    icono.transparent = 'Imagenes/Marcadores/transparent.png';
    //    icono.imageMap = [18, 5, 19, 6, 26, 7, 26, 8, 26, 9, 26, 10, 26, 11, 27, 12, 28, 13, 30, 14, 30, 15, 29, 16, 29, 17, 28, 18, 27, 19, 28, 20, 28, 21, 28, 22, 28, 23, 28, 24, 25, 25, 23, 26, 23, 27, 23, 28, 22, 29, 13, 29, 12, 28, 12, 27, 10, 26, 7, 25, 7, 24, 7, 23, 7, 22, 7, 21, 7, 20, 7, 19, 6, 18, 5, 17, 5, 16, 6, 15, 8, 14, 9, 13, 9, 12, 9, 11, 9, 10, 9, 9, 9, 8, 15, 7, 16, 6, 17, 5];


    var divProyectos = document.getElementById("divDescripcionProyecto");
    divProyectos.style.display = 'none';

    var divFondoProyectos = document.getElementById("divFondoProyectos");
    divFondoProyectos.style.display = 'none';

    var divMapaProyectos = document.getElementById("divMapaProyectos");
    divMapaProyectos.style.display = '';

    var myLatlng = new GLatLng(40, 0);
    map = new GMap2(divMapaProyectos);

    var mt = map.getMapTypes();
    for (var i = 0; i < mt.length; i++) {
        mt[i].getMinimumResolution = function() { return 2; }
        mt[i].getMaximumResolution = function() { return 13; }
    }

    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(myLatlng, 2);
    map.enableDoubleClickZoom();
    map.enableContinuousZoom();
    map.enableScrollWheelZoom();
    
    // Ponemos el mapa en modo Híbrido
    // para que no se muestre la etiqueta en el borde infoerior del mapa
    //map.setMapType(G_HYBRID_MAP);

    var marcadorEspania = crearMarcadorPais(new GLatLng(38.102726, -4.064941));
    var marcadorFrancia = crearMarcadorPais(new GLatLng(44.338838, 3.14209));
    var marcadorItalia = crearMarcadorPais(new GLatLng(40.363927, 12.722168));

    var marcadoresPaises = [];
    marcadoresPaises.push(marcadorEspania);
    marcadoresPaises.push(marcadorFrancia);
    marcadoresPaises.push(marcadorItalia);

    //    var p1 = crearMarcadorProyecto(new GLatLng());
    //    var p2 = crearMarcadorProyecto(new GLatLng());
    //    var p3 = crearMarcadorProyecto(new GLatLng());
    //    var p4 = crearMarcadorProyecto(new GLatLng());
    //    var p5 = crearMarcadorProyecto(new GLatLng());
    //    var p6 = crearMarcadorProyecto(new GLatLng());
    //    var p7 = crearMarcadorProyecto(new GLatLng(38.078077, -3.043213));
    //    var p8 = crearMarcadorProyecto(new GLatLng(38.216316, -3.768311));
    //    var p9 = crearMarcadorProyecto(new GLatLng(39.142559, -1.263428));
    //    var p10 = crearMarcadorProyecto(new GLatLng(39.176633, -2.515869));

    //    var marcadoresProyectos = [];
    //    marcadoresProyectos.push(p1);
    //    marcadoresProyectos.push(p2);
    //    marcadoresProyectos.push(p3);
    //    marcadoresProyectos.push(p4);
    //    marcadoresProyectos.push(p5);
    //    marcadoresProyectos.push(p6);
    //    marcadoresProyectos.push(p7);
    //    marcadoresProyectos.push(p8);
    //    marcadoresProyectos.push(p9);
    //    marcadoresProyectos.push(p10);

    //    var p11 = crearMarcadorProyecto(new GLatLng());
    //    var p12 = crearMarcadorProyecto(new GLatLng(47.76493, 3.306885));
    //    var p13 = crearMarcadorProyecto(new GLatLng(46.946262, 4.053955));
    //    marcadoresProyectos.push(p11);
    //    marcadoresProyectos.push(p12);
    //    marcadoresProyectos.push(p13);

    //    var p14 = crearMarcadorProyecto(new GLatLng());
    //    var p15 = crearMarcadorProyecto(new GLatLng());
    //    var p16 = crearMarcadorProyecto(new GLatLng(41.750551, 14.864502));
    //    var p17 = crearMarcadorProyecto(new GLatLng(40.975475, 14.381104));
    //    marcadoresProyectos.push(p14);
    //    marcadoresProyectos.push(p15);
    //    marcadoresProyectos.push(p16);
    //    marcadoresProyectos.push(p17);

    var marcadoresProyectos = [];
    for (var i = 0; i < jsonData.markers.length; i++) {
        var m = crearMarcadorProyecto(jsonData.markers[i]);
        marcadoresProyectos.push(m);
    }

    var mm = new GMarkerManager(map, { borderPadding: 1 });

    mm.addMarkers(marcadoresPaises, 2, 4);
    mm.addMarkers(marcadoresProyectos, 5, 13);
    mm.refresh();
}


function crearMarcadorPais(latLng) {
    icono = new GIcon();
    icono.shadow = 'Imagenes/Marcadores/shadow.png';
    icono.image = 'Imagenes/Marcadores/image.png';
    icono.iconSize = new GSize(36, 36);
    icono.shadowSize = new GSize(54, 36);
    icono.iconAnchor = new GPoint(18, 36);
    icono.infoWindowAnchor = new GPoint(18, 0);
    var marcador = new GMarker(latLng, icono);
    GEvent.addListener(marcador, "click", function() {
        map.setCenter(latLng);
        zoomIn(latLng);
    });
    return marcador;
}

function zoomIn(latLng) {
    if (map.getZoom() < 5) {
        map.zoomIn();
        window.setTimeout(zoomIn, 5);
    }
}

function crearMarcadorProyecto(proy) {
    icono = new GIcon();
    icono.shadow = 'Imagenes/Marcadores/shadow.png';

    if (proy.tipo == "Suelo") {
        icono.image = 'Imagenes/Marcadores/LogoSuelo.png';
        icono.iconSize = new GSize(36, 22);
        icono.shadowSize = new GSize(54, 36);
        icono.iconAnchor = new GPoint(18, 36);
        icono.infoWindowAnchor = new GPoint(18, 0);
    }
    else {
        icono.image = 'Imagenes/Marcadores/LogoCubiertas.png';
        icono.iconSize = new GSize(36, 36);
        icono.shadowSize = new GSize(54, 36);
        icono.iconAnchor = new GPoint(18, 36);
        icono.infoWindowAnchor = new GPoint(18, 0);
    }


    var latLng = new GLatLng(proy.latitud, proy.longitud);
    var marcador = new GMarker(latLng, icono);
    GEvent.addListener(marcador, "click", function() {
        map.panTo(latLng);
        marcador.openInfoWindowHtml(proy.html);
    });
    return marcador;
}
