﻿var map;
var iconRed = new GIcon();
var currentSortieID;
iconRed.image = '/Content/img/small_red.png';
iconRed.shadow = '/Content/img/small_red_shadow.png';
iconRed.iconSize = new GSize(12, 20);
iconRed.shadowSize = new GSize(22, 20);
iconRed.iconAnchor = new GPoint(6, 20);
iconRed.infoWindowAnchor = new GPoint(5, 1);

function InitCarte(lng, lat, zoom) {
    window.onunload = GUnload;
    InitGMap(lng, lat, zoom);
}

function CreeMarqueursInitiaux() {
    var bounds = map.getBounds();
    var lngMax = bounds.getNorthEast().lng();
    var latMax = bounds.getNorthEast().lat();
    var lngMin = bounds.getSouthWest().lng();
    var latMin = bounds.getSouthWest().lat();

    $.getJSON("/idee-sortie/listecarte/?lngMax=" + lngMax + "&latMax=" + latMax + "&lngMin=" + lngMin + "&latMin=" + latMin + "&dateDebut=&dateFin=&categorie=0", {},
                function(data) {
                    jQuery.each(data, function(x) {
                        map.addOverlay(createMarker(data[x]));
                    });
                });
}


function CreeMarqueursInitiauxSortiesTaggees(idTag) {

    $.getJSON("/idee-sortie/taggeeslistecarte/?idTag=" + idTag, {},
                function(data) {
                    var resultatVisible = false;
                    jQuery.each(data, function(x) {
                        var marker = createMarker(data[x]);
                        map.addOverlay(marker);
                        if (map.getBounds().containsLatLng(marker.getLatLng())) // Le marqueur est-il visible ?
                        {
                            resultatVisible = true;
                        }
                    });
                    // Aucun des marqueurs n'est visible => on dézoom
                    if (!resultatVisible) {
                        map.setZoom(5);
                    }
                });
}

function ExecSearch() {

    var bounds = map.getBounds();
    var lngMax = bounds.getNorthEast().lng();
    var latMax = bounds.getNorthEast().lat();
    var lngMin = bounds.getSouthWest().lng();
    var latMin = bounds.getSouthWest().lat();

    // log google analytics
    pageTracker._trackPageview('/idee-sortie/carte');

    var dateDeb = $("#dateDebutPicker").val();
    var dateFin = $("#dateFinPicker").val();
    var categ = $("#Categorie").val();
    $.getJSON("/idee-sortie/listecarte/?lngMax=" + lngMax + "&latMax=" + latMax + "&lngMin=" + lngMin + "&latMin=" + latMin + "&dateDebut=" + dateDeb + "&dateFin=" + dateFin + "&categorie=" + categ, {},
                function(data) {
                    map.clearOverlays(); // On vire les marqueurs actuels
                    $("#testdiv").empty();
                    jQuery.each(data, function(x) {
                        $("#testdiv").append(data[x].HTML);
                        map.addOverlay(createMarker(data[x]));
                    });
                });

}

function InitGMap(lng, lat, zoom) {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.addControl(new GSmallMapControl());
        map.setCenter(new GLatLng(lat, lng), zoom);

        GEvent.addListener(map, "dragend", function() {
            ExecSearch();
        });

        GEvent.addListener(map, "zoomend", function() {
            ExecSearch();
        });
    }
}

function createMarker(sortie) {
    var point = new GLatLng(parseFloat(sortie.Latitude), parseFloat(sortie.Longitude));

    var marker = new GMarker(point, iconRed);

    var imgHtml = "<img src='/public_files/" + sortie.ImgUrl + "' id='imgGMap' />";

    var html = "<div id='divGMap'>" + imgHtml + "<div id='nomSortieGMap'>" + sortie.Nom + "</div><div id='descrSortieGMap'>" + sortie.Description + "</div><div id='adresseGMap'>" + sortie.NomLocalisation + "</div><div id='dateGMap'>" + sortie.Date + "</div><div id='lienGMap'><a href='" + sortie.DetailsUrl + "'>Cliquer ici pour plus de détails</a></div></div>";

    // Evenement clic sur le marqueur
    GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
    });

    // Clic sur l'element dans la sidebar
    if (document.getElementById("sortie_id" + sortie.ID) != null) {
        GEvent.addDomListener(document.getElementById("sortie_id" + sortie.ID), 'mouseover', function() {
            if (currentSortieID != sortie.ID) {
                marker.openInfoWindowHtml(html);
            }
            currentSortieID = sortie.ID;
        });
    }
    return marker;
}

function centreCarte(adresse) {
    if (jQuery.trim(adresse) != '') {
        var geocoder = new GClientGeocoder();
        geocoder.getLatLng(adresse,
        function(point) {
            if (!point) {
                alert("Adresse non trouvée");
            } else {
                map.setCenter(point, 10);
                ExecSearch();
            }
        }
      );
    }
    else {
        ExecSearch();
    }
}

