OpenLayers.Control.Texte = OpenLayers.Class(OpenLayers.Control, {
	title: "Posar un text al mapa",
	dialegHTML: 'Escriu el text:<br>' +
        '<input id="funcionsAvancadesTextInput" value="Quedem aquĆ­" size="32" type="text"><br>' +
        'i clica el punt en el mapa on vols inserir-hi el text.',
    type: OpenLayers.Control.TYPE_TOOL,
	handler: null,
	etiquetes: [],
	
	initialize: function(options) {
		OpenLayers.Control.prototype.initialize.apply(this, arguments);
		handlerOptions = {
		    'single': true, 'double': false,
		    'pixelTolerance': 2,
		    'stopSingle': false, 'stopDouble': false,
		    'delay': 50, 'cursor':"crosshair"
		};
		this.handler = new OpenLayers.Handler.Click(this,
			{'click': this.insereix}, handlerOptions);
	 
	},
	
	insereix: function(evt) {
		//var text = $('funcionsAvancadesTextInput').value;
		var text = $('text_planol').value;
		if (text.length==0) {
//			alert("Cal que escriguis un text");
			mapa.mostraMissatge(4,true);
			return;
		}
		var lonlat = this.map.getLonLatFromPixel(evt.xy);
		var etiqueta = new OpenLayers.Popup.Etiqueta(lonlat, text);
		etiqueta.setBorder(1);
		etiqueta.events.register("clicked", this, this.esborra);
        this.map.addPopup(etiqueta,false);
        this.etiquetes.push(etiqueta);
        
        
        //this.map.div.style.cursor = "crosshair";
        // afegit per a desactivar la funció de posar una marca
        mapa.seleccionaIcon(2);
        mapa.seleccionaIcon(2);
	},
	
	esborra: function(evt) {
		// Control "Esborra". Crido a l'actualitzaciĆ³ del diĆ leg.
		var controlEsborra = this.map.getControlsByClass("OpenLayers.Control.Esborra")[0];
		if(controlEsborra && controlEsborra.active) {
			var etiqueta = evt.object;
			etiqueta.events.unregister("clicked", this, this.esborra);
			OpenLayers.Util.removeItem(this.etiquetes, etiqueta);
			this.map.removePopup(etiqueta);
			controlEsborra.updateDialeg();
		}
	},
	
	numEtiquetes: function() {
    	return this.etiquetes.length;
    },
	
	activate: function() {
		OpenLayers.Control.prototype.activate.apply(this);
		this.handler.activate();
		//this.map.div.style.cursor = "crosshair";	
	},
	
	
	deactivate: function() {
		this.handler.deactivate();
		OpenLayers.Control.prototype.deactivate.apply(this);
		/*this.map.div.style.cursor = "hand";
		this.map.div.style.cursor = "pointer";*/
	},
	

	CLASS_NAME: "OpenLayers.Control.Texte"
});
