OpenLayers.Control.DesplegableEscala = OpenLayers.Class(OpenLayers.Control, {
     divDesti: null,
     selectElem: null,
     textPrimerOption: null,

    initialize: function(opcions) {
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
		this.divDesti = opcions.divDesti;
		if( typeof opcions.textPrimerOption!='undefined' ) this.textPrimerOption = opcions.textPrimerOption;
    },


    setMap: function(map) {
        OpenLayers.Control.prototype.setMap.apply(this, arguments);

		this.map.events.on({
            "moveend": this.redraw,
            "zoomend": this.redraw,
            scope: this
        });
    },
	
	redraw: function() {
		//alert( this.map );
		//alert( this.map.getScale() );
		this.selectElem.value = Math.round(this.map.getScale());
	},

	draw: function() {
		OpenLayers.Control.prototype.draw.apply(this);
		this.selectElem = document.createElement("select");
		this.selectElem.id = this.CLASS_NAME;
		this.selectElem.name = this.CLASS_NAME;
		var opcions = '';
		if(this.textPrimerOption != null) opcions += '<option value="">'+this.textPrimerOption+'</option>';
		var resolutions = this.map.baseLayer.resolutions;
		zooms = Array();
		for (var i=0; i<resolutions.length; i++) {
			
			var esc = Math.round( OpenLayers.Util.getScaleFromResolution(resolutions[i], "m"));
			//opcions += '<option value="'+ esc +'">1:'+ esc +'</option>';
			this.selectElem.options[ this.selectElem.length] = new Option('1:'+ esc, esc);
				

		}
		//Collons d'ie
		//Bug amb ie, fem ús del jquery
			
		//alert(opcions);
		//this.selectElem.outerHTML = ""+opcions+""
		//OpenLayers.Util.getElement('nodelist').innerHTML

		//jQuery.noConflict();
		//jQuery(this.selectElem).html(opcions);
		this.divDesti.appendChild(this.selectElem);

		OpenLayers.Event.observe(this.selectElem, "change", this.aplicaEscala);
		OpenLayers.Event.observe(this.selectElem, "keyup", this.aplicaEscala);
		
	},

	 aplicaEscala: function(evt) {
		//Collons d'ie
		var element = evt.target != undefined ? evt.target : evt.srcElement;
		var valor = element.value;
		if(valor!=""){
			//console.debug( valor );
			map.zoomToScale(valor, true);
		}
		//console.debug( "Després Escala=>" + map.getScale() );
		OpenLayers.Event.stop(evt);
	},


    CLASS_NAME: "OpenLayers.Control.DesplegableEscala"
});
