var lat = gup( 'lat' )
var lon = gup( 'lon' )
var zoom = gup( 'zoom' )

if( lat == "" )
{
  lat=49.4
  lon=11.55
  zoom=12
}
else
{
	lat=parseFloat(lat)
	lon=parseFloat(lon)
	zoom=parseInt(zoom)
}        

var map; //complex object of type OpenLayers.Map

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
	return "";
  else
	return results[1];
}

// Use www.loki.com to locate the user
function lokiMe() {
	if (LokiAPI.isInstalled()) { 
		var loki = LokiAPI();
		loki.onSuccess = function(location) {
			var lonLat = new OpenLayers.LonLat(location.longitude, location.latitude).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());

			map.setCenter (lonLat, 12);}
		loki.onFailure = function(error) {alert('Loki error: '+ error + ' - More Information about this geolocation feature at www.loki.com.')} 
		loki.setKey('broadbox.de');
		loki.requestLocation(true,loki.NO_STREET_ADDRESS_LOOKUP); 
	}
}

$(document).ready(function(){	
	map = new OpenLayers.Map ("map", {
		controls:[
			new OpenLayers.Control.Navigation(),
			new OpenLayers.Control.PanZoomBar(),
			new OpenLayers.Control.Attribution(),
			new OpenLayers.Control.Permalink(),
			new OpenLayers.Control.MousePosition(),
			new OpenLayers.Control.KeyboardDefaults(),
			new OpenLayers.Control.ScaleLine()
			],
			maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
            maxResolution: 156543.0399,
            numZoomLevels: 19,
            units: 'm',
            projection: new OpenLayers.Projection("EPSG:900913"),
            displayProjection: new OpenLayers.Projection("EPSG:4326")
	} );


	// Define the relief-layer...
	var hills = new OpenLayers.Layer.OSM("Relief", "http://topo.geofabrik.de/relief/${z}/${x}/${y}.png",
                {minZoomLevel: 8, maxZoomLevel: 15, isBaseLayer:true, transitionEffect: null, attribution: "DEM by <a href='http://srtm.csi.cgiar.org'>CIAT</a>" });

	// ...and the OSMC overlay
	var trails = new OpenLayers.Layer.OSM("Reitkarte", "http://topo.geofabrik.de/trails/${z}/${x}/${y}.png",
		{minZoomLevel: 8, maxZoomLevel: 15, isBaseLayer:false, transitionEffect: null, attribution: "Data by <a href='http://openstreetmap.org/'>OSM</a>"});

	// ...and finally add them to the map object.
	map.addLayers([hills,trails]);

	var lonLat = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());

	if (!map.getCenter())
	  map.setCenter (lonLat, zoom);

	// Add  Loki button to the Openlayers map object
    $("#map_OpenLayers_ViewPort").append("<div id='PhotosmControls' class='olControlSelect' style='position: absolute; left: 8px; top: 160px; z-index: 1006;'></div>");
    $("#PhotosmControls").append("<div id='LokiMe' style='position: absolute; left: 8px; top: 160px; z-index: 1006;'><a href='javascript:lokiMe()'><img id='LokiButton' style='position: relative; width: 26px; height: 25px; border: 0px;' src='images/loki_marker.png' alt='Locate me' /></a></div>");	  
	  
	$(".btn-slide").click(function(){
		$("#panel").slideToggle("slow");
		$(this).toggleClass("active"); return false;
	});

	$('#windowOpen').bind(
		'click',
		function() {
			if($('#window').css('display') == 'none') {
				$(this).TransferTo(
					{
						to:'window',
						className:'transferer2', 
						duration: 400,
						complete: function()
						{
							$('#window').show();
						}
					}
				);
			}
			this.blur();
			return false;
		}
	);
	$('#windowClose').bind(
		'click',
		function()
		{
			$('#window').TransferTo(
				{
					to:'windowOpen',
					className:'transferer2', 
					duration: 400
				}
			).hide();
		}
	);
	$('#windowMin').bind(
		'click',
		function()
		{
			$('#windowContent').SlideToggleUp(300);
			$('#windowBottom, #windowBottomContent').animate({height: 10}, 300);
			$('#window').animate({height:40},300).get(0).isMinimized = true;
			$(this).hide();
			$('#windowResize').hide();
			$('#windowMax').show();
		}
	);
	$('#windowMax').bind(
		'click',
		function()
		{
			var windowSize = $.iUtil.getSize(document.getElementById('windowContent'));
			$('#windowContent').SlideToggleUp(300);
			$('#windowBottom, #windowBottomContent').animate({height: windowSize.hb + 13}, 300);
			$('#window').animate({height:windowSize.hb+43}, 300).get(0).isMinimized = false;
			$(this).hide();
			$('#windowMin, #windowResize').show();
		}
	);
	$('#window').Resizable(
		{
			minWidth: 200,
			minHeight: 60,
			maxWidth: 750,
			maxHeight: 400,
			dragHandle: '#windowTop',
			handlers: {
				se: '#windowResize'
			},
			onResize : function(size, position) {
				$('#windowBottom, #windowBottomContent').css('height', size.height-33 + 'px');
				var windowContentEl = $('#windowContent').css('width', size.width - 25 + 'px');
				if (!document.getElementById('window').isMinimized) {
					windowContentEl.css('height', size.height - 48 + 'px');
				}
			}
		}
	);
	
});