
$(function(){
    var map = new GMap2(document.getElementById("map"));
    GEvent.addListener(map, "moveend", reloadMarkers);

    map.setCenter(new GLatLng(41.893554, 12.392578), 6);
    map.setUIToDefault();
    
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.iconSize = new GSize(26, 40);
    baseIcon.shadow = null;
    baseIcon.shadow = "/img/videomap_ombra.png";
    baseIcon.shadowSize = new GSize(49, 40);
    baseIcon.iconAnchor = new GPoint(13, 39);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    
    var pointIcon = new GIcon(G_DEFAULT_ICON);
    pointIcon.iconSize = new GSize(15, 15);
    pointIcon.shadow = null;
    pointIcon.iconAnchor = new GPoint(7, 7);
    pointIcon.infoWindowAnchor = new GPoint(7, 7);
    
    var daWeb = new GIcon(baseIcon);
    daWeb.image = "/img/agenzia-red.png";
    
    var notDaWeb = new GIcon(pointIcon);
    notDaWeb.image = "/img/agenzia-grey-pt.png";
    
    //lista dei punti visualizzati in mappa
    var ptList = [];
    
    function daWebOrder(marker){
        return GOverlay.getZIndex(marker.getPoint().lat())+1000000;
    }
    
    function reloadMarkers(){
        var bounds = map.getBounds();
        
        //chiamata richesta punti
        var params = {
            swlat:bounds.getSouthWest().lat(),
            swlng:bounds.getSouthWest().lng(),
            nelat:bounds.getNorthEast().lat(),
            nelng:bounds.getNorthEast().lng(),
            zoom:map.getZoom()
        }
        $.getJSON('/agencies/getagencies', params, function(data, textStatus){
            var newList = [];
            var found;
            var dl = data.length;
            //per ogni punto ritornato dalla chiamata, cerco quelli uguali
            for (var i = 0; i < dl; i++) {
                //cerco il punto corrente nella lista dei punti visualizzati
                found = false;
                for (var pi = 0; pi < ptList.length; pi++) {
                    if (ptList[pi].id == data[i][0]) {
                        //trovato il punto, la sposto nella lista dei nuovi
                        newList.push(ptList[pi]);
                        //lo tolgo dalla lista dei punti correnti
                        ptList.splice(pi, 1);
                        found = true;
                        break;
                    }
                }
                if (found == false) {
                    //punto nuovo, lo aggiungo alla lista dei nuovi
                    newList.push({
                        id: data[i][0],
                        d: data[i]
                    });
                }
            }
            
            //ora la lista dei correnti in realtà è diventata la lista
            //dei punti da cancellare perché non più presenti
            //cancello tutti i punti della lista dei vecchi visualizzati
            for (var i = 0; i < ptList.length; i++) {
                if (ptList[i].p) {
                    map.removeOverlay(ptList[i].p);
                    ptList[i].p = null;
                }
                if (ptList[i].listener) {
                    GEvent.removeListener(ptList[i].listener);
                    ptList[i].listener = null;
                }
            }
            
            //mostro i nuovi punti
            for (var i = newList.length - 1; i > -1; i--) {
                //se mai disegnato, lo renderizzo
                if (!newList[i].p) {
                    var d = newList[i].d;
                    var opts = {title: d[4], icon:((d[3] == 1) ? daWeb : notDaWeb), zIndexProcess:((d[3] == 1) ? daWebOrder : null)};
                    var marker = new GMarker(new GLatLng(d[1], d[2]), opts);
                    marker.value = d[5];
                    newList[i].listener = GEvent.addListener(marker, "click", function(){
                        this.openInfoWindowHtml(this.value);
                    });

                    map.addOverlay(marker);
                    newList[i].p = marker;
                }
            }
            
            //aggiorno la lista dei punti correnti
            ptList = newList;
        });
    };
    
    //elenco regioni
    $('#selectRegioni').change(function(){
        var coords = $('#selectRegioni').val().split('|');
        map.setCenter(new GLatLng(coords[0], coords[1]), 9);
    });
});
