/* 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 
 */ 
 	 
 /** 
 * Class: OpenLayers.Control.Measure 
 * Allows for drawing of features for measurements. 
 * 
 * Inherits from: 
 *  - <OpenLayers.Control> 
 *
 * IF 29/05/2008
 * Please note this is a simplified measuring tool - 2.7 openlayers may have proper implementation
 *
 */ 
 OpenLayers.Control.UKMeasure = OpenLayers.Class(OpenLayers.Control, { 
 		 
 	/** 
 	* APIProperty: handlerOptions 
 	* {Object} Used to set non-default properties on the control's handler 
 	*/ 
 	handlerOptions: null, 
 		     
 	/** 
 	* APIProperty: onMeasure 
 	* {Function} After a geometry is drawn, onMeasure is called with three 
 	*     arguments: the geometry, its length, and its area. 
 	*/ 
 	onMeasure: function() {}, 
 		     
 	/** 
 	* Property: callbacks 
 	* {Object} The functions that are sent to the handler for callback 
 	*/ 
 	callbacks: null, 
 		     
    /** 
    * Property: displayUnits 
 	* {String} Units for output.  Must be one of 'in', 'ft', 'mi', 'm', 'km', 
 	*     or 'dd'.  If null, displayUnits will be assumed to be the same as 
 	*     map units. 
 	*/ 
 	displayUnits: null, 
 	 
 	/** 
 	* Constructor: OpenLayers.Control.UKMeasure 
 	*  
 	* Parameters: 
 	* handler - {<OpenLayers.Handler>}  
 	* options - {Object}  
 	*/ 
 	initialize: function(handler, options) { 
	        OpenLayers.Control.prototype.initialize.apply(this, [options]); 
	        	        this.callbacks = OpenLayers.Util.extend( 
	            {done: this.measureGeometry, point: this.partialMeasure}, 
	            this.callbacks 
        ); 
	        this.handler = new handler(this, this.callbacks, this.handlerOptions); 
	}, 
 
 	/** 
 	* Method: measureGeometry 
 	*/ 
 	measureGeometry: function(geometry) { 
 		    var area = this.getArea(geometry); 
 	        var length = this.getLength(geometry); 
 	        this.onMeasure(geometry, length, area, this.displayUnits); 
 	}, 
 		     
 	/** 
 	* Method: getArea 
 	*/ 
 	getArea: function(geometry) { 
 	        var area = geometry.getArea(); 
 	        var inPerDisplayUnit = OpenLayers.INCHES_PER_UNIT[this.displayUnits]; 
 	       if(inPerDisplayUnit) { 
             var inPerMapUnit = OpenLayers.INCHES_PER_UNIT[this.map.units]; 
 	            area *= Math.pow((inPerMapUnit / inPerDisplayUnit), 2); 
 	       } 
         return area; 
     }, 
     
	/** 
 	* Method: getLength 
 	*/ 
 	getLength: function(geometry) { 
 		   var length = geometry.getLength(); 
 		   var inPerDisplayUnit = OpenLayers.INCHES_PER_UNIT[this.displayUnits]; 
 		   if(inPerDisplayUnit) { 
 		            var inPerMapUnit = OpenLayers.INCHES_PER_UNIT[this.map.units]; 
 		            length *= (inPerMapUnit / inPerDisplayUnit); 
 		        } 
 		        return length; 
 		    }, 
 		 
 		    CLASS_NAME: "OpenLayers.Control.UKMeasure" 
 	}); 
 	
 	/* 
 	* Simple Class to hold UKMeasurementResults
 	* used by the UKMeasurement (overidden onMeasurement function) class 
 	*/

 OpenLayers.Control.UKMeasurement = OpenLayers.Class({

    /**
     * APIProperty: w
     * {<OpenLayers.Geometry>}
     */
    geometry: null,
    
    /**
     * APIProperty: h
     * {Number} length
     */
    length: 0.0,
    
    /**
     * APIProperty: h
     * {Number} area
     */
    area: 0.0,    

    /** 
    * Property: displayUnits 
 	* {String} Units for output.  Must be one of 'in', 'ft', 'mi', 'm', 'km', 
 	*     or 'dd'.  If null, displayUnits will be assumed to be the same as 
 	*     map units. 
 	*/ 
 	displayUnits: null, 

    /**
     * Constructor: OpenLayers.Size
     * Create an instance of OpenLayers.Size
     *
     * Parameters:
     * w - {Number} width
     * h - {Number} height
     */
    initialize: function(geometry, length, area, displayUnits) {
    	this.geometry = geometry;
        this.length = length;
        this.area = area;
        this.displayUnits = displayUnits;
    },
    
    getGeometry: function(){
    	return this.geometry;
    },
    
    getLength: function(){
    	return this.length;
    },

    getArea: function(){
    	return this.area;
    },   
    
    getDisplayUnits: function(){
    	return this.displayUnits;
    },  
    
    /**
     * Method: toString
     * Return the string representation of a size object
     *
     * Returns:
     * {String} The string representation of OpenLayers.Control.UKMeasurementResults object. 
     */
    toString:function() {
        return ("not implemented");
    },
    
    CLASS_NAME: "OpenLayers.Control.UKMeasurement"
});        
