OpenLayers.Control.DesaParametres = OpenLayers.Class(OpenLayers.Control, {
	title: "Desar els paràmetres del mapa",
	dialegHTML: 'Copia la URL per crear un vincle amb els paràmetres actuals:<br>'+
        '<input id="funcionsAvancadesPermalink" value="" size="33" type="text">',
    type: OpenLayers.Control.TYPE_TOOL,
    base: null,
    
    initialize: function(options) {
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
        this.base = document.location.href;
    },

    setMap: function(map) {
        OpenLayers.Control.prototype.setMap.apply(this, arguments);

        //make sure we have an arg parser attached
        for(var i=0; i< this.map.controls.length; i++) {
            var control = this.map.controls[i];
            if (control.CLASS_NAME == "OpenLayers.Control.ArgParser") {
                
                // If a permalink is added to the map, and an ArgParser already
                // exists, we override the displayProjection to be the one
                // on the permalink. 
                if (control.displayProjection != this.displayProjection) {
                    this.displayProjection = control.displayProjection;
                }    
                
                break;
            }
        }
        if (i == this.map.controls.length) {
            this.map.addControl(new OpenLayers.Control.ArgParser(
                { 'displayProjection': this.displayProjection }));       
        }

    },
    
    activate: function() {
        this.map.events.on({
            'moveend': this.updateLink,
            'changelayer': this.updateLink,
            'changebaselayer': this.updateLink,
            scope: this
        });
        
        OpenLayers.Control.prototype.activate.apply(this);
        this.updateLink();
    },

    deactivate: function() {
        this.map.events.un({
            'moveend': this.updateLink,
            'changelayer': this.updateLink,
            'changebaselayer': this.updateLink,
            scope: this
        });
 
        OpenLayers.Control.prototype.deactivate.apply(this);
    },
    
    updateLink: function() {
        var input = $('funcionsAvancadesPermalink');
        if (input) {
	        input.value = this.getPermalink();
	    }
    },
    
    getPermalink: function() {
        var center = this.map.getCenter();
        
        // Map not initialized yet. Break out of this function.
        if (!center) { 
            return; 
        }

        var params = OpenLayers.Util.getParameters(this.base);
        
        params.zoom = this.map.getZoom(); 
        var lat = center.lat;
        var lon = center.lon;
        
        if (this.displayProjection) {
            var mapPosition = OpenLayers.Projection.transform(
              { x: lon, y: lat }, 
              this.map.getProjectionObject(), 
              this.displayProjection );
            lon = mapPosition.x;  
            lat = mapPosition.y;  
        }       
        params.lat = Math.round(lat*100000)/100000;
        params.lon = Math.round(lon*100000)/100000;
        
        params.layers = '';
        for(var i=0; i< this.map.layers.length; i++) {
            var layer = this.map.layers[i];
            if (layer.isBaseLayer) {
                params.layers += (layer == this.map.baseLayer) ? "B" : "0";
            } else {
                params.layers += (layer.getVisibility()) ? "T" : "F";           
            }
        }
		
        var href = this.base;
        if( href.indexOf('?') != -1 ){
            href = href.substring( 0, href.indexOf('?') );
        }

        href += '?' + OpenLayers.Util.getParameterString(params) + "&titol=prova";
        return href;
    },

	CLASS_NAME: "OpenLayers.Control.DesaParametres"
});
