/* Copyright (c) 2008 Geosignal, 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.SelectPoint
 * Selects gsMap features from a given layer on click. 
 *
 * Inherits from:
 *  - <OpenLayers.Control>
 */
OpenLayers.Control.SelectRectangle = OpenLayers.Class(OpenLayers.Control, {
    /**
     * Constructor: <OpenLayers.Control.SelectPoint>
     *
     * Parameters:
     * layer - {<OpenLayers.Layer.gsMap>} 
     * options - {Object} 
     */
    initialize: function(layer, options) {
    		this.options = options;
    		this.handlerOptions = {
    			sides:4,
    			irregular:true,
    			style : OpenLayers.Util.extend({},OpenLayers.Feature.Vector.style['default'])
    		};
    		
        OpenLayers.Control.prototype.initialize.apply(this, [options]);
        this.callbacks = OpenLayers.Util.extend({done: this.sendRequest});
        this.layer = layer;   
        this.handler = new OpenLayers.Handler.RegularPolygon(this, this.callbacks, this.handlerOptions);
        
        //OpenLayers.Util.extend(this.handler.style,{pointRadius: 1,fillColor:'#ff0000'});
        //OpenLayers.Util.extend(this.handler.style,{pointRadius: 1,fillColor:'#ff0000'});
        
    },
    
		sendRequest: function(feature){
        var bounds = feature.getBounds();

        var minXY = this.map.getViewPortPxFromLonLat(
                    new OpenLayers.LonLat(bounds.left, bounds.top));
        var maxXY = this.map.getViewPortPxFromLonLat(
                    new OpenLayers.LonLat(bounds.right, bounds.bottom));
        var query = "typeSelection=rect&ctrl=0&ptStart="+minXY.lon+","+minXY.lat+"&ptEnd="+maxXY.lon+","+maxXY.lat;
        var startX = parseInt(minXY.x);
        var startY = parseInt(minXY.y);

        var endX = parseInt(maxXY.x);
        var endY = parseInt(maxXY.y);
        setSelection(
        	{
        		x: endX,
        		y : endY
        	},
        	{
        		x: startX,
        		y : startY
        	}
        );
return ;
        if(!this.layer)return ;
        this.layer.query = query;
        this.layer.redraw();
        this.layer.query = "";
        //this.handler.removeBox();
        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.SelectRectangle"
});

