<%-- $HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/apps/igeoportal-standard/branches/2.5_testing/modules/digitizer/digitizer_window.jsp $
 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
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
<%@ page import="org.deegree.framework.util.CharsetUtils" %>
<%@ page import="org.deegree.enterprise.WebUtils" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
    int width = Integer.parseInt( request.getParameter( "mapWidth" ) );
    int height = Integer.parseInt( request.getParameter( "mapHeight" ) );
    
    String geomTypes = request.getParameter( "geomTypes" );
    String defaultGeomType = geomTypes.split( ";" )[0]; 
    
    boolean validateGeom = Boolean.valueOf( request.getParameter( "validateGeometry" ) ) ;
              
    //UT: TODO check parameter availability
    String[] formLabels = request.getParameter( "formLabels" ).split( ";" );
    String[] formTemplates = request.getParameter( "formTemplates" ).split( ";" );
    String[] featureTypes = request.getParameter( "featureTypes" ).split( ";" );
%>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="author" content="lat/lon GmbH" />
        <meta name="KeyWords" content="deegree OGC iGeoPortal lat/lon" />
        <meta name="audience" content="" />
        <title>Digitizer</title>
        <link href="./../../css/deegree.css" rel="stylesheet" type="text/css" />
        <script language="JavaScript1.2" type="text/javascript" src="digitizer.js"></script>
        <script language="JavaScript1.2" type="text/javascript" src="geometries.js"></script>
        <script language="JavaScript1.2" type="text/javascript" src="digitizer_utils.js"></script>
        <script language="JavaScript1.2" type="text/javascript" src="../../mapmodel.js"></script>
        <script language="JavaScript1.2" type="text/javascript"	src="../../wmsrequestfactory.js"></script>
        <script language="JavaScript1.2" type="text/javascript"	src="../../javascript/geotransform.js"></script>
        <script language="JavaScript1.2" type="text/javascript" src="../../javascript/envelope.js"></script>
        <script language="JavaScript1.2" type="text/javascript" src="../../javascript/rpc.js"></script>
        <script language="JavaScript1.2" type="text/javascript" src="../../javascript/layerutils.js"></script>
        <script language="JavaScript1.2" type="text/javascript" src="../../javascript/dojo-1.4.1/dojo/dojo.js"></script>
            
        <script type="text/javascript">
             dojo.require("dojox.gfx");
        </script>
        <script language="JavaScript1.2" type="text/javascript">
        <!-- //
	
        var templateDoc = null;

        var geomCheckerProxy = null;

        var geomType = '<%= defaultGeomType %>';
	
        var digitizer;
	
        var geoTrans;
	
        var mapDocument;
	
        var isNav = true;
	
        var geometry = null;
	
        var crs = opener.parent.controller.mapModel.getSrs();

        var mapLeft = <%= request.getParameter( "mapLeft" ) %>;

        var mapTop = 0; //<!--  don't do this request.getParameter( "mapTop" ) -->
	
        var isFirstPoint = true;
        
        var selectedTemplate = "<%= formTemplates[0] %>"; // default value
        
        var selectedFeatureType = "<%= featureTypes[0] %>"; // default value
        
        var formToFeatureTypeMap = new Array();
        <%
        for (int i = 0; i < formTemplates.length; i ++ ) {
            // formToFeatureTypeMap["nameOfFormTemplate"] = "nameOfFeatureType";
            out.println( "formToFeatureTypeMap[\"" + formTemplates[i] + "\"] = \"" + featureTypes[i] + "\";" );
        }
        %>
        
        function swapTemplate( list ) {
            // change template in iframe
            templatePath = list.value;
            var im = document.getElementById( "templateWindow" );
            im.src = templatePath;
            
            // store chosen template name in variable
            var tmp = templatePath.split( "<%=WebUtils.getAbsoluteContextPath(request)%>/" ); 
            // second array item is the template name as given in the module configuration
            selectedTemplate = tmp[1];
            
            // store corresponding featureType in variable
            selectedFeatureType = formToFeatureTypeMap[selectedTemplate];
        }
        
        function init(){
            geomCheckerProxy.initializeModule();
            var col = document.getElementById( 'leftCol' );
            col.setAttribute( 'width', mapLeft + 'px');
            initMap();
            paintMap();
            initDigitizer();
            initEventHandling();
        }
        
        function sendRequest(){
            if( geometry == null ){
                alert( 'Please digitize a geometry.' );
                return;
            }
            
            // for details on params see digitizer_rpcRequest_definition.xml
            // set first rpc param (param[0])
            var params = "<param><value><string><![CDATA[" + selectedFeatureType + "]]></string></value></param>";
            // set second and more rpc params (param[1] .. param[n])
            if ( templateDoc != null ){
                params += templateDoc.getRPCFragment();
            }
            
            var rpc = createMethodCall( 'dig:digitizeFeature', params, '<%= CharsetUtils.getSystemCharset() %>' );
            document.getElementById( 'hidden_dig_rpc' ).value = rpc;
            // TODO: comment out the next line (the alert) ...
            alert( "The digizer module needs to be configured properly.\nPlease refer to the documentation for details.\nThe WFS-T request will be aborted." );
            // TODO: ... and comment in the following line ( document.submit() )
            //document.getElementById( 'dig_form' ).submit();
            digitizer.clear();
            paintProgress();
            
            // TODO: comment out the next line ( window.close(); ) 
            window.close();
        }	
	
        /*
         * paint a new layer into the window, containing a progress bar.
         */
        function paintProgress() {
        	var layer = createLayer( document, "progress", "progress", 350, 250, 230, 183, 1000, null, true );
        	createImage( "../../images/progress.gif", document, layer, 230 , 183, "progressIMG" );
        	document.getElementsByTagName( 'body' )[0].appendChild( layer );
        }
	
        function initDigitizer(){
            var lineColor = "<%= request.getParameter("lineColor") %>";
            digitizer = new Digitizer( <%= width %>, <%= height %>, mapLeft, 0, lineColor );
            digitizer.setDocument(document);
            digitizer.init();
        }
	
        function paintMap() {
            var mm = opener.parent.controller.mapModel;

            var bb = '<%=request.getParameter( "bbox" )%>'.split( ',' );
            var bbox = new Envelope( parseFloat( bb[0] ), parseFloat( bb[1] ), 
    					             parseFloat( bb[2] ), parseFloat( bb[3] ) );
            
            var env = ensureAspectRatio( bbox, <%= width %>, <%= height %> );
            var llist = mm.getLayerList();
            var lGroups = mm.getLayerList().getLayerGroups();
		
            var localMapModel = new MapModel( llist, mm.getSrs(), env, <%=width %>, <%=height %> );
            var factory = new WMSRequestFactory();
		
            var sid = null;
            if( opener.parent.controller.vSessionKeeper != null ){
                sid = opener.parent.controller.vSessionKeeper.id;
            }
		
            for( var i = 0; i < lGroups.length; ++i ){
                var src = factory.createGetMapRequest( lGroups[i], localMapModel, sid );
                document.getElementById( "image" + i ).src = src;
            }
            //UT deviating from normal FIX IT: +mapLeft and top, if you will
            geoTrans = new GeoTransform( env.minx, env.miny, env.maxx, env.maxy, 0 + mapLeft, 0, <%= width %> + mapLeft, <%= height %> );
        }
	
        function doCheckGeometry(){
            <%
            if( validateGeom ){
            %>
                updateMesgField( 'Checking geometry...', 'background-color:#77FF77');
                geomCheckerProxy.checkGeometry( geometry );	
            <%
            } else {
                out.println("//Nothing to validate");
            }
            %>
        }
        
        // -->
        </script>
    </head>
    <body class="pDigitizerWindow" onload="init();" onunload="digitizer.clear();" marginheight="0" marginwidth="0">
        <% if( validateGeom ){ %>
            <iframe src="geom_checker_proxy.jsp" id="geomCheckerProxy" height="0" width="0" frameborder="0" ></iframe>
        <% } %>
        <form action="<%= WebUtils.getAbsoluteContextPath(request)%>/control" id="dig_form" method="post">
            <input id="hidden_dig_rpc" name="rpc" type="hidden" value="" />
        </form>
        <table border="0" width="100%">
            <tr>
                <td width="150px" id="leftCol" >
                    <%			
                    if( formLabels.length > 1 ){
                    %>
                        <select id="templateChooser" onchange="swapTemplate( this )">
                    <%
                        for( int i = 0; i < formLabels.length; i++ ){
                    %>
                            <option value="<%= WebUtils.getAbsoluteContextPath(request) + "/" +formTemplates[i] %>"> <%= formLabels[i] %></option>
                    <%
                        }
                    %>
                        </select>
                    <%
                    } else {
                    %>
                        <p><b><%=formLabels[0] %></b></p>
                    <%  
                    }
                    %>
                </td>
            </tr>
            <tr>
                <td>
                    <iframe id="templateWindow" src="<%= WebUtils.getAbsoluteContextPath(request) + "/" + formTemplates[0] %>" frameborder="0" width="100%" height="450px"></iframe>
                </td>
            </tr>
            <tr>
                <td>
                	<jsp:include flush="true" page="geom_control.jsp?geomTypes=<%= geomTypes %>"></jsp:include>
                </td>
            </tr>
        </table>
    </body>
</html>
