/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
 * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
 * full text of the license. */


/**
 * @requires OpenLayers/Handler.js
 * @requires OpenLayers/Geometry/Point.js
 */

/**
 * Class: OpenLayers.Handler.Point
 * Handler to draw a point on the map.  Point is displayed on mouse down,
 * moves on mouse move, and is finished on mouse up.  The handler triggers
 * callbacks for 'done' and 'cancel'.  Create a new instance with the
 * <OpenLayers.Handler.Point> constructor.
 * 
 * Inherits from:
 *  - <OpenLayers.Handler>
 */
OpenLayers.Handler.Point.Ux = OpenLayers.Class(OpenLayers.Handler.Point, {    
		/** 
	 	* APIProperty: persist 
	 	* {Boolean} Leave the feature rendered until clear is called.  Default 
	 	*     is false.  If set to true, the feature remains rendered until 
	 	*     clear is called, typically by deactivating the handler or starting 
	 	*     another drawing. 
	 	*/ 
	 	persist: false, 
    /**
     * Constructor: OpenLayers.Handler.Point
     * Create a new point handler.
     *
     * Parameters:
     * control - {<OpenLayers.Control>} The control that owns this handler
     * callbacks - {Object} An object with a 'done' property whose value is a
     *             function to be called when the point drawing is finished.
     *             The callback should expect to recieve a single argument,
     *             the point geometry.  If the callbacks object contains a
     *             'cancel' property, this function will be called when the
     *             handler is deactivated while drawing.  The cancel should
     *             expect to receive a geometry.
     * options - {Object} An optional object with properties to be set on the
     *           handler
     */
    initialize: function(control, callbacks, options) {
        // TBD: deal with style
        this.style = OpenLayers.Util.extend(OpenLayers.Feature.Vector.style['default'], {});
        OpenLayers.Handler.Point.prototype.initialize.apply(this, arguments);
    },
    

    /**
     * APIMethod: deactivate
     * turn off the handler
     */
    deactivate: function() {
        if(!OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) {
            return false;
        }
        // call the cancel callback if mid-drawing
        if(this.drawing) {
            this.cancel();
        } 
        /** 
        * Modif de LRA pour l'outil de mesure         
        **/
        this.destroyFeature(); 
        /*
        * Fin de modif de LRA pour l'outil de mesure 
        */
        
        // If a layer's map property is set to null, it means that that layer
        // isn't added to the map. Since we ourself added the layer to the map
        // in activate(), we can assume that if this.layer.map is null it means
        // that the layer has been destroyed (as a result of map.destroy() for
        // example.
        if (this.layer.map != null) {
            this.layer.destroy(false);
        }
        this.layer = null;
        return true;
    },
    /**
     * Method: finalize
     * Finish the geometry and call the "done" callback.
     */
    finalize: function() {
    		/**
    		* Commenter par LRA pour l'integration de l'outil de mesure 
    		*/
        
        //this.layer.renderer.clear();
        
        /**
        * Fin de commentaire de LRA pour l'integration de l'outil de mesure 
        */
        this.drawing = false;
        this.mouseDown = false;
        this.lastDown = null;
        this.lastUp = null;
        this.callback("done", [this.geometryClone()]);
        /**
        * Début de modif de LRA pour l'integration de l'outil de mesure 
        */
        //this.destroyFeature();
        if(!this.persist) {
        	this.clear(); 
        }  
        /**
        * Fin de modif de LRA pour l'integration de l'outil de mesure 
        */
    },

    /**
     * APIMethod: cancel
     * Finish the geometry and call the "cancel" callback.
     */
    cancel: function() {
    		/**
    		* Commenter par LRA pour l'integration de l'outil de mesure 
    		*/
        
        //this.layer.renderer.clear();
        
        /**
        * Fin de commentaire de LRA pour l'integration de l'outil de mesure 
        */
        this.drawing = false;
        this.mouseDown = false;
        this.lastDown = null;
        this.lastUp = null;
        this.callback("cancel", [this.geometryClone()]);
        /**
        * Début de modif de LRA pour l'integration de l'outil de mesure 
        */
        //this.destroyFeature();
        	this.clear(); 
        /**
        * Fin de modif de LRA pour l'integration de l'outil de mesure 
        */
    },
    /**
     * Method: mousedown
     * Handle mouse down.  Adjust the geometry and redraw.
     * Return determines whether to propagate the event on the map.
     * 
     * Parameters:
     * evt - {Event} The browser event
     *
     * Returns: 
     * {Boolean} Allow event propagation
     */
    mousedown: function(evt) {
        // check keyboard modifiers
        if(!this.checkModifiers(evt)) {
            return true;
        }
        // ignore double-clicks
        if(this.lastDown && this.lastDown.equals(evt.xy)) {
            return true;
        }
        if(this.lastDown == null) {
        	/** 
        	* Début de modif de LRA pour l'integration de l'outil de mesure 
        	*/
        	if(this.persist) {
        		this.clear(); 
        	}
        	/**
        	* Fin de modif de LRA pour l'integration de l'outil de mesure 
        	*/
           this.createFeature();
        }
        this.lastDown = evt.xy;
        this.drawing = true;
        var lonlat = this.map.getLonLatFromPixel(evt.xy);
        this.point.geometry.x = lonlat.lon;
        this.point.geometry.y = lonlat.lat;
        this.point.attributes = {
                name: "toto",
                age: 20
        };
        //this.style = OpenLayers.Util.extend({},OpenLayers.Feature.Vector.style['default']);
        this.drawFeature();
        return false;
    },
    
    /**
     * APIMethod: activate
     * turn on the handler
     */
    activate: function() {
        if(!OpenLayers.Handler.prototype.activate.apply(this, arguments)) {
            return false;
        }
        // create temporary vector layer for rendering geometry sketch
        // TBD: this could be moved to initialize/destroy - setting visibility here
        var options = {
            displayInLayerSwitcher: false,
            renderers : ['Canvas', 'VML'] ,
            calculateInRange: function() { return true; }
        };
        this.layer = new OpenLayers.Layer.Vector(this.CLASS_NAME, options);
        this.map.addLayer(this.layer);
        return true;
    },
    

    CLASS_NAME: "OpenLayers.Handler.Point.Ux"
});

