//$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/apps/igeoportal-standard/branches/2.5_testing/javascript/rendering.js $
/*----------------------------------------------------------------------------
 This file is part of deegree, http://deegree.org/
 Copyright (C) (2005) by:

 Florian Rengers / grit 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:

 Florian Rengers, grit GmbH
 Landwehrstraße 143
 59368 Werne
 http://www.grit.de

 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 Rendering(featureCollection, style, layWidth, layHeight, hspc, vspc) {

	// attributes
	this.featureCollection = featureCollection;
	this.style = style;
	this.iWidth = layWidth;
	this.iHeight = layHeight;
	this.hspc = hspc;
	this.vspc = vspc;
	this.srf = null;
	this.doc = null;
		
	// methods
	this.paint = paint;	
	this.setFeatureCollection = setFeatureCollection;
	this.setStyle = setStyle;
	this.init = init;
	this.kill = kill;
	this.drawLinestring = drawLinestring;
	this.drawPolygon = drawPolygon;
	this.drawPoint = drawPoint;
	this.drawPoint_ = drawPoint_;
	
	
	// implementation
	
	function init(doc) {
		this.doc = doc;
		this.kill();
		this.surface = dojox.gfx.createSurface( getElement( 'drawCanvas', doc ), 1900, 1500);
		this.srf = this.surface.createGroup();
    }
    
    function kill() {
    	if ( this.srf != null ) {			
			this.srf.clear();
			removeChildren( getElement( 'drawCanvas', this.doc ) );
		}	
	}
	
	/**
	* if bbox != null just features intersecting passed bbox will be painted
	*/
	function paint(doc, geoTransformer, bbox) {
		if ( this.featureCollection != null ) {
			this.init( doc );
			var features = this.featureCollection.getFeatures();
			for ( var i = 0; i < features.length; i++ ) {				
				if ( getGeometryType( features[i].getGeometry() ) == POINT ) {
					this.drawPoint(doc, geoTransformer, bbox, features[i].getGeometry());
				} else if ( getGeometryType( features[i].getGeometry() ) == LINESTRING) {
					this.drawLinestring(doc, geoTransformer, bbox, features[i].getGeometry());
				} else if ( getGeometryType( features[i].getGeometry() ) == POLYGON ) {
					this.drawPolygon(doc, geoTransformer, bbox, features[i].getGeometry());
				} 
			}
		}
	}
	
	function drawPoint(doc, gt, bbox, point) {		
		var x = new Array();
		var y = new Array();
		x.push( gt.getDestX( point.x ) );
		y.push( gt.getDestY( point.y ) );
		this.drawPoint_( x, y, this.style.point );
	}
	
	function drawPoint_( x, y, stl ) {
		var bg = stl.bgcolor;
		if ( bg == null ) {
			bg = [ 255,255,255,0 ];
		}
		var fg = stl.fgcolor;
		if ( fg == null ) {
			fg = [ 255,255,255,0 ];
		}
		
		if ( stl.mark != null ) {
			var sz = stl.size;
			var xx =  x[i]-sz/2;
			var yy = y[i]-sz/2;
			for ( var i = 0; i < x.length; i++) {
				if ( stl.mark == 1 ) {
					var circle = this.srf.createCircle({ cx: x[i], cy: y[i], r: sz/2 });
					circle.setFill( bg )
	                circle.setStroke( fg );	
				} else if ( stl.mark == 2 ) {
					var rect = this.srf.createShape({type: "rect", x: xx, y: yy, width: sz, height: sz});
					rect.setFill( bg )
	                rect.setStroke( fg );	                
				} else if ( stl.mark == 3 ) {
					// TODO
				} else if ( stl.mark == 4 ) {
					var l1 = this.srf.createPolyline([ x[i]-sz/2, y[i], x[i]+sz/2, y[i] ]);
					l1.setStroke( fg );
					var l2 = this.srf.createPolyline([ x[i], y[i]+sz/2, x[i], y[i]-sz/2 ]);
					l2.setStroke( fg );
				} else if ( stl.mark == 5 ) {
					var l1 = this.srf.createPolyline([ x[i]-sz/2, y[i]-sz/2, x[i]+sz/2, y[i]+sz/2 ]);
					l1.setStroke( fg );
					var l2 = this.srf.createPolyline([ x[i]-sz/2, y[i]+sz/2, x[i]+sz/2, y[i]-sz/2 ]);
					l2.setStroke( fg );
				} else if ( stl.mark == 6 ) {
					var l1 = this.srf.createPolyline([ x[i]-sz/2, y[i], x[i]+sz/2, y[i] ]);
					l1.setStroke( fg );
					var l2 = this.srf.createPolyline([ x[i], y[i]+sz/2, x[i], y[i]-sz/2 ]);
					l2.setStroke( fg );
					var l3 = this.srf.createPolyline([ x[i]-sz/2, y[i]-sz/2, x[i]+sz/2, y[i]+sz/2 ]);
					l3.setStroke( fg );
					var l4 = this.srf.createPolyline([ x[i]-sz/2, y[i]+sz/2, x[i]+sz/2, y[i]-sz/2 ]);
					l4.setStroke( fg );
				} 
			}
			this.srf.moveToFront();
		}
	}
	
	function drawPolygon(doc, gt, bbox, polygon) {		
		var points = polygon.points;
		var x = new Array();
		var y = new Array();
		for ( var i = 0; i < points.length; i++) {
			x.push( gt.getDestX( points[i].x ) );
			y.push( gt.getDestY( points[i].y ) );
		}
		var p1 = this.srf.createShape({type: "path"});
		p1.setStroke( {color: this.style.polygon.line.color, width: this.style.polygon.line.width} );
		p1.setFill(this.style.polygon.fillColor);						
		p1.moveTo( x[0], y[0] );
		for ( var i = 1; i < x.length; i++) {
			p1.lineTo( x[i], y[i] )
		}
		this.srf.moveToFront();
		
		if ( this.style.polygon.point != null ) {
			this.drawPoint_( x, y, this.style.polygon.point );
		}
		
	}
	
	function drawLinestring(doc, gt, bbox, linestring) {		
		var points = linestring.points;
		var x = new Array();
		var y = new Array();
		for ( var i = 0; i < points.length; i++) {
			x.push( gt.getDestX( points[i].x ) );
			y.push( gt.getDestY( points[i].y ) );
		}
		var p1 = this.srf.createPath().setStroke( {color: "red", width: 2} );
		p1.moveTo( x[0], y[0] );
		for ( var i = 1; i < x.length; i++) {
			p1.lineTo( x[i], y[i] )
		}		
		if ( this.style.polygon.point != null ) {
			this.drawPoint_( x, y, this.style.line.point );
		}		
	}	
	
	function setFeatureCollection(featureCollection) {
		this.featureCollection = featureCollection;
	}
	
	function setStyle( style ) {
		this.style = style;
	}
	
}
