/**
 * Oggetto mappa
 *
 * @class JGMap
 * @input targetId string Identificativo dell'elemento in cui posizionare la mappa
 * @author lorenzo.caggioni
 */
JGMap = function(targetId) {
    this.init(targetId);
}
$.extend(JGMap.prototype, {
    /**
     * Array gLatLng
     */
    gLatLng: new Array(),
    /**
     * Array gMarker
     */
    gMarker: new Array(),
    /**
     * Array mapOverlay
     */
    mapOverlay: new Array(),
    /**
     * latlngbounds
     */
    latlngbounds: new GLatLngBounds(),
    /**
     * listing
     */
    listing: null,
    /**
     * marker piccolo
     */
    markerPiccolo: new GIcon(G_DEFAULT_ICON),

    /**
     * marker grande
     */
    marker: new GIcon(G_DEFAULT_ICON),

    /**
     * marker di default google
     */
    markerGoogle: new GIcon(G_DEFAULT_ICON),

    /**
     * focusId
     */
    focusId: null,

    /**
     * Genera un oggetto mappa di google posizionandolo nell'oggetto DOM identificato dall'Id targetId
     * @input targetId string identificativo dell'oggetto in cui posizionare la mappa
     * @constructor
     */
    init: function(targetId){
        // verifico che il browser sia compatibile */
        if (!GBrowserIsCompatible()) {
            return;
        }
        this.map = new GMap2(document.getElementById(targetId));
        //zoom della mappa limitato
        G_NORMAL_MAP.getMinimumResolution = function() {
            return 5
        };
        G_HYBRID_MAP.getMinimumResolution = function() {
            return 5
        };
        G_PHYSICAL_MAP.getMinimumResolution = function() {
            return 5
        };

        // customizzazione della mappa
        this.customUI = this.map.getDefaultUI();
        this.customUI.controls.scalecontrol = false;
        this.customUI.maptypes.satellite = false;
        this.map.setUI(this.customUI);

        // imposto la vistualizzazione di default
        this.map.setMapType(G_PHYSICAL_MAP);

        // disabilto scroll mouse
        this.map.disableScrollWheelZoom();

        // customizzazione dei marker
        this.markerPiccolo.image = "/images/marker_small.gif";
        this.markerPiccolo.iconSize = new GSize(16, 15);
        this.markerPiccolo.shadow = "";
        this.markerPiccolo.iconAnchor = new GPoint(7, 14);

        this.marker.image = "/images/marker.gif";
        this.marker.iconSize = new GSize(43, 58);
        this.marker.shadow = "/images/marker_shadow.png";
        this.marker.shadowSize = new GSize(84, 58);
        this.marker.iconAnchor = new GPoint(22, 58);
    },
    /**
     * Metodo per l'aggiunta di un marker.
     * Il marker viene inserito nell'array this.gMarker[id]
     * La posizione viene inserita nell'array this.gLatLng[id]
     *
     * @input id string identificativo dell'agriturismo
     * @input lat float Latitudine
     * @input lat float Longitudine
     * @author lorenzo.caggioni
     */
    addMarker: function(id,lat,lon,markerType) {
        var markerType = (markerType == null) ? this.markerPiccolo : markerType;
        this.gLatLng[id] = new GLatLng(lat,lon);
        this.gMarker[id] = new GMarker(this.gLatLng[id],markerType);
        this.mapOverlay[id] = this.map.addOverlay(this.gMarker[id]);
        // Memorizzo posizione per centrare automaticamente la mappa e lo zoom
        this.latlngbounds.extend(this.gLatLng[id]);
        // Bind dell'evento sul click
        GEvent.bind(this.gMarker[id], "click", this, function(){
            this.focusOnMap(id);
            if(this.listing != null){
                this.listing.focusOnList(id);
            }
        });
        return true;
    },

    /**
     * Metodo per il focus della mappa su di un marker
     *
     * @input gLatLon GLatLng Coordinate su cui fare l focus
     * @input zLevel livello zoom, default = 13
     * @author lorenzo.caggioni
     */
    setView: function(gLatLng, zLevel){
        var zLevel = (zLevel == null) ? 13 : zLevel;
        this.map.setCenter(gLatLng, zLevel);
        return true;
    },

    /**
     * Metodo per il cambio del marker
     *
     * @input id string Identificativo del marker
     * @input markerType GIcon Oggetto GIcon da utilizzare come marker
     * @author lorenzo.caggioni
     */
    changeMarker: function(id, markerType){
        var markerType = (markerType == null) ? this.markerPiccolo : markerType;
        this.map.removeOverlay(this.gMarker[id]);
        this.gMarker[id] = new GMarker(this.gLatLng[id],markerType);
        this.mapOverlay[id] = this.map.addOverlay(this.gMarker[id]);
        // Bind dell'evento sul click
        GEvent.bind(this.gMarker[id], "click", this, function(){
            this.focusOnMap(id);
            if(this.listing != null){
                if(this.listing.currentElementId != id){
                    this.listing.focusOnList(id);
                }
            }
        });
        return true;
    },

    /**
     * Metodo per il focus sulla mappa
     *
     * @input id string Identificativo del marker
     * @author lorenzo.caggioni
     */
    focusOnMap: function(id){
        // ripristino il marker
        if(this.focusId != null){
            this.changeMarker(this.focusId);
        }
        this.changeMarker(id,this.marker);
        if(!this.map.getBounds().containsLatLng(this.gLatLng[id])){
            this.map.panTo(this.gLatLng[id], 1);
        }
        GEvent.bind(this.gMarker[id], "dblclick", this, function(){
        });
        this.focusId = id;
        return true;
    }
});
