//$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/apps/igeoportal-standard/branches/2.5_testing/modules/digitizer/digitizermodule.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 DigitizerModule( standAloneMode, geomTypes, lineColor, label, validateGeom, mapDimension, windowDimension, formLabels, formTemplates, featureTypes, wfsAddresses, wfsInsertTemplates ) {

    // variable declaration
    this.standAloneMode = standAloneMode;
    this.geomTypes = geomTypes;
    this.label = label;
	this.lineColor = escape(lineColor);

	this.mainWindow = null;
	this.digitizerWindow = null;
	
	this.mapDimension = mapDimension;
	this.windowDimension = windowDimension;
	
    // method declaration
    this.paint = paint;
    this.repaint = repaint;
    
    //needed for callig digitzer from module html/jsp
    this.openDigWindow = openDigWindow;
    
    // method implementation
    function paint( targetDocument, parentElement ) {
    	this.mainWindow = targetDocument;    	
    	createLink( targetDocument, parentElement, this.label );    	
    }

    function repaint() {
		//nothing to do    
    }

    //needed for callig digitzer from module html/jsp    
    function openDigWindow(){
   		//break something like http://host:80/myportal/mod/dig/digmodule.html
        //into http://host:80/myportal/mod/dig/
        var src = this.mainWindow.location.href.substr( 0, this.mainWindow.location.href.lastIndexOf( "/" ) + 1 );
        src += "digitizer_window.jsp?formLabels=" + formLabels + '&formTemplates='+ formTemplates + '&featureTypes=' + featureTypes +
        	   '&geomTypes=' + this.geomTypes + '&validateGeometry=' +validateGeom + '&lineColor=' + this.lineColor;

        if( standAloneMode ){
    		var dim = this.mapDimension.split( ',' );
    		var mapW = dim[0];
    		var mapH = dim[1];
    		src += "&mapWidth=" + mapW + "&mapHeight=" + mapH;
    		
    		// now the window dims
    		dim = this.windowDimension.split( ',' );
    		var w = dim[0];
    		var h = dim[1];
    		
    		//be generous an take some space -> creates a left margin
    		var mapLeft = parseInt( w ) - parseInt( mapW ) - 0;
    		
    		//UT mapTop is overridden in digitizer_window.jsp
    		var mapTop = parseInt( h ) - parseInt( mapH );
    		
    		src += "&mapLeft=" + mapLeft + "&mapTop=" + mapTop;
    		
            // TODO: if you want to ensure a specific scale denominator (e.g. 1000 => a 1:1000 map)
            // in the digitizing window, then choose the second line ( var env = scaleEnv() ).
            // The first line uses the scale of the current portal setting.
            var env = parent.controller.mapModel.getBoundingBox();
            //var env = scaleEnv( 100000 );
                        
            src += "&bbox=" + env.minx + "," + env.miny + "," + env.maxx + "," + env.maxy;
            
    		digitizerWindow = window.open( src, "DigitizerWindow", "width="+w+",height="+h+",left=0,top=0");
	    	digitizerWindow.focus();
    	} else {
    		alert( "Only runs in stand alone for now." );
    		//var win = this.mainWindow.getElementById( 'templateWindow' );
    		//win.src = src;
    	}
    }
    
    /* take the bbox from the mapModel.
     * return a new envelope with the same center for the passed scaleDenominator
     */
    function scaleEnv( newScaleDenominator ) {
    
    	var env = parent.controller.mapModel.getBoundingBox();
    	var imgWidth = parent.controller.mapModel.getWidth();
    	var imgHeight = parent.controller.mapModel.getHeight();
    	var pixelWidth = 0.00028; //[m]  //default for 96 dpi = 0.28 mm
    	
		var dx = (env.maxx - env.minx) / imgWidth;
        var dy = (env.maxy - env.miny) / imgHeight;
        var oldScale = Math.sqrt((dx*dx)+(dy*dy));
        
		var oldScaleDenominator = oldScale / ( pixelWidth * Math.sqrt( 2 ) );
		var factor = oldScaleDenominator / newScaleDenominator;

		// old width and height of bounding box		
		var envWidth = env.maxx - env.minx;
        var envHeight = env.maxy - env.miny;

		// middle of the bounding box
		var midx = envWidth/2.0 + env.minx;
        var midy = envHeight/2.0 + env.miny;

		// new width and height of bounding box
        envWidth = envWidth / factor;
		envHeight = envHeight / factor;
		
		// new envelope 
		var minx = midx - envWidth/2.0;
		var maxx = midx + envWidth/2.0;
		var miny = midy - envHeight/2.0;
		var maxy = midy + envHeight/2.0;

		return new Envelope( minx, miny, maxx, maxy );
    }

    function createLink( targetDocument, parentNode, label ){
    
    	this.mainWindow = targetDocument;
    
    	var withImg = false;
    	var pureLabel = label;
    	var imgSrc = null;

    	if( pureLabel.indexOf( '|' ) > 0 ){//then it should contain an img name
	    	withImg = true;
	    	var tmp = label.split( '|' );
	    	pureLabel = tmp[0];
	    	imgSrc = tmp[1];
    	}
    
    	var a = targetDocument.createElement( "a" );
    	a.setAttribute( 'href', 'javascript:openDigWindow();');
    	
		var linkNode = null;		
		if( withImg ){
			linkNode = targetDocument.createElement( "img" );
			linkNode.setAttribute( 'src', imgSrc );
			linkNode.setAttribute( 'alt', pureLabel );
			linkNode.setAttribute( 'title', pureLabel );
			linkNode.setAttribute( 'border', '0' );
		} else {
			linkNode = targetDocument.createTextNode( pureLabel );

            if ( typeof ActiveXObject != 'undefined' ) {
    			//IE
                a.setAttribute("className", "buttonArea");    
            } else {
            	//Firefox
                a.setAttribute("class", "buttonArea");
            }
		}
		
    	a.appendChild( linkNode );
    	
        /*
    	var span = targetDocument.createElement( "span" );
    	span.appendChild( a );
    	parentNode.appendChild( span );
		*/
        var p = targetDocument.getElementById("digiLink");
        p.appendChild( a );
    }
    
}
