<%-- $HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/apps/igeoportal-standard/branches/2.5_testing/modules/gazetteer/gaz_take.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.model.feature.Feature" %>
<%@ page import="org.deegree.model.feature.FeatureCollection" %>
<%@ page import="org.deegree.model.spatialschema.Envelope" %>
<%@ page import="org.deegree.model.spatialschema.Geometry" %>
<%@ page import="org.deegree.model.spatialschema.GeometryFactory"%>
<%@ page import="org.deegree.model.spatialschema.Point"%>
<%@ page import="org.deegree.model.spatialschema.Position"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>  	
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta name="author" content="Judit Mays, lat/lon GmbH">
        <meta name="KeyWords" content="deegree iGeoPortal lat/lon" />
        <title>WFS Gazetteer</title>
        <link href="../../css/deegree.css" rel="stylesheet" type="text/css" />
		
		<%
	    FeatureCollection  fc = (FeatureCollection)request.getAttribute( "RESULT" );
		Feature feature = null;
		if ( fc.size() == 0 ) {
		    System.out.println( "WARNING: feature is null" );
		} else {
		    feature = fc.getFeature(0);
			//System.out.println( "DEBUG: feature = " + feature );
		}
		%>
		
	    <script LANGUAGE='JavaScript1.2' TYPE='text/javascript' src='./javascript/event.js'></script>
	    <script LANGUAGE='JavaScript1.2' TYPE='text/javascript' src='./javascript/envelope.js'></script>
	    <script language="JavaScript1.2">
		<!--
		
		function performOnLoad() {
			parent.ifdocument = document;			
			performZoomTo();
		}
		
		function close() {
			parent.window.close();
		}
				
		function performZoomTo() {
		<%
			if ( feature != null ) {
			    Geometry[] geoms = feature.getGeometryPropertyValues();
                
                if ( geoms != null && geoms.length > 0 ){
                    // FIXME calculate combining bbox for all geoms
                    Envelope env = null;
                    
                    final double buffer = 50.0; // HINT: buffer value might need adjustment
                    if ( geoms[0] instanceof Point ) {
                        Point p = (Point)geoms[0];
                        Position pos = p.getPosition();
                        env = GeometryFactory.createEnvelope( pos.getX()-buffer, pos.getY()-buffer,
                                                              pos.getX()+buffer, pos.getY()+buffer,
                                                              p.getCoordinateSystem() );
                    } else {
                        env = geoms[0].getEnvelope();    
                    }
            
					if ( env == null ) {
					    out.println( "alert( 'envelope is null' )" );
					} else {
					    //System.out.println("before: " + env );
					    // value "75" might need to be adjusted
					    if( env.getWidth() < 75 || env.getHeight() < 75 ){ 
							// increase envelope because it is too small
							// factor "20" might need to be adjusted
							final double expansionFactor = 20;
							env = env.getBuffer( expansionFactor ); 
					    }
						//System.out.println("after: " + env );

						double minx = env.getMin().getX();
						double miny = env.getMin().getY();
						double maxx = env.getMax().getX();
						double maxy = env.getMax().getY();
						// correct box width/height to the right aspect ratio
						double factor = 1.0; //its the aspect ratio of the map. adapt it if needed.
						if ( env.getWidth()  > ( env.getHeight() * factor ) ) {
							double d2 = (env.getWidth()/2d)/factor;
							double mid = miny + env.getHeight()/2d;
							miny = mid - d2;
							maxy = mid + d2;
						} else {
							double d2 = ( env.getHeight()/2d ) * factor;
							double mid = minx + env.getWidth()/2d;
							minx = mid - d2;
							maxx = mid + d2;
						}
						
						out.println( "var env = new Envelope( " +  minx + "," + miny  + "," + maxx + "," + maxy +  " );" );
						out.println( "var event = new Event( '', 'BBOX', env );" );

                        // If you are running the gazetteer module with gazmodule.html and gazwindow.jsp 
                        // (running the module in its own window)
                        // then you find the controller at "parent.parent.opener.parent.controller".
                        // If you are running the gazetteer module with gazmodule_mw.html 
                        // (running the module in the portals main window)
                        // then you find the controller at "parent.parent.parent.controller".
						// (also including hack to force update of scale switcher)
						
                        out.println( "if ( parent.parent.opener != null ) {" );
							out.println( "parent.parent.opener.parent.controller.actionPerformed( event );" );
							out.println( "parent.parent.opener.parent.controller.vScaleSwitcher.getActualScaleValue();" );
						out.println( "} else {" );                       
                        	out.println( "parent.parent.parent.controller.actionPerformed( event );" );
							out.println( "parent.parent.parent.controller.vScaleSwitcher.getActualScaleValue();" );
                        out.println( "}" );
					}
	            } else {
	            	out.println( "alert( 'The chosen entity has no geometry.' );" );
	            }
			} else {
				out.println( "alert( 'No matches found!');" );
			}
		%>
		}
				
		//-->
		</script>	
	</head>
	<body onload="performOnLoad();">
		<form action="" id="form" method="post"></form>
	</body>
</html>
