//$HeadURL: svn+ssh://developername@svn.wald.intevation.org/deegree/apps/igeoportal-standard/trunk/modules/timeselect/timeselect.js $
/*----------------------------------------------------------------------------
 This file is part of deegree, http://deegree.org/
 Copyright (C) 2001-2009 by:
 - Department of Geography, University of Bonn -
 and
 - lat/lon GmbH -

 This library is free software; you can redistribute it and/or modify it under
 the terms of the GNU Lesser General Public License as published by the Free
 Software Foundation; either version 2.1 of the License, or (at your option)
 any later version.
 This library is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 details.
 You should have received a copy of the GNU Lesser General Public License
 along with this library; if not, write to the Free Software Foundation, Inc.,
 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

 Contact information:

 lat/lon GmbH
 Aennchenstr. 19, 53177 Bonn
 Germany
 http://lat-lon.de/

 Department of Geography, University of Bonn
 Prof. Dr. Klaus Greve
 Postfach 1147, 53001 Bonn
 Germany
 http://www.geographie.uni-bonn.de/deegree/

 e-mail: info@deegree.org
----------------------------------------------------------------------------*/
function Measurement() {
	
	// variable declaration
	this.targetDoc; 
	this.parentElement;
	this.tbMeasurement; 
	this.digits = 0;
	
	// method declaration
	this.paint = paint;
	this.repaint = repaint;	
	this.kill = kill;
	this.stopMeasurement = stopMeasurement;
	this.updateMeasurement = updateMeasurement;
	this.actionPerformed = actionPerformed;
	this.showSegmentInfo = showSegmentInfo;
	
	// method implementation
	
	function paint(targetDoc, parentElement) {
		this.targetDoc = targetDoc; 
		this.parentElement = parentElement; 
		var btLength = new ToggleButton( 'measurement:length', 'Measure length', 'images/measure_length.png', 
																		    'images/measure_length.png' );
		btLength.setListener( this );
		var btArea = new ToggleButton( 'measurement:area', 'Measure area', 'images/measure_areas.png', 
																		'images/measure_areas.png' );
		btArea.setListener( this );
		var btReset = new PushButton( 'measurement:reset', 'Reset measurement', 'images/refresh.gif', 
																			  'images/refresh.gif' );
		btReset.setListener( this );
		var bgMeasurement = new ButtonGroup( 'BGmeasurement' );
		bgMeasurement.addButton( btLength );
		bgMeasurement.addButton( btArea );
		bgMeasurement.addButton( btReset );
		var buttonGroups = new Array( bgMeasurement );
		this.tbMeasurement = new Toolbar( 'Measurment', buttonGroups, 100, 30, null );
		this.tbMeasurement.paint( targetDoc, getElement( 'measureTB', targetDoc ) );
	}
	
	function repaint() {
		// nothing to do
	}
	
	function actionPerformed(event) {
		this.tbMeasurement.reset();
		this.kill();
		if ( event.name != 'measurement:resetPressed' ) {
			var mm = controller.mapModel;
			var geom = null; 
            var props = new Array();
            props[0] = new Object();
            props[0]["Date"] = new Date();
            var feature = new Feature( "ID", null, props, geom );               
            var fc = mm.getFeatureCollection( "digitizing" );
            if ( fc == null ) {
                var style = getDefaultStyle();               
                fc = new FeatureCollection( "FC", new Array() ); 
                var rendering = new Rendering( fc, style, mm.getWidth(), mm.getHeight(), 0, 0 ); 
                mm.addFeatureCollection( "digitizing", fc, rendering );
            }
            fc.removeAll();
            fc.addFeature( feature );
			controller.mode = event.name.substring( 0, event.name.length - 7 );
			// show input fields for measurement values
			getElement( 'tbLength', this.targetDoc ).style.visibility = 'visible';
			if ( event.name == 'measurement:areaPressed' ) {
				getElement( 'tbArea', this.targetDoc ).style.visibility = 'visible';
			} 
		}
	}
	
	/**
	 * updates GUI with passed length and area value 
	 * 
	 * @param length
	 * @param area 
	 */
	function updateMeasurement(length, area) {
		var s = "" + length;
		var p = s.indexOf( "." );
		if ( this.digits  == 0 ) {
			s = s.substring( 0, p + this.digits  );
		} else {
			s = s.substring( 0, p + this.digits + 1 );
		}
		getElement( 'inpLength', this.targetDoc ).value = s;
		if ( area ) {
			s = "" + area;
			var p = s.indexOf( "." );
			if ( this.digits  == 0 ) {
				s = s.substring( 0, p + this.digits  );
			} else {
				s = s.substring( 0, p + this.digits + 1 );
			}
			getElement( 'inpArea', this.targetDoc ).value = s;
		}			
	}
	
	function showSegmentInfo() {
		removeChildren( getElement( 'segInfoArea', this.targetDoc ) );
		getElement( 'segInfoArea', this.targetDoc ).style.visibility = 'visible';
		var fc = controller.mapModel.getFeatureCollection( "digitizing" );
		if ( fc != null && fc.size() > 0 ) {
			var f = fc.getFeature( 0 );
			var points = f.getGeometry().points;		
			for ( var i = 0; i < points.length-1; i++ ) {
				var x1 = points[i].x;
				var y1 = points[i].y;
				var x2 = points[i+1].x;
				var y2 = points[i+1].y;
				var d = Math.sqrt( (x2 -x1 ) * (x2 -x1) +  (y2 -y1 ) * (y2 -y1) );
				var s = "" + d;
				var p = s.indexOf( "." );
				if ( this.digits  == 0 ) {
					s = s.substring( 0, p + this.digits  );
				} else {
					s = s.substring( 0, p + this.digits + 1 );
				}
				createElement( 'div', 'd' + i, '', 'Segment ' + (i + 1) + ': ' + s + ' m', 
						       getElement( 'segInfoArea', this.targetDoc ), this.targetDoc );			
				
			}
		}
	}
	
	function stopMeasurement() {
		this.tbMeasurement.reset();
		controller.mode = '-';
		getElement( 'buttonArea', this.targetDoc ).style.visibility = 'visible';
	}
	
	/**
	 * removes and hides all elements except toolbar
	 * 
	 */
	function kill() {
		// remove measured geometry
		var mm = controller.mapModel;
    	var fcs = mm.getFeatureCollections();
        for (var i = 0; i < fcs.length; i++) {
            fcs[i]['REND'].kill();
            fcs[i]['FC'].removeAll();
        }
        mm.removeFeatureCollection( 'digitizing' );
        // reset controller
        controller.mode = '-';
        controller.repaint();        
		// clear input fields for measurement values        
        getElement( 'inpLength', this.targetDoc ).value = '';
        getElement( 'inpArea', this.targetDoc ).value = '';
		// hide input fields for measurement values        
        getElement( 'tbLength', this.targetDoc ).style.visibility = 'hidden';
		getElement( 'tbArea', this.targetDoc ).style.visibility = 'hidden';
		getElement( 'buttonArea', this.targetDoc ).style.visibility = 'hidden';
		getElement( 'segInfoArea', this.targetDoc ).style.visibility = 'hidden';
	}
	
}