/**
 * function buildStartPageAccordion
 * 
 * builds the main accordion which contains highlighted features of the page (e.g. the Map-Client)
 */
var buildStartPageAccordion = function () {
	jQuery("#tagline ul").accordion( {
		header: 'div.accordion-header',
		event: 'mouseover',
		change: function(event, ui) {
			if(ui.newHeader.parent().hasClass('maps')) {
				jQuery('#tagline .demo-maps').show('fast');
				jQuery('#tagline .demo-geodata').hide();
				jQuery('#tagline .demo-sensors').hide();
			} else if(ui.newHeader.parent().hasClass('geodata')) {
				jQuery('#tagline .demo-maps').hide();
				jQuery('#tagline .demo-geodata').show('fast');
				jQuery('#tagline .demo-sensors').hide();
			} else if(ui.newHeader.parent().hasClass('sensors')) {
				jQuery('#tagline .demo-maps').hide();
				jQuery('#tagline .demo-geodata').hide();
				jQuery('#tagline .demo-sensors').show('fast');
			}
		}
	});
};

/**
 * function buildStartPageOpenLayersClient
 * 
 * builds the OL-Client
 */
var buildStartPageOpenLayersClient = function () {
	var options = {
		projection: new OpenLayers.Projection("EPSG:900913"),
		displayProjection: new OpenLayers.Projection("EPSG:4326"),
		units: "m",
		controls: [], 
		maxResolution: 156543.0339,
		maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34)
	};
	// accesing global map variable
	map = new OpenLayers.Map('maps_demo', options);
	layers = {};
	
	layers.mapnik = new OpenLayers.Layer.OSM.Mapnik(
		'OpenStreetMap Mapnik', 
		{ 
			sphericalMercator : true, 
			attribution : 'Daten <a href="http://creativecommons.org/licenses/by-sa/2.0/de">CC-By-SA</a> von <a href="http://openstreetmap.org/">OpenStreetMap</a>'
		}
	);	
	layers.osmarender = new OpenLayers.Layer.OSM.Osmarender(
		'OpenStreetMap Osmarender', 
		{ 
			sphericalMercator : true, 
			attribution : 'Daten <a href="http://creativecommons.org/licenses/by-sa/2.0/de">CC-By-SA</a> von <a href="http://openstreetmap.org/">OpenStreetMap</a>'
		}
	);	
	
	layers.cyclemap = new OpenLayers.Layer.OSM.CycleMap(
		'OpenStreetMap CycleMap', 
		{
			sphericalMercator : true, 
			attribution : 'Daten <a href="http://creativecommons.org/licenses/by-sa/2.0/de">CC-By-SA</a> von <a href="http://openstreetmap.org/">OpenStreetMap</a> &amp; <a href="http://www.opencyclemap.org/">OpenCycleMap</a>'
		}
	);	

	layers.gphy = new OpenLayers.Layer.Google("Google Physical", { type: G_PHYSICAL_MAP,  sphericalMercator:true });
	layers.gmap = new OpenLayers.Layer.Google("Google Streets", { sphericalMercator:true });
	layers.ghyb = new OpenLayers.Layer.Google("Google Hybrid", { type: G_HYBRID_MAP, sphericalMercator:true });
	layers.gsat = new OpenLayers.Layer.Google("Google Satellite", { type: G_SATELLITE_MAP, sphericalMercator:true });

	layers.yahoo_reg = new OpenLayers.Layer.Yahoo("Yahoo Maps Regul&auml;r", { type : YAHOO_MAP_REG, sphericalMercator:true });
	layers.yahoo_hyb = new OpenLayers.Layer.Yahoo("Yahoo Maps Hybrid", { type : YAHOO_MAP_HYB, sphericalMercator:true });
	layers.yahoo_sat = new OpenLayers.Layer.Yahoo("Yahoo Maps Satellit", { type : YAHOO_MAP_SAT, sphericalMercator:true });
	
	layers.markers = new OpenLayers.Layer.Text( "text", {location: "./maps_demo.txt"} );
	
	map.addLayers([layers.mapnik, layers.osmarender, layers.cyclemap, layers.gphy, layers.gmap, layers.ghyb, layers.gsat, layers.yahoo_reg, layers.yahoo_hyb, layers.yahoo_sat, layers.markers]);

	var lon = 7.093412;
	var lat = 50.743905;
	var dlat = 0.05;
	var dlon = 0.05;
	
	map.zoomToExtent( new OpenLayers.Bounds( lon-dlon, lat-dlat, lon+dlon, lat+dlat).transform(map.displayProjection, map.projection) );

	map.addControl(new OpenLayers.Control.Attribution({position: new OpenLayers.Pixel(5,260)}));
	map.addControl(new OpenLayers.Control.PanPanel());
	map.addControl(new OpenLayers.Control.ZoomPanel());
	map.addControl(new OpenLayers.Control.Navigation());
	
	jQuery('#maps_demo_navigation .layer_switcher').bind('change', function(e) {
		window.map.setBaseLayer(window.layers[jQuery(this).val()]);
	});
	jQuery('#maps_demo_navigation .prev_layer').bind('click', function(e) {
		select_option_element(jQuery('#maps_demo_navigation .layer_switcher'), -1);
	});  
	jQuery('#maps_demo_navigation .next_layer').bind('click', function(e) {
		select_option_element(jQuery('#maps_demo_navigation .layer_switcher'), 1);
	});

	jQuery('#maps_demo_navigation .layer_switcher').trigger('change');
};

/**
 * function select_option_element
 * 
 * selects a given option-element specified by a delta of the currently selected option
 */
var select_option_element = function ($select, diff) {
  	var actual = $select.attr('selectedIndex');
  	var future = (actual == 0 && diff < 0) 
		? $select.find('option').length - 1
		: ( actual == $select.find('option').length - 1 && diff > 0) 
			? 0
			: actual + diff; 
  	$select.attr('selectedIndex', future).trigger('change');
};

/**
 * global variables
 */
var map, layers;

/**
 * bind page behaviour to DOM-Ready-Event
 */
jQuery( document ).ready( function() {
	buildStartPageAccordion();
	buildStartPageOpenLayersClient();
});
