OpenLayers.Control.Coordenades = OpenLayers.Class(OpenLayers.Control, {
	title: "Veure coordenades",
	dialegHTML: 'Clica un punt en el mapa per fixar les coordenades:<br>'+
        '<span>XUTM:YUTM</span><br>'+
        '<input id="funcionsAvancadesCoordenadesFixes" size="19" type="text">',
    type: OpenLayers.Control.TYPE_TOOL,

	positionControl: null,
	handler: null,
    tooltipDiv: null,
	
	initialize: function(options) {
		OpenLayers.Control.prototype.initialize.apply(this, arguments);
		
		handlerOptions = {
		    'single': true, 'double': false,
		    'pixelTolerance': 2,
		    'stopSingle': false, 'stopDouble': false,
		    'delay': 50
		};
		this.handler = new OpenLayers.Handler.Click(this,
			{'click': this.fixa}, handlerOptions);

		this.tooltipDiv = document.createElement("div");
		this.tooltipDiv.style.border="1px solid black";
		this.tooltipDiv.style.background="white";
		this.tooltipDiv.style.position="absolute";
		this.tooltipDiv.style.padding="1px";
		this.tooltipDiv.style.display="none";
	},
	
	fixa: function(evt) {
		var lonlat = this.map.getLonLatFromPixel(evt.xy);
		//$('funcionsAvancadesCoordenadesFixes').value=lonlat.lon.toFixed(0) + " : " + lonlat.lat.toFixed(0);
		$('ctl_coord').value=lonlat.lon.toFixed(0) + " : " + lonlat.lat.toFixed(0);
	},
	
	activate: function() {
		OpenLayers.Control.prototype.activate.apply(this);

		positionOptions = {
		    numdigits: 0,
			separator: ',&nbsp;',
			element: this.tooltipDiv
		};
		
		// TODO: Un-append on deactivate
		this.div.appendChild(this.tooltipDiv);
		this.div.style.zIndex = this.map.Z_INDEX_BASE['Popup'] - 1;
		this.positionControl = new OpenLayers.Control.MousePosition(positionOptions);
		this.map.addControl(this.positionControl);
		this.map.events.register('mousemove', this, this.moveTooltip);
		this.map.events.register('mouseout', this, this.hideTooltip);
		this.map.events.register('moveend', this, this.crosshair);
				
		this.handler.activate();
		this.crosshair();
	},

	moveTooltip: function(e) {
		this.tooltipDiv.style.top = e.xy.y + 2;
		this.tooltipDiv.style.left = e.xy.x + 2;
		this.tooltipDiv.style.display="";
	},
	
	hideTooltip: function() {
		this.tooltipDiv.style.display="none";
	},

	crosshair: function() {		
	    //this.map.div.style.cursor = "crosshair";	 
	},
	
	deactivate: function() {
		if(this.positionControl) {
			if(this.positionControl.element) {
				this.positionControl.element.innerHTML = "";
			}
			this.map.removeControl(this.positionControl);
			this.positionControl.destroy();
		}
		this.map.events.unregister('mousemove', this, this.moveTooltip);
		this.map.events.unregister('mouseout', this, this.hideTooltip);
		this.map.events.unregister('moveend', this, this.crosshair);
		try {
			this.div.removeChild(this.tooltipDiv);
		} catch (e) {};
		this.handler.deactivate();
		//this.map.div.style.cursor = "crosshair";	
		OpenLayers.Control.prototype.deactivate.apply(this);
	},
	
	destroy: function() {
		this.handler.destroy();
		OpenLayers.Control.prototype.destroy.apply(this);
	},
	
	CLASS_NAME: "OpenLayers.Control.Coordenades"
});