//$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/apps/igeoportal-standard/branches/2.5_testing/modules/digitizer/digitizer.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 Digitizer (layWidth, layHeight, hspc, vspc, drawColor) {

	this.iWidth = layWidth;
	this.iHeight = layHeight;
	this.hspc = hspc;
	this.vspc = vspc;
	this.drawColor = drawColor;
	this.mouseX = 0;
    this.mouseY = 0;
	this.x = 0;
	this.y = 0;
	this.lineDrawing = false;
    this.is5up = false;
    this.doc = document;
    this.closed = false;
    this.coords = new Array();

	this.isPointMode = false;
    this.stroke = 1;
    this.srf; 
    this.surface;
	
	// method declaration
	this.init = init;
	this.kill = kill;
	this.clear = clear;
	this.getMouse = getMouse;
	this.chkMouseUp = chkMouseUp;
	this.startDigitize = startDigitize;
	this.getCoordinates = getCoordinates;
	this.closeToPolygon = closeToPolygon;
	this.getAsLineString = getAsLineString;
	this.getAsPolygon = getAsPolygon;
	this.createLayer = createLayer;
	this.createImage = createImage;
    this.setDocument = setDocument;
   
   	//UT
	this.setPointMode = setPointMode;
	this.drawCross = drawCross;
    this.setStroke = setStroke;
	this.getAsPoint = getAsPoint;
	this.setDigitizingComplete = setDigitizingComplete;

	// method implementation
	function setDocument(docum) {
		this.doc = docum;
	}

	function init() {
		this.kill();
		this.createLayer( "digitizeLayer", this.hspc, this.vspc, 50, null, true );
		this.surface = dojox.gfx.createSurface( this.doc.getElementById( "digitizeLayer" ), this.iWidth, this.iHeight );
		this.srf = this.surface.createGroup();
    }
	
	function createLayer(name, offsetx, offsety, zIdx, bgColor, visible) {
		newLayer = this.doc.createElement("div");
		newLayer.setAttribute("id", name);
		newLayer.setAttribute("name", name);
		newLayer.style.position = "absolute";
		newLayer.style.top = offsety + 'px';
        newLayer.style.left = offsetx + 'px';
		newLayer.style.zIndex = zIdx;
		newLayer.style.width = this.iWidth;
		newLayer.style.height = this.iHeight;
		newLayer.style.overflow ='visible';
		newLayer.style.visibility = visible ? 'visible' : 'hidden';
		if ( bgColor != null ) {
			newLayer.style.backgroundColor = bgColor;
		}		
		this.doc.getElementsByTagName('body')[0].appendChild(newLayer);
	}
    
    function createImage(newLayer) {
		var newImage = this.doc.createElement("img");
		newImage.setAttribute("id", "overviewt");
		newImage.setAttribute("src", "../../images/up.gif");
		newImage.setAttribute("border", 1);	
		newImage.setAttribute("height", this.iHeight);
		newImage.setAttribute("width", this.iWidth);
		newLayer.appendChild(newImage);
	}
	
	function clear() {		
		this.closed = true;
		this.lineDrawing = false;
	}
	
	function kill() {
		if ( this.srf != null ) {			
			this.srf.clear();
			removeChildren( this.doc.getElementById( "digitizeLayer" ) );
		}
		this.closed = true;
		this.lineDrawing = false;		
        var layer = this.doc.getElementById( "digitizeLayer" );
		if ( layer != null ) {
			this.clear();
			layer.parentNode.removeChild( layer );	
		}
	}	
	
    function getMouse( xx, yy ) {    
        if ( (xx <= this.iWidth) && (yy <= this.iHeight) && xx > 0 && yy > 0 &&
             this.mouseX >= 0 && this.mouseY >= 0 && xx > 5 && yy > 5 ) {
        	 this.srf.clear();
             if( this.isPointMode ){
    	         //do nothing, cross has already been drawn
	         } else {    
				this.x = xx;
				this.y = yy;
				if ( this.lineDrawing ) {
					var p1 = this.srf.createPath().setStroke( {color: "red", width: this.stroke} );
					p1.moveTo( this.coords[0].x, this.coords[0].y );
					for ( var i = 1; i < this.coords.length; i++) {
						p1.lineTo( this.coords[i].x, this.coords[i].y )
					}
					p1.lineTo( xx, yy )
					this.srf.moveToFront();
				} 
				
				
			}
		} 
	}

    function chkMouseUp() {
		//this.lineDrawing = false;		
		this.coords.push( new Point( this.x, this.y ) ); 
		this.mouseX = this.x;
		this.mouseY = this.y;
    }

    function startDigitize(x,y) {
    	if ( this.closed ) {
    		this.coords = new Array();
    		this.closed = false;
    	}
        if ( !this.lineDrawing ) {
            this.mouseX= x;
            this.mouseY= y;
            this.x = this.mouseX;
            this.y = this.mouseY;
        	this.coords.push( new Point( x, y ) );
        	
        	if( this.isPointMode ){
	         	this.drawCross( x, y );
	        }
        }
        this.lineDrawing = true;	
    }

    function getCoordinates() {
    	return this.coords;
    }
    
    function getAsLineString(gt) {
    	var points = Array();
    	for ( var i = 0; i < this.coords.length; i++) {
    		var xx = gt.getSourceX( this.coords[i].x + this.hspc );
    		var yy = gt.getSourceY( this.coords[i].y + this.vspc );
			points.push( new Point( xx, yy ) );
    	}
    	this.lineDrawing = false;
    	return new LineString( points );
    }
    
    function closeToPolygon() {
    	this.coords.push( new Point( this.coords[0].x, this.coords[0].y ) );     	
		this.closed = true;
        this.lineDrawing = false;
    }
    
    function getAsPolygon(gt) {
		if ( !this.closed ) {
			this.closeToPolygon();
		}
		var points = Array();
    	for ( var i = 0; i < this.coords.length; i++) {
    		var xx = gt.getSourceX( this.coords[i].x + this.hspc );
    		var yy = gt.getSourceY( this.coords[i].y + this.vspc );
			points.push( new Point( xx, yy ) );
    	}
    	return new Polygon( points );
	}

	function setStroke( s ){
		this.stroke = s;
	}

	function setPointMode( pointMode ){
		this.isPointMode = pointMode;
	}
	
	function drawCross( x, y){
		var l1 = this.srf.createPolyline([ x-5, y[i], x[i]+5, y[i] ]);
		l1.setStroke( fg );
		var l2 = this.srf.createPolyline([ x[i], y[i]+5, x[i], y[i]-5 ]);
		l2.setStroke( fg );
     }
     
     function getAsPoint(gt) {
   		var xx = gt.getSourceX( this.coords[0].x + this.hspc );
   		var yy = gt.getSourceY( this.coords[0].y + this.vspc );
    	return new Point( xx, yy );
	}
	
	function setDigitizingComplete( isComplete ){
		this.closed = isComplete;
	}
}
