/*+-------------+----------------------------------------------------------*
 *|        /\   |   University of Bonn                                     *
 *|       |  |  |     Department of Geography                              *
 *|      _|  |_ |     Chair of Cartography                                 *
 *|    _/      \|                                                          *
 *|___|         |                                                          *
 *|             |     Meckenheimer Allee 172                               *
 *|             |     D-53115 Bonn, Germany                                *
 *+-------------+----------------------------------------------------------*/
/**
 * <p><b>Title: Styles</b></p>
 * <p><b>Description:</b> Styles and Helper Functions </p>
 *
 * <p><b>Copyright:</b> Copyright (c) 2008</p>
 * <p><b>Institution:</b> University of Bonn, Department of Geography</p>
 * @author Pascal Neis, neis@geographie.uni-bonn.de
 * @version 1.0 2008-07-11
 */

//Helper
//**********************************************************
	function lonlatzoom(lonlat,zoomvalue){
		lonlat.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
		markerPosition.clearMarkers();
		markerPosition.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(lonlat.lon,lonlat.lat),markerIconPosition));
		
		if(zoomvalue == null)
			zoomValue = map.zoom;
		
		map.setCenter(lonlat, zoomvalue);
	}
	
	function clearMarkers(markerName){
		if(markerName == 'POIs'){
			markersPOI.clearMarkers();
			document.getElementById("Clear1").disabled=true;
			document.getElementById("Clear2").disabled=true;
		}
	}
	
	function markerzoom(markername,lonlat,zoomvalue,freeFormGeocode){
		lonlat.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));

		if(markername == 'start'){
			if(startExists){  //markerVerschieben, wenn schon vorhanden noch nicht implementiert
				
				for(var i= 0; i<lyRouteFeatures.features.length; i++){
					if(lyRouteFeatures.features[i].name == 'Start'){
						lyMarkers.removeMarker(lyRouteFeatures.features[i].marker);
						lyRouteFeatures.removeFeatures([lyRouteFeatures.features[i]]);//lyRouteFeatures.features[i]
					}	
				}
				var edgeLonLat = getEdgeLonLat(lonlat);
				bounds = new OpenLayers.Bounds(lonlat.lon,lonlat.lat,edgeLonLat.lon,edgeLonLat.lat);
				startPoint = new OpenLayers.Feature.Vector(bounds.toGeometry(),{icon:markerIconStart});
				startPoint.bounds = bounds;
				startPoint.lonlat = new OpenLayers.LonLat(lonlat.lon,lonlat.lat);
				startPoint.marker = new OpenLayers.Marker(lonlat, startPoint.data.icon.clone());
				lyMarkers.addMarker(startPoint.marker);
				startPoint.style = startPointStyle;
				startPoint.name = 'Start';
				startPoint.myId ='';
				lyRouteFeatures.addFeatures([startPoint]);
				startExists = true;
			}
			if ((lyRouteFeatures.features.length == 0)||!(startExists)){
				
				var edgeLonLat = getEdgeLonLat(lonlat);
				bounds = new OpenLayers.Bounds(lonlat.lon,lonlat.lat,edgeLonLat.lon,edgeLonLat.lat);
				startPoint = new OpenLayers.Feature.Vector(bounds.toGeometry(),{icon:markerIconStart});
				startPoint.bounds = bounds;
				startPoint.lonlat = new OpenLayers.LonLat(lonlat.lon,lonlat.lat);
				startPoint.marker = new OpenLayers.Marker(lonlat, startPoint.data.icon.clone());
				lyMarkers.addMarker(startPoint.marker);
				startPoint.style = startPointStyle;
				startPoint.name = 'Start';
				startPoint.myId ='';
				lyRouteFeatures.addFeatures([startPoint]);
				startExists = true;
			}
		}
		else if(markername == 'end'){
			if(endExists){  //markerVerschieben, wenn schon vorhanden noch nicht implementiert
				for(var i= 0; i<lyRouteFeatures.features.length; i++){
					if(lyRouteFeatures.features[i].name == 'End'){
						lyMarkers.removeMarker(lyRouteFeatures.features[i].marker);
						lyRouteFeatures.removeFeatures([lyRouteFeatures.features[i]]);//lyRouteFeatures.features[i]
					}	
				}
				var edgeLonLat = getEdgeLonLat(lonlat);
				bounds = new OpenLayers.Bounds(lonlat.lon,lonlat.lat,edgeLonLat.lon,edgeLonLat.lat);
				endPoint = new OpenLayers.Feature.Vector(bounds.toGeometry(),{icon:markerIconEnd});
				endPoint.bounds = bounds;
				endPoint.lonlat = new OpenLayers.LonLat(lonlat.lon,lonlat.lat);
				endPoint.marker = new OpenLayers.Marker(lonlat, endPoint.data.icon.clone());
				lyMarkers.addMarker(endPoint.marker);
				endPoint.style = endPointStyle;
				endPoint.name = 'End';
				endPoint.myId ='';
				lyRouteFeatures.addFeatures([endPoint]);
				endExists = true;
			}
			if ((lyRouteFeatures.features.length == 0)||!(endExists)){
				var edgeLonLat = getEdgeLonLat(lonlat);
				bounds = new OpenLayers.Bounds(lonlat.lon,lonlat.lat,edgeLonLat.lon,edgeLonLat.lat);
				endPoint = new OpenLayers.Feature.Vector(bounds.toGeometry(),{icon:markerIconEnd});
				endPoint.bounds = bounds;
				endPoint.lonlat = new OpenLayers.LonLat(lonlat.lon,lonlat.lat);
				endPoint.marker = new OpenLayers.Marker(lonlat, endPoint.data.icon.clone());
				lyMarkers.addMarker(endPoint.marker);
				endPoint.style = endPointStyle;
				endPoint.name = 'End';
				endPoint.myId ='';
				lyRouteFeatures.addFeatures([endPoint]);
				endExists = true;
			}
		}
		else if(markername == 'position'){
			markerPosition.clearMarkers();
			markerPosition.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(lonlat.lon,lonlat.lat),markerIconPosition));
			
		}
		else if(contains(markername,'tfViaSearch')){
			var edgeLonLat = getEdgeLonLat(lonlat);
			var tmp = markername.split('h');
			var id = tmp[1];
			bloop: for (var i = 0; i<viaStorage.length; i++){
	        		if (viaStorage[i].myId==id){
	        			thisViaExists = true;
	        			break bloop;
	        		}
	        }
			if(thisViaExists){
				for(var i= 0; i<lyRouteFeatures.features.length; i++){
					if(lyRouteFeatures.features[i].myId == id){
						lyMarkers.removeMarker(lyRouteFeatures.features[i].marker);
						lyRouteFeatures.removeFeatures([lyRouteFeatures.features[i]]);//lyRouteFeatures.features[i]
					}	
				}
				for(var i = 0; i<viaStorage.length; i++){
					if(viaStorage[i].myId==id){
						viaStorage.splice(i,1);
					}
				}
				bounds = new OpenLayers.Bounds(lonlat.lon,lonlat.lat,edgeLonLat.lon,edgeLonLat.lat);
				var viaPoint = new OpenLayers.Feature.Vector(bounds.toGeometry(),{icon:markerIconVia});
				viaPoint.bounds = bounds;
				viaPoint.style = viaPointStyle;
				viaPoint.name = 'Via';
				viaPoint.myId = id;
				viaPoint.lonlat = new OpenLayers.LonLat(lonlat.lon,lonlat.lat);
				viaPoint.marker = new OpenLayers.Marker(lonlat, viaPoint.data.icon.clone(), id, id);
				lyRouteFeatures.addFeatures([viaPoint]);
				lyMarkers.addMarker(viaPoint.marker);

				viaStorage.push(viaPoint);
			}
			
			if ((viaStorage.length == 0)||!(thisViaExists)){
                bounds = new OpenLayers.Bounds(lonlat.lon,lonlat.lat,edgeLonLat.lon,edgeLonLat.lat);
				var viaPoint = new OpenLayers.Feature.Vector(bounds.toGeometry(),{icon:markerIconVia});
				viaPoint.bounds = bounds;
				viaPoint.style = viaPointStyle;
				viaPoint.name = 'Via';
				viaPoint.myId = id;
				viaPoint.lonlat = new OpenLayers.LonLat(lonlat.lon,lonlat.lat);
				viaPoint.marker = new OpenLayers.Marker(lonlat, viaPoint.data.icon.clone(), id, id);
				lyRouteFeatures.addFeatures([viaPoint]);
				lyMarkers.addMarker(viaPoint.marker);

				viaStorage.push(viaPoint);
				
            }
		}
		map.setCenter(lonlat, zoomvalue);
		lyRouteFeatures.redraw();
	}
	
	function changeColorRoute(){
	
		if(route_style.strokeColor == "#000000"){
			vectorLayerRoute.style.strokeColor = "#0000FF";
			vectorLayerRoute.style.strokeOpacity = 0.5;
		}
		else{
			vectorLayerRoute.style.strokeColor = "#000000";
			vectorLayerRoute.style.strokeOpacity = 0.7;		
		}

		if(vectorLayerRoute.features.length > 0){
			for (var i = 0; i < vectorLayerRoute.features.length; i++) {
				vectorLayerRoute.features[i].style = vectorLayerRoute.style;
			}
		}

		vectorLayerRoute.redraw();

		//alert(vectorLayerRoute.style.strokeColor);
		//alert(route_style.strokeColor+" "+route_style.strokeOpacity);
	}

//**********************************************************

//Definitionen der verwendeten Styles
//**********************************************************
            var route_style = { //Style der Route
                strokeColor: "#0000FF",
                strokeOpacity: 0.5,
                strokeWidth: 4
            };
			var avoidareas_style = { //Style der Avoid Area
                strokeColor: "#FF0000",
				strokeOpacity: 0.5,
				pointRadius: 5,
				fillColor: "#FF0000",
                fillOpacity: 0.5
            };
			var analysearea_style = { //Style des Analyse Polygon
                strokeColor: "#00CC99",
				strokeOpacity: 0.5,
				pointRadius: 5,
				fillColor: "#00CC99",
                fillOpacity: 0.5
            };
			
			var startPointStyle = {fillColor: "#ee9900",
			        fillOpacity: 0, 
			        hoverFillColor: "white",
			        hoverFillOpacity: 0.8,
			        strokeColor: "#ee9900",
			        strokeOpacity: 0,
			        strokeWidth: 1,
			        strokeLinecap: "round",
			        hoverStrokeColor: "red",
			        hoverStrokeOpacity: 1,
			        hoverStrokeWidth: 0.2,
			        pointRadius: 16,
			        hoverPointRadius: 1,
			        hoverPointUnit: "%",
			        pointerEvents: "visiblePainted",
			        cursor: "",
			        externalGraphic: "img/arrow-green.png"
			       };
			
			var endPointStyle = {fillColor: "#ee9900",
			        fillOpacity: 0, 
			        hoverFillColor: "white",
			        hoverFillOpacity: 0.8,
			        strokeColor: "#ee9900",
			        strokeOpacity: 0,
			        strokeWidth: 1,
			        strokeLinecap: "round",
			        hoverStrokeColor: "red",
			        hoverStrokeOpacity: 1,
			        hoverStrokeWidth: 0.2,
			        pointRadius: 16,
			        hoverPointRadius: 1,
			        hoverPointUnit: "%",
			        pointerEvents: "visiblePainted",
			        cursor: "",
			        externalGraphic: "img/arrow-finish.png"
			       };
			var viaPointStyle = {fillColor: "#ee9900",
			        fillOpacity: 0, 
			        hoverFillColor: "white",
			        hoverFillOpacity: 0.8,
			        strokeColor: "#ee9900",
			        strokeOpacity: 0,
			        strokeWidth: 1,
			        strokeLinecap: "round",
			        hoverStrokeColor: "red",
			        hoverStrokeOpacity: 1,
			        hoverStrokeWidth: 0.2,
			        pointRadius: 16,
			        hoverPointRadius: 1,
			        hoverPointUnit: "%",
			        pointerEvents: "visiblePainted",
			        cursor: "",
			        externalGraphic: "img/arrow-orange.png"	
			       };
        
//**********************************************************

//Variables ...
//WMS
//var urlWMS = "http://localhost:8080/geoserver/wms";
//var layerlistWMS = 'osm:world,osm:waterways,osm:naturals,osm:naturalssmall,osm:roads,osm:roadssmall,osm:points,osm:buildings,osm:places';

//var osmWorld = 'osm:world';
//var osmWaterways = 'osm:waterways080424';
//var osmNatural = 'osm:naturals080424,osm:naturals080416small';
//var osmRoads = 'osm:roads080424,osm:roads080410small';
//var osmPoints = 'osm:points080424';
//var osmBuildings = 'osm:buildings080424';
//var osmPlaces = 'osm:places080424';

//GetFeatureInfo / WFS
//var urlGetFeaturInfo = "http://localhost:8080/geoserver/wms";
//var urlGetFeaturInfo = "http://openls.giub.uni-bonn.de/geoserver-osm/wms";
var urlGetFeaturInfo = "http://129.206.229.158/cached/osm";
//var layerlistGetFeatureInfo = "osm:naturals,osm:waterways,osm:roads,osm:points,osm:buildings";
var layerlistGetFeatureInfo = "osm_auto:naturals,osm_auto:waterways,osm_auto:roads,osm_auto:points,osm_auto:buildings";

//TMC
//var urlTMC = 'http://karto.giub.uni-bonn.de:8080/WFSTMC/wms';
//var urlTMC = 'http://services.giub.uni-bonn.de/tmc/wms';
var urlTMC = 'http://koenigstuhl.geog.uni-heidelberg.de/tmc/wms';
//var layerlistTMC = 'tmc:OSMRoads,tmc:Points';
var layerlistTMC = 'tmc:tmcpoints';

//ORS TileCache
//var urlORS = "http://openls.giub.uni-bonn.de/ors-tilecache/tilecache.py?";
var urlORS = "http://129.206.229.158/cached/osm?";

var hs_name = "Hillshade of Germany";
//var hs_url = "http://services.giub.uni-bonn.de/hillshade?";
var hs_url = "http://129.206.229.158/cached/hillshade?";
var hs_options = {layers: 'europe_wms:hs_srtm_europa',srs: 'EPSG:900913', format: 'image/JPEG', transparent: 'true'};