Roja.Control.Coordinate = OpenLayers.Class( OpenLayers.Control, {
     /** 
     * @private
     * Property: type 
     * {OpenLayers.Control.TYPES} Controls can have a 'type'. The type
     * determines the type of interactions which are possible with them when
     * they are placed into a toolbar. 
     */
    type: OpenLayers.Control.TYPE_TOGGLE,
    /**
    * @private 
	* Property: id
	* {String}
	*/
    id: null,

    /**
  	* Property: div component
	* {DOMElement}
	*/
    div: null,

    /**
  	* Property: inputText
  	* input text that contains coordinate selected on map
	* {DOMElement}
	*/
    //inputText: null,

    /**
  	* Property: inputTextSettedFunction
  	* function to execute when input test has been setted
	* {DOMElement}
	*/
    //inputTextSettedFunction: null,
    
    /**
    * @private 
	* Property: markersLayer
	* {} Marker layer 
	*/
	markersLayer: null,

    /**
  	* Property: messageDiv component
  	* message div component 
	* {DOMElement}
	*/
    //messageDiv: null,
	
    /**
  	* Property: selectFotogrammaObj
  	* Control to select fotogramma on map
  	* 
	* {Roja.Control.Click}
	*/
	//selectFotogrammaObj: null,

    /**
  	* Property: popupShowerObj
  	* Control to activate popup hidden by other elements
  	* 
	* {Roja.Control.PopupShower}
	*/
	//popupShowerObj: null,
	
    /**
     * @private
	* Property: fallThrough
	* {boolean}
    * Should Address allow events on the map to fall through to other
    * elements on the page, or should it swallow them? (#457)
    * Default is to swallow them.
	*/
    fallThrough: false,

    /**
    * Property: iconUrl
    * {String} The relative path of icon image. This path is added to constant OpenLayers.ImgPath
    */
    iconUrl:  "fa_marker.png",

    /**
    * Property: iconSize
    * {OpenLayers.Size} size icon associated to markers
    */
    iconSize: new OpenLayers.Size(60,38),

    /**
     * Property: restrictedAreaSize
     * {OpenLayers.Size} restricted area to icon associated to markers
     */
    restrictedAreaSize: new OpenLayers.Size(20,40),
    
     /**
    * Property: popupSize
    * {OpenLayers.Size} size associated to popup
    */
    popupSize: new OpenLayers.Size(200,130),

    /**
    * Property: doubleMap
    * {Roja.Control.DoubleMap} double map control
    */
    //doubleMap: null,
    
      /**
     * Constant: defaultHandlerOptions
     * {Object} Hashtable of default Handler settings
     */
    defaultHandlerOptions: {
        'single': true,
        'double': false,
        'pixelTolerance': 0,
        'stopSingle': false,
        'stopDouble': false
    },
    
    /**
    * @private 
	* Property: lonlat
	* {OpenLayers.LonLat}
	*/
    lonlat: null,

	/**
	* Constructor: Roja.Control.Coordinate
	* Constructor for a new <Roja.Control.Coordinate> instance.
	*
	* Parameters:
	* options - {Object} layer that the tile will go in.
	*/
    initialize: function (options) {
    	    	  
       this.handlerOptions = OpenLayers.Util.extend(
            {}, this.defaultHandlerOptions
       );
       OpenLayers.Control.prototype.initialize.apply(this, [options]);		
        
       this.handler = new OpenLayers.Handler.Click(
	        this, {
	            'click': this.onClick
	        }, this.handlerOptions
        );							  
        //use of Util class:A unique id string, built on the passed in prefix
        this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_");	   
        
        //define marker layer
        this.markersLayer = new Roja.Layer.Markers( "Marker" );		
        this.markersLayer.displayInLayerSwitcher=false;
    },
    
    /**
     * Method: draw
     */    
    draw: function() {
	    this.markersLayer = new OpenLayers.Layer.Markers( "Markers" );		
	    this.markersLayer.displayInLayerSwitcher=false;
    	this.map.addLayer(this.markersLayer);
    	this.markersLayer.setZIndex(900);
		return OpenLayers.Control.prototype.draw.apply(this,arguments);
    },
    
    /**
     * Method: markersClear
     * reset markers layer to initial status
     */    
	markersClear: function () {
        if (this.markersLayer.markers != null) {
            while(this.markersLayer.markers.length > 0) {
            	var marker = this.markersLayer.markers[0];
            	this.markersLayer.removeMarker(marker);
            	marker.destroy();
            }
        }
    },
    
    /**
     * Method: onClick
     * event on click of the mouse
    */
    onClick: function(e) {
        var lonlat = this.map.getLonLatFromPixel(e.xy);
        this.addMarker(lonlat);
        calcolaCoordinate(lonlat);
        
  		//this.messageDiv.className=this.messageDiv.className.replace('display','hidden');
        //OpenLayers.Event.stop(e);
    },

    /**
     * Method: addMarker
     * add marker to map
    */
    addMarker: function(lonlat) {
        this.lonlat = lonlat;
       /*
        AutoSizeFramedCloud = OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
            'autoSize': true,
             'minSize': this.popupSize                     
        });                
		var popupClass = AutoSizeFramedCloud;
		
		this.markersClear();
		
		var feature = new OpenLayers.Feature(this.markersLayer, lonlat); 
        feature.data.popupContentHTML = "";
               
        var offset = new OpenLayers.Pixel(-(this.iconSize.w/2), -this.iconSize.h);
	    var icon =  new OpenLayers.Icon(OpenLayers.ImgPath + this.iconUrl,this.iconSize,offset);

        feature.data.icon = icon;			                                                                                
        var marker = feature.createMarker();*/        
/*
        if (this.inputText!=null)
        {
            this.inputText.value = this.roundNumber(lonlat.lon,0) + "," + this.roundNumber(lonlat.lat,0);
            if (this.inputTextSettedFunction!=null)
            {
                this.inputTextSettedFunction();
            }
        }*/
		this.markersClear();
		var options = { lonlat: lonlat,
			    iconUrl: OpenLayers.ImgPath + this.iconUrl,
			    iconSize: this.iconSize,
			    restrictedAreaSize: this.restrictedAreaSize
			  };
		
		marker = new Roja.Marker(options);
		if (this.map != null) {
			this.markersLayer.addMarker(marker);
			var text = '<font style="font-family:Verdana;font-size:9px;color:black;">Coordinate</font><br/><font' +
					' style="font-family:Verdana;font-size:9px;color:black;font-weight:bold;">' + this.roundNumber(lonlat.lon,0) + " , " + this.roundNumber(lonlat.lat,0) +
		   '</font>';
		   
		    var context = {
		    	object:this,
		    	marker:marker
		    }
		    marker.events.register('click', this, this.invokeFixMarker.bindAsEventListener(context));   
		   	marker.createPopup(text,true);
			this.fixMarker(marker);
		}	
		//this.doubleMap.checkCoordinate();
    },    

    /**
     * APIMethod: activate
     * Activate the control and deactivate select fotogramma control.
     * 
     * Returns:
     * {Boolean} Successfully activated the control.
     */
    activate: function() {
    	/*
    	if (this.selectFotogrammaObj!=null)
    	{
        	this.selectFotogrammaObj.deactivate();
    	}
    	if (this.popupShowerObj!=null)
    	{
        	this.popupShowerObj.deactivate();
    	}
    	*/
    	if (this.markersLayer!=null)
    	{
        	this.markersLayer.map.viewPortDiv.style.cursor="crosshair";
    	}
    	//this.panel_div.title="Premere sul pulsante per disattivare l'inserimento punti di registrazione in mappa";

        return (OpenLayers.Control.prototype.activate.apply(this, arguments));
    },
    
    /**
     * APIMethod: deactivate
     * Deactivate the control,activate select fotogramma and clear markers layer.
     * 
     * Returns:
     * {Boolean} Successfully deactivated the control.
     */
    deactivate: function() {
    	this.markersClear();
  /*  	if (this.selectFotogrammaObj!=null)
    	{
        	this.selectFotogrammaObj.activate();
    	}*/
    	if (this.popupShowerObj!=null)
    	{
        	this.popupShowerObj.activate();
    	}
    	if (this.markersLayer!=null)
    	{
        	this.markersLayer.map.viewPortDiv.style.cursor="";
    	}
		//this.doubleMap.checkCoordinate(false);
    	//this.panel_div.title="Premere sul pulsante per attivare l'inserimento punti di registrazione in mappa";
		
        return (OpenLayers.Control.prototype.deactivate.apply(this, arguments));
    },
    
    
    /**
	 * invokeFixMarker invoke the fixMarker function
   */
	invokeFixMarker:function(evt) {
		
		this.object.fixMarker(this.marker);			
    },
  
  
    /**
	 * round number to decimal dec value
    */
    roundNumber:function(num, dec) {
    	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    	return result;
    },    
  
    /**
	 * fixMarker, fix the position of the marker if out from the map
   */
	fixMarker:function(marker) {
	
	 if (marker.popup != null)
     {
       if (marker.popup.visible()==true) {
			if (this.map.getZoom()>0)
		  	{
		    	//This x-y values is used to show div in best place
		    	var extent = this.map.getExtent();
		
				var  mapcenter= this.map.getCenter();
				var  x= mapcenter.lon;
				var  y= mapcenter.lat;
				
				var maxBounds = this.map.getMaxExtent();  
		
				if ((marker.lonlat.lon + marker.popup.size.w/2 * this.map.getResolution()) > extent.right)
				{
					x = x + ((marker.lonlat.lon + marker.popup.size.w/2 * this.map.getResolution()) - extent.right);
					if (x>maxBounds.right) {
						x = maxBounds.right;
					}
				}
				if ((extent.left > marker.lonlat.lon - marker.popup.size.w/2 * this.map.getResolution()))
				{
					x = x - ((extent.left - marker.lonlat.lon + marker.popup.size.w/2 * this.map.getResolution()));
					if (x<maxBounds.left) {
						x = maxBounds.left;
					}
				}
				if ((marker.lonlat.lat + marker.popup.size.h * this.map.getResolution() + marker.icon.size.h * this.map.getResolution()) > extent.top)
				{
					y = y + ((marker.lonlat.lat + marker.popup.size.h * this.map.getResolution() + marker.icon.size.h * this.map.getResolution()) - extent.top);
					if (y>maxBounds.top) {
						y = maxBounds.top;
					}
				}
		
				this.map.setCenter(new OpenLayers.LonLat(x, y), this.map.getZoom());
		  	}
       }
     }
	
},
      
    
     /** @final @type String */
    CLASS_NAME: "Roja.Control.Coordinate"
});





