﻿var isSelectedPano = false;
var pano;
var isSelectedWiki = false;
var wiki;
var idiomaId;

function GenerateMapLatLong(div, latitud, longitud, controls) {
    var map = new GMap2(div);
    for (var i = 0; i < controls.length; i++) {
        map.addControl(controls[i]);
    }

    map.setCenter(new GLatLng(latitud, longitud), 14);
    map.setMapType(G_HYBRID_MAP);

    return map;
}

function GenerateMap(div, destino, controls) {
    var map = new GMap2(div);
    for (var i = 0; i < controls.length; i++) {
        map.addControl(controls[i]);
    }

    map.setMapType(G_HYBRID_MAP);
    MapZoomToLocation(map, destino);
    
    return map;
}

function MapAddListener(map, event, process) {
    GEvent.addListener(map, event, process);
}

function MapAddMarker(point, map, icon) {
    var sUrlFoto = document.location.protocol + "//" + document.location.host + icon;

    var mylabel = { "url": sUrlFoto,
        "anchor": new GPoint(1, 1),
        "size": new GSize(40, 31)
    };

    var Icon = new GIcon(G_DEFAULT_ICON, sUrlFoto, mylabel);
    Icon.iconSize = new GSize(40, 31);
    Icon.iconAnchor = new GPoint(15, 31);
    Icon.infoWindowAnchor = new GPoint(20, 31);

    marker = new GMarker(new GLatLng(point.y, point.x), Icon);
    map.addOverlay(marker);
    
    return marker
}

function MapAddClickeableMarker(point, map, icon, popupContent, size) {
    var sUrlFoto = document.location.protocol + "//" + document.location.host + icon;

    var mylabel = { "url": sUrlFoto,
        "anchor": new GPoint(1, 1),
        "size": size
    };

    var Icon = new GIcon(G_DEFAULT_ICON, 'http://maps.gstatic.com/intl/es_ALL/mapfiles/markerTransparent.png', mylabel);
    Icon.iconSize = new GSize(32, 19);
    Icon.iconAnchor = new GPoint(16, 29); 


    var marker = new GMarker(new GLatLng(point.y, point.x), Icon);
    map.addOverlay(marker);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(popupContent);
        }
    );
}

function MapClearMarkers(map) {
    map.clearOverlays();
}

function MapLatLongValues(overlay, point, Map, oLatH, oLongH) {
    if ((oLongH != undefined) && (oLatH != undefined)) {
        oLongH.val(point.x);
        oLatH.val(point.y);
    }
    MapClearMarkers(Map);
    MapAddMarker(point, Map, "/images/seeAndDoIcon.png");
}

function MapZoomToLocation(map, address) {
    if (address) {
        var geo = new GClientGeocoder();
        geo.getLocations(address, function(result) {

            var N = result.Placemark[0].ExtendedData.LatLonBox.north;
            var S = result.Placemark[0].ExtendedData.LatLonBox.south;
            var E = result.Placemark[0].ExtendedData.LatLonBox.east;
            var W = result.Placemark[0].ExtendedData.LatLonBox.west;
            var bounds = new GLatLngBounds(new GLatLng(S, W), new GLatLng(N, E));
            var zoom = map.getBoundsZoomLevel(bounds);
            map.setCenter(bounds.getCenter(), zoom);
        }
      );
    }
}   

function CustomControls(idioma) {
    idiomaId = idioma;
}

CustomControls.prototype = new GControl();

CustomControls.prototype.initialize = function(map) {
    var container = document.createElement("div");
    container.style.width = "500px";

    var panoDiv = document.createElement("div");
    pano = new GLayer("com.panoramio.all");
    this.setButtonStyle_(panoDiv);
    container.appendChild(panoDiv);
    panoDiv.appendChild(document.createTextNode("Panoramio"));
    panoDiv.style.left = "-200px";
    GEvent.addDomListener(panoDiv, "click", function() {
        if (isSelectedPano) {
            map.removeOverlay(pano);
            panoDiv.style.fontWeight = "normal";
        }
        else {
            map.addOverlay(pano);
            panoDiv.style.fontWeight = "bold";
        }
        isSelectedPano = !isSelectedPano;
    });

    var wikiDiv = document.createElement("div");
    wiki = new GLayer("org.wikipedia." + idiomaId);
    wikiDiv.style.top = "-22px";
    wikiDiv.style.left = "-120px";
    this.setButtonStyle_(wikiDiv);
    container.appendChild(wikiDiv);
    wikiDiv.appendChild(document.createTextNode("Wiki"));
    GEvent.addDomListener(wikiDiv, "click", function() {
        if (isSelectedWiki) {
            map.removeOverlay(wiki);
            wikiDiv.style.fontWeight = "normal";
        }
        else {
            map.addOverlay(wiki);
            wikiDiv.style.fontWeight = "bold";
        }
        isSelectedWiki = !isSelectedWiki;
    });

    map.getContainer().appendChild(container);
    return container;
}

CustomControls.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(300, 7));
}

CustomControls.prototype.setButtonStyle_ = function(button) {
    button.style.float = "left";
    button.style.textDecoration = "none";
    button.style.color = "black";
    button.style.backgroundColor = "white";
    button.style.font = "12px Arial";
    button.style.border = "1px solid black";
    button.style.padding = "1px";
    button.style.marginBottom = "3px";
    button.style.textAlign = "center";
    button.style.width = "6em";
    button.style.cursor = "pointer";
    button.style.marginRight = "0px";
    button.style.position = "relative";
}
