(function($) {
	/**
	 * private functions
	 */
	var layerClass;
	var basicConfig;
	var registered = [];

	// Basiskonfiguration speichern
	function setBasicConfig(config) {
		basicConfig = config;
	}

	// Funktion zum hinzufuegen von Layerlinks
	function addLayer(setup) {

		// notwendigen Container zum Body hinzufuegen
		jQuery('<div id="' + setup.background.substr(1) + '"></div>').hide().appendTo('body');
		jQuery('<div id="' + setup.container.substr(1) + '"></div>').hide().addClass(basicConfig.layerClass).appendTo('body');

		// jQuery Objekte erzeugen
		setup.$link = jQuery(setup.link);
		setup.$container = jQuery(setup.container);
		setup.$background = jQuery(setup.background);

		// Global ablegen
		registered.push(setup);
		setup.$link.bind('click',function() {
			var current = jQuery.op_header.getActiveTab();
			if(current == setup.key) {
				hideLayer(setup);
			} else if(current && current != setup.key) {
				var oldSetup = getConfig(current);
				oldSetup.$link = jQuery(oldSetup.link);
				oldSetup.$container = jQuery(oldSetup.container);
				oldSetup.$background = jQuery(oldSetup.background);
				hideLayer(oldSetup);
				showLayer(setup);
			} else {
				showLayer(setup);
			}
		});
	}

	function getScript(setup) {

	}

	// Funktion zum verstecken des Layers
	function hideLayer(setup) {
		if(setup.$container) {
			setup.$container.hide();
		}
		if(setup.$background) {
			setup.$background.hide();
		}
	}

	function hideAllLayer() {
		jQuery.each(registered, function(){
			hideLayer(this);
		});
	}

	//Funktion zum anzeigen des Layers
	function showLayer(setup) {
		if(setup.$container.hasClass('loaded')) {
			setup.$container.show();
			setup.$background.show();
		} else if(!setup.doNotFetchLink) {
			jQuery.op_header.abortAjax();
			setup.$container.show();
			setup.$background.show();
			var layerRequest = jQuery.ajax({
				url: setup.$link.attr('href'),
				type: 'GET',
				dataType: 'html',
				success: function(data) {

					var newContent = jQuery(data).find(setup.ajaxContainer).html();
					jQuery(setup.ajaxContainer).find(setup.closeLink).hide().remove();
					setup.$container.append(newContent);

					// Layer Schliessen Button
					jQuery(setup.closeLink).click(function(){
						setup.$link.click();
						return false;
					});
					setBackgroundHeight(setup.$container,setup.$background);
					setup.$container.addClass('loaded');
				}
			});
			jQuery.op_header.setAjaxObj(layerRequest);
		}

		//load script for layer
		else if (setup.script) {
			setup.$container.show();
			setup.$background.show();
			jQuery.getScript(setup.script, function() {
				setBackgroundHeight(setup.$container,setup.$background);
				setup.$container.addClass('loaded')
				// Layer Schliessen Button
				jQuery(setup.closeLink).click(function(){
					setup.$link.click();
					return false;
				});
			});
		}
	}

	// Hoehe des Hintergrund Layers berechnen und setzen
	function setBackgroundHeight($container,$background) {
		var bodyHeight = jQuery('body').outerHeight();
		//tryLog(bodyHeight);
		var windowHeight = jQuery(window).height();
		var containerHeight = $container.outerHeight();
		//tryLog(containerHeight);
		$background.height(Math.max(bodyHeight - basicConfig.offset,containerHeight,windowHeight));
	}

	function getConfig(key) {
		var config = false;
		jQuery(registered).each(function(){
			if(config == false && this.key == key) {
				config = this;
			}
		});
		return config;
	}

	/**
	 * public functions
	 */
	jQuery.op_layer = {
		setBasicConfig : function (config) {
			setBasicConfig(config);
		},
		addLayer : function (setup) {
			addLayer(setup);
		},
		setBackgroundHeight : function ($container,$background) {
			setBackgroundHeight($container,$background);
		},
		hideAllLayer : hideAllLayer
	}
})(jQuery);

jQuery(document).ready(function(){
	loadAjax = false;
	jQuery.op_layer.setBasicConfig({
		'layerClass': 'layer',
		'offset': '79'
	});
	// Laenderauswahl Layer
	jQuery.op_layer.addLayer({
		'key': 'country',
		'link': '#countrySelect a',
		'container': '#countrySelectLayer',
		'ajaxContainer': '#countrySelectMap',
		'closeLink': '#closeCountrySelectLayer',
		'background': '#backgroundCountrySelect',
		'script': '/de/laenderauswahl/wmap.js',
		'doNotFetchLink': true
	});
	// Sitemap Layer
	jQuery.op_layer.addLayer({
		'key': 'sitemap',
		'link': '#sitemapLink a',
		'container': '#sitemapLayer',
		'ajaxContainer': '#sitemap',
		'closeLink': '#closeSitemapLayer',
		'background': '#backgroundSitemap'
	});
	jQuery('body').bind('hideAllLayer', function(){
		jQuery.op_layer.hideAllLayer();
	});
});
