/* 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/Control.js
 * @requires OpenLayers/Feature/Vector.js
 * @requires OpenLayers/Handler/Feature.js
 */

/**
 * Class: OpenLayers.Control.SelectFeature
 * Selects vector features from a given layer on click or hover. 
 *
 * Inherits from:
 *  - <OpenLayers.Control>
 */
OpenLayers.Control.SelectCircle = OpenLayers.Class(OpenLayers.Control, {
  

    /**
     * Constructor: <OpenLayers.Control.SelectFeature>
     *
     * Parameters:
     * layer - {<OpenLayers.Layer.Vector>} 
     * options - {Object} 
     */
    initialize: function(layer, options) {
    		this.options = options;
        this.handlerOptions = {persist: true, sides: 40};
        OpenLayers.Control.prototype.initialize.apply(this, [options]);
        this.callbacks = OpenLayers.Util.extend({done: this.sendRequest},
                                                this.callbacks);
        this.layer = layer;   
        this.handler = new OpenLayers.Handler.RegularPolygon(this, this.callbacks, this.handlerOptions);
    },
    
    sendRequest: function(feature){
        var bounds = feature.getBounds();
        var center = bounds.getCenterLonLat() ;
        var radius = bounds.getWidth()/2 ;

        var query = "typeSelection=cercle&ctrl=0&ptCenter="+center.lon+' '+center.lat+'&ptRadius='+radius;
        if(!this.layer)return ;
        //ToDo si plusieurs layers OU layer actif ?!
        this.layer.query = query;
        this.layer.redraw();
        this.layer.query = "";
        this.handler.clear();
        
        if(this.options.afterSelect) {
        	this.options.afterSelect();
        }
    },
    /** 
     * Method: setMap
     * Set the map property for the control. 
     * 
     * Parameters:
     * map - {<OpenLayers.Map>} 
     */
    setMap: function(map) {
        this.handler.setMap(map);
        OpenLayers.Control.prototype.setMap.apply(this, arguments);
    },

    CLASS_NAME: "OpenLayers.Control.SelectCircle"
});

