/* Copyright (c) 2006-2007 MetaCarta, Inc., published under the BSD license.
 * See http://svn.openlayers.org/trunk/openlayers/release-license.txt 
 * for the full text of the license. */

/**
 * @requires OpenLayers/Control.js
 * @requires OpenLayers/Handler/Box.js
 *
 * Class: OpenLayers.Control.iSelectBox
 *
 * Inherits from:
 *  - <OpenLayers.Control>
 */
OpenLayers.Control.iSelectBox= OpenLayers.Class(OpenLayers.Control, {
    /**
     * Property: type
     * {OpenLayers.Control.TYPE}
     */
    type: OpenLayers.Control.TYPE_TOOL,

    /**
     * Property: layer
     * {<OpenLayers.Layer.Vector>}
     */
    layer: null,
    
    
    /**
     * Property: multibox (default true, allowing multiple selection boxes)
     * {Boolean}
     */
    multibox: false,
    
    /**
     * Constructor: <OpenLayers.Control.DeleteFeature>
     *
     * Parameters:
     * layer - {<OpenLayers.Layer.Vector>} 
     * options - {Object} 
     */
    initialize: function(layer, options) {
        OpenLayers.Control.prototype.initialize.apply(this, [options]);
        this.layer = layer;
    },   
    
    /**
     * Method: draw
     */    
    draw: function() {
        this.handler = new OpenLayers.Handler.Box( this,
                            {done: this.selectBox}, {keyMask: this.keyMask} );
    },
    
    /**
     * APIProperty: featureAdded
     * {Function} Called after each feature is added
     */
    featureAdded: function() {},

    /**
     * Method: zoomBox
     *
     * Parameters:
     * position - {<OpenLayers.Bounds>} or {<OpenLayers.Pixel>}
     */
    selectBox: function (position) {
        if (position instanceof OpenLayers.Bounds) {
        
            var minXY = this.map.getLonLatFromPixel(
                            new OpenLayers.Pixel(position.left, position.bottom));
            var maxXY = this.map.getLonLatFromPixel(
                            new OpenLayers.Pixel(position.right, position.top));
            var bounds = new OpenLayers.Bounds(minXY.lon, minXY.lat,
                                               maxXY.lon, maxXY.lat);
                                               
		if (!this.multibox)this.layer.destroyFeatures();

        //var geometry = new OpenLayers.Geometry.Rectangle(minXY.lon, minXY.lat,maxXY.lon-minXY.lon, maxXY.lat-minXY.lat );


        //Create a polygon geometry box (to allow manipulation of vertices etc)
        var points = [];
        points.push(new OpenLayers.Geometry.Point(minXY.lon, minXY.lat));
        points.push(new OpenLayers.Geometry.Point(minXY.lon, maxXY.lat));
        points.push(new OpenLayers.Geometry.Point(maxXY.lon, maxXY.lat));
        points.push(new OpenLayers.Geometry.Point(maxXY.lon, minXY.lat));
    	var ring = new OpenLayers.Geometry.LinearRing(points);
    	var geometry =  new OpenLayers.Geometry.Polygon([ring]);
    	
       	var feature = new OpenLayers.Feature.Vector(geometry);
        this.layer.addFeatures([feature]);
        this.featureAdded(feature);
            
        } 
        else {
        	// Do nothing
        }
    },

    CLASS_NAME: "OpenLayers.Control.iSelectBox"
});

