//$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/apps/igeoportal-standard/branches/2.5_testing/mapview.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 MapView(mapModel, border) {
	
	this.changed = true;
	this.mapModel = mapModel;
	this.htmlLayer = new Array();
	this.border = border;
	this.parentNode;
	this.parentDoc;
	this.aktiv;
	this.digitizer;
	
	this.paint = paint;
	this.repaint = repaint;
	this.getMapModel = getMapModel;
	this.setMapModel = setMapModel;
	this.removeHTMLLayers = removeHTMLLayers;
	this.getLayerType = getLayerType;
	this.isChanged = isChanged;
    this.getHTMLLayers = getHTMLLayers;
    this.offX = this.border
    this.offY = this.border
	this.showProgress = showProgress;
	this.hideProgress = hideProgress;
	this.render = render;
	this.setDigitizer = setDigitizer;
	this.killDigitizer = killDigitizer;
	
	function setDigitizer( digitizer ) {
		this.digitizer = digitizer;
	}
	
	function killDigitizer() {
		if ( this.digitizer != null ) {
			this.digitizer.kill();
			this.digitizer = null;
	        var fc = controller.mapModel.getFeatureCollection( "digitizing" );
	        if ( fc != null ) {
	        	fc.removeAll();
	        	controller.repaint();
	        }	        
		}
	}
	
	function getMapModel() {
		return this.mapModel;
	}

	function setMapModel(mapModel) {
		this.mapModel = mapModel;
		this.changed = true;
	}

	function removeHTMLLayers(parentDocument) {
		// remove HTML layers
		for(var i = 0; i < this.htmlLayer.length; i++){
			//if (  this.htmlLayer[i].getLayerGroup().isChanged() ) {
				var id = this.htmlLayer[i].getId();
				var node = parentDocument.getElementById( id );
				if ( node != null ) {
					node.parentNode.removeChild( node );
				} 
			//}
		}
	}
    
    function getHTMLLayers() {
        return this.htmlLayer;
    }

	function getLayerType(layerGroup) {
		var type = '';
		var st = layerGroup.getServiceType();
		if ( st.indexOf('WMS') > 0 ) {
			type = 'WMSLAYER';
		} else if ( st.indexOf('WFS') > 0 ) {
			type = 'WFSLAYER';
		} else if ( st.indexOf('WCS') > 0 ) {
			type = 'WCSLAYER';
		}
	}

	function isChanged() {
		return this.changed;
	}

	function paint(parentDocument, parentNode) {

		this.parentDoc = parentDocument;
		this.parentNode = parentNode;
		
		window.setTimeout( 'controller.vMapView.render()', 250 );
	}
	
	function render() {
		this.hideProgress();
		this.showProgress();

		if( this.changed  || this.mapModel.isChanged() ) {
			this.removeHTMLLayers( this.parentDoc );
		} 
		var layerlist = this.mapModel.getLayerList();
		var layerGroups = layerlist.getLayerGroups();
		var tmpArr = new Array();
		for(var i = 0; i < layerGroups.length; i++){
			if( this.changed || layerGroups[i].isChanged() ) {
				var type = this.getLayerType( layerGroups[i] );
				var id = 'HTMLMapLayer:' + layerGroups[i].id;
				var tmp = new HTMLMapLayer( id , layerGroups.length - i, type, this.offY, 
				                            this.offX, layerGroups[i], this.mapModel );
				tmp.paint( this.parentDoc, this.parentNode );
				tmpArr.push( tmp );
			} else {
				tmpArr.push( this.htmlLayer[i] );
			}
		}        
		this.htmlLayer = tmpArr;
		this.changed = false;
		this.mapModel.setChanged( false );
				
		// render javascript features
		var gt = this.mapModel.getTransformer();

        var fcs = this.mapModel.getFeatureCollections();
		for (var i = 0; i < fcs.length; i++) {
			fcs[i]["REND"].paint( this.parentDoc, gt, null );			
		}		
		this.aktiv = window.setInterval( "controller.vMapView.hideProgress()", 100 );
	}

	function repaint() {
		this.paint(this.parentDoc, this.parentNode);
	}
	
	function showProgress() {		
		var x1 = this.mapModel.getWidth() / 2 -115;
		var x2 = this.mapModel.getWidth() / 2 +115;
		var y1 = this.mapModel.getHeight() / 2 -91;
		var y2 = this.mapModel.getHeight() / 2 +91;
		var layer = createLayer( this.parentDoc, "progress", "progress", x1, y1, 
							     x2, y2, 
								 1000,  null, true );
		createImage( "images/progress.gif", this.parentDoc, layer, 230 , 182, "progressIMG" );
		this.parentNode.appendChild( layer );
	}
	
	function hideProgress() {		
		var finished = true;
		
		for (var i = 0; i < this.parentDoc.images.length; ++i) {
			if ( !this.parentDoc.images[i].complete && 
					this.parentDoc.images[i].id. indexOf("overview") < 0 ) {
				 finished = false;
			}
		}

		if ( finished ) {
			removeHTMLLayer( this.parentDoc, "progress" );
			window.clearInterval(this.aktiv);
			if ( controller.vLayerTree ) {
				controller.vLayerTree.repaint();
		    }
		}		
	}

}
