<%-- $HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/apps/igeoportal-standard/branches/2.5_testing/modules/mapeditor/mapeditor.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
--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="de.latlon.tlda.model.GeomReference"%>
<%
    GeomReference obj = (GeomReference)session.getAttribute( "GEOMREF" );
%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>deegree igeoPortal Editor</title>
		<link href="../../css/deegree.css" rel="stylesheet" type="text/css" />
        <script language="JavaScript1.2" type="text/javascript" src="./../../javascript/feature.js"></script>
		<script language="JavaScript1.2" type="text/javascript" src="./../../javascript/exception.js"></script>
		<script language="JavaScript1.2" type="text/javascript" src="./../../javascript/utils.js"></script>
		<script language="JavaScript1.2" type="text/javascript" src="./../../javascript/json2.js"></script>
        <script language="JavaScript1.2" type="text/javascript" src="./../../javascript/request_handler.js"></script>
        <script language="JavaScript1.2" type="text/javascript" src="./../../javascript/style.js"></script>
        <script language="JavaScript1.2" type="text/javascript" src="./../../javascript/geometryutils.js"></script>
        <script language="JavaScript1.2" type="text/javascript" src="./../../javascript/geometries.js"></script>
        <script language="JavaScript1.2" type="text/javascript" src="./../../javascript/geometryfactory.js"></script>
        <script language="JavaScript1.2" type="text/javascript" src="./../../javascript/wktadapter.js"></script>
        <script language="JavaScript1.2" type="text/javascript" src="./../../javascript/event.js"></script>
        <script language="JavaScript1.2" type="text/javascript" src="./mapeditor.js"></script>
		<script language="JavaScript1.2" type="text/javascript">
        <!--
        
        var baseURL = "http://localhost:8080/geoeditor/ajax";
        var loadInputGUI;
        
		function register() {
            if ( parent.controller == null ) {
                parent.controller = new parent.Controller();
                parent.controller.init();
            }
        }

        function initEditor() {
            parent.controller.initMapEditor( document );
<%
    if (  obj != null && obj.getGeometry() == null  ) {
%>            
            alert( "Eine Georeferenz mit der Kennung: <%=obj.getObjectNo() %>  ist noch nicht vorhanden!" );
<%
    } else {
%>     
            var crs = getSelectedValue( "CRS" );       
            parent.controller.vMapEditor.loadPreview(  '<%=obj.getGeometry() %>', crs  );
<%
    }
%>
        }         
        
        function validateCoordinate( inp, length ) {
            if ( inp.value == null || inp.value.length < length ) {
    			var crs = getSelectedValue( "CRS" );
                alert( "Bei Auswahl des CRS: " + crs + " müssen Sie " + length + "-stellige Koordinaten angeben!" );
                inp.focus();
                inp.value = "";
            }
        }
                
        /**
        * loads a GUI depending on selected geometry type for editing geometry coordinates directly 
        */        
        function loadInputGUI(action) {           
            parent.controller.vMapEditor.reset();
            parent.controller.vMapEditor.loadInputGUI( action, getSelectedValue( "CRS" ) );
        }
        
        /**
        * loads a preview of the geometry that currently is editied through the text fields
        */
        function preview() {
            var wkt = createWKT();
			var crs = getSelectedValue( "CRS" );       
        	parent.controller.vMapEditor.loadPreview( wkt, crs  );
        }       
        
        /**
        * sends a geometry to the sever to be persited
        */
        function store() {
            var wkt = createWKT();       
            var centroid = 'POINT(' + getInputValue( 'xCenterCoord' ) + ' ' + getInputValue( 'yCenterCoord' ) + ')';    
            var crs = getSelectedValue( "CRS" );
            var fc = parent.controller.mapModel.getFeatureCollection( "digitizing" );
            parent.controller.vMapEditor.storeFeature( fc.getFeature( 0 ), crs, wkt  );
        }
        
        /**
        * creates a WKT geometry from coordinates read from editor formular
        */
        function createWKT() {
            var wkt = null;
            if ( getInputValue( 'xCoord' ) ) {
                wkt = 'POINT(' + getInputValue( 'xCoord' ) + ' ' + getInputValue( 'yCoord' ) + ')';
            } else if ( getInputValue( 'minxCoord' ) ) {
                wkt = 'POLYGON((' +  getInputValue( 'minxCoord' ) + ' ' +  getInputValue( 'minyCoord' ) + ',' +
                                                    getInputValue( 'minxCoord' ) + ' ' +  getInputValue( 'maxyCoord' ) + ',' +
                                                    getInputValue( 'maxxCoord' ) + ' ' +  getInputValue( 'maxyCoord' ) + ',' +
                                                    getInputValue( 'maxxCoord' ) + ' ' +  getInputValue( 'minyCoord' ) + ',' +
                                                    getInputValue( 'minxCoord' ) + ' ' +  getInputValue( 'minyCoord' ) + '))';
            } else if ( getInputValue( 'TA' ) ) {
               wkt = createWKTPolygonFromList( getInputValue( 'TA' ) );
            }
            return wkt;
        }
        
         /**
        * creates a WKT Polygon from the coordinate list read from textarea with ID 'TA'
        */
        function createWKTPolygonFromList( list ) {
            var coords = list.split( ',' );
            var wkt = "POLYGON((";
            for (var i = 0; i <  coords.length; i += 2) {
                wkt += ( coords[i] + ' ' + coords[i+1] );
                if ( i < coords.length-2 ) {
                    wkt += ',';
                }
            }
            wkt += '))';
            return wkt;
        }
                		        
        // -->
		</script>
	</head>    
	<body onload="register(); initEditor(); " class="pMapEditor">
<%
    if ( obj != null ) {
%>        
		<div style="position:absolute; left:0px; top:40px; width:220px; height:350 border:#000000 2px solid; ">
			<div id="COORDS" style="position:absolute; left:10px; right:10px; width=230px; color:#EEEEEE">            
				Punktkoordinate:
                <br><br>
                x: <input id="xCoord" class="pMapEditorCoords"  type="text" maxlength="7" value="" onblur="validateCoordinate( this, 7 )"/>
                <br><br>
                y: <input id="yCoord" class="pMapEditorCoords"  type="text" maxlength="7" value="" onblur="validateCoordinate( this, 7 )"/>
            </div>
            <div style="position:absolute; top:310px; left:10px; right:10px; width=230px; color:#EEEEEE">
				<div style="position:absolute; top:0px; width:215px">CRS ausw&auml;hlen:</div>
				<select id="CRS" size="1" style="position:absolute; top:22px; width:225px;" >
					<option value="EPSG:25832">ETRS89,UTM Zone 32</option>
					<option value="EPSG:25833">ETRS, UTM Zone 33</option>
					<option value="EPSG:4258">ETRS89,geographisch</option>
					<option value="EPSG:4746">PD83 geographisch</option>
					<option value="EPSG:3396">PD83, GK3-grad, 3.Streifen</option>
					<option value="EPSG:3397">PD83, GK 3-grad, 4.Steifen</option>
					<option value="EPSG:4326">WGS84,geographisch</option>
					<option value="EPSG:31467">DHDN, GK-3-grad, Zone 3</option>
					<option value="EPSG:31468" selected>DHDN, GK 3-grad, Zone 4</option>
				</select>
    			<div style="position:absolute; left:0px; width:220px; top:50px;">
    				<input id="cbPoint" type="radio" name="geomType" value="POINT" checked  onclick="loadInputGUI( 'loadPointGUI' )">Punkt</input><br>
    				<input id="cbBox" type="radio" name="geomType" value="BOX" onclick="loadInputGUI( 'loadBoxGUI' ); parent.controller.mode = 'mapeditor:drawBox' ">Box/Rechteck</input><br>
    				<input id="cbPolygon" type="radio" name="geomType" value="POLYGON" onclick="loadInputGUI( 'loadPolygonGUI' )">Polygon</input><br>
    			</div>
    			<div style="position:absolute; left:0px; width:110px; top:120px; border:#000000 1px solid; background-color:#3a4068;">
    				<a href="javascript:preview()" style="color:#EEEEEE; padding-left:20px; text-decoration:none; ">Vorschau</a>
    			</div>
    			<div style="position:absolute; left:120px; width:110px; top:120px; border:#000000 1px solid; background-color:#3a4068;">
    				<a href="javascript:store()" style="color:#EEEEEE; padding-left:20px; text-decoration:none;">Speichern</a>
    			</div>
            </div>
		</div>
<%
    } else {
%>
        <div style="position:absolute; left:0px; top:40px; width:230px; height:350 border:#000000 2px solid; font-weight:bold">
            Sie müssen beim Aufruf des GeoEditors eine Objekt-Kennung mitgeben!            
            <br>
            <br>
            Beispiel: 
            <br>
            http://localhost:8080/geoeditor/welcome.jsp?object=ABC1234
        </div>
<%    
    }
%>        
	</body>
</html>