/**
 * Vue MVC pour les fonctions de carto. S'enregistre comme observateur auprès du
 * Modèle pour actualiser l'affichage quand les données ont changé.
 *
 * @see ControleurDataCarto
 * @see Observer
 * @package carif_commun
 */
var VueCarto = Class.create(Observer, {

    /**
     * Constructeur : déclaration comme observateur auprès du modèle, création
     * de la carte GMap2.
     *
     * @param modele ModeleCarto
     * @param controleur ControleurCarto
     * @param fond_region Passer ce paramètre si on veut afficher les limites régionales
     */
    initialize: function(modele, controleur, fond_region) {

        this._controleur = controleur;

        // observation du modèle
        this._modele = modele;
        this._modele.attache(this);

        // identifiant du div de la carte dans la page web
        this._nom_div_carte = 'carte';

        // création carte
        this.map = new GMap2($(this._nom_div_carte));

        // centre
        this.map.setCenter(new GLatLng(43.78299262890581, 1.5655517578125), 8);
        this.map.savePosition();

        // options de la carte
        this.map.addControl(new GSmallMapControl()); // boutons zoom
        //this.map.addControl(new GMapTypeControl()); // bouton type (carte ou photo)
        this.map.enableDoubleClickZoom();
        this.map.enableContinuousZoom();

		if (arguments.length == 3) {


			// Clicked lat and lng: 42.565219, -0.340576
			var mapZoom = 8;
			// Map pixel coords for clicked point at this zoom level
			var pointx = 32706;
			var pointy = 24189;
			var scaled = true;
			var imagetype = true;
			var overlaycontent = '/commun/images/carto/limites_region_4.png';
			var width = 693;
			var height = 630;
			var pt = this.map.getCurrentMapType().getProjection();
			var swpoint = new GPoint(pointx, pointy);
			var nepoint = new GPoint(pointx + width, pointy - height);
			var swcoord = pt.fromPixelToLatLng(swpoint,mapZoom);
			var necoord = pt.fromPixelToLatLng(nepoint,mapZoom);
			var geoinfo = new GLatLngBounds(new GLatLng(swcoord.lat(), swcoord.lng()), new GLatLng(necoord.lat(), necoord.lng()));
			// The code for MyCustomOverlay is in mycustomoverlay.js
			this.frontieres_region = new MyCustomOverlay(overlaycontent, geoinfo, imagetype, scaled);
			this.map.addOverlay(this.frontieres_region);


//	        // image en overlay des limites régionales
//	        this.limites_region = new GLatLngBounds(
//	            new GLatLng(42.35, -0.8),
//	            new GLatLng(45.15, 4.23)
//	        );
//	        this.frontieres_region = new GGroundOverlay(
//	            '/commun/images/carto/limites_region_4.png',
//	            this.limites_region
//	        );
//	        this.map.addOverlay(this.frontieres_region);
		}

        // nettoyage de la carte en fin de script
        Event.observe(window, 'unload', GUnload);

    },

    /**
     * Réaffiche l'overlay
     */
    affiche_frontieres: function() {
        this.map.removeOverlay(this.frontieres_region);
        this.limites_region = new GLatLngBounds(
            new GLatLng(parseFloat($F('sw_lat')), parseFloat($F('sw_lng'))),
            new GLatLng(parseFloat($F('ne_lat')), parseFloat($F('ne_lng')))
        );
        this.frontieres_region = new GGroundOverlay(
            '/obs/si/inclus/limites_region_4.png',
            this.limites_region
        );
        this.map.addOverlay(this.frontieres_region);
        return false;
    },

    /**
     * Affiche /  masque les frontières de la région
     */
    bascule_frontieres: function() {
        if (this.frontieres_region.isHidden()) {
            this.frontieres_region.show();
        }
        else {
            this.frontieres_region.hide();
        }
    },

	/**
	 * Remet la carte à sa position et son zoom initial.
	 */
	centrer_carte: function() {
		this.map.returnToSavedPosition();
	},

    /**
     * Implémentation de Observer.Update. Appelé par le Modèle quand ses données
     * ont été modifiées.
     *
     * Doit être implémenté par les classes héritant de celle-ci.
     *
     * @argument {String} Précise quelles données ont changé.
     */
    mise_a_jour: function(contexte) {
        switch (contexte) {
            default:
                break;
        }
    }
});
