;(function($) {
	var baseUrl			= "ernstings-family.de";
	var baseUrl_staging 	= "ernstings.spot-media.de";

	if(window.location.host.match(/wcsefdev/)) {
		baseUrl = baseUrl_staging;
	}
	
	var spassBaseUrl		= "http://spass." + baseUrl;
	var serviceBaseUrl		= "http://service." + baseUrl;
	var newsletterBaseUrl	= "http://newsletter.ernstings-family.com";
	var staticBaseUrl		= "http://static." + baseUrl;
	
	/**
	 * configurations for layers that might be shown on page-load.
	 * For each layer an URL-Pattern and a set of parameters is defined.
	 * The parameters are read from the GET-Variables and are placed
	 * into the corresponding placeholder of the URL-Pattern.
	 *
	 * Members:
	 * - urlPattern: the URL containing a placeholder ("{...}") for
	 *     each parameter it contains
	 * - params: a list of parameters being replaced in the urlPattern.
	 *     The actual value of the parameter is taken from the window's
	 *     location. 
	 * - isVolatileUrl: nyroModal moves the created iframe around in the
	 *     dom, which causes the firefox to reload the URL with every
	 *     modification. Most URLs won't be affected from being loaded
	 *     multiple times but some will. These URLs are called 'volatile'.
	 *     If set to true, countermeasures will be initiated to avoid
	 *     loading the volatile URL multiple times.
	 * - width, height: you may have a wild guess on these...
	 */ 
	var layerConfig = {
//		freeshippingcharge: {
//		    urlPattern: staticBaseUrl + "/layer/freeshippingcharge/layer.iframe.freeshippingcharge.html",   
//		    params: [],
//		    isVolatileUrl: true,
//		    closeButton: '<a href="#" class="modalClose" id="modalClose" '
//		    	+ 'style="color:#ffffff; background:transparent url(http://static.ernstings-family.de/layer/freeshippingcharge/images/btn_close_weiss.gif) no-repeat scroll right 2px" '
//        		+ 'title="Fenster schließen">Fenster schließen</a>',
//			width: 587, height: 552
//		},

		ecards: {
			urlPattern: spassBaseUrl + "/ecard/show/{ecardId}",
			params: ['ecardId'],
			isVolatileUrl: true,
			width: 585, height: 575
		},
		
		// NEU
		storesearch: {
			urlPattern: serviceBaseUrl + '/filialsuche', 
			params: [],
			isVolatileUrl: true,
			width: 687, height: 666
		},
		
		bannerLandingPage: {
			urlPattern: "/layer/lilalaune.layer.html",
			params: [],
			forceType: false,
			endShowContent : function() {
				cmCreatePageviewTag('Li-La-Laune Newsletter Layer', 'PAGELOAD_LAYER');
				
				$('#layer_lilalaune form').submit(function() {
					var submitUrl = newsletterBaseUrl + "/public/remotebox.jsp"
							+ "?uemail={uemail}";
					var uemailValue = $('#newsletter_uemail').val();
					$.nyroModalManual({
						url: submitUrl.replace('{uemail}', uemailValue),
						width: 685, height: 481
					});
					return false;
				});
			}
		},
		
		// NEU
		newsletterSubscribe: {
			urlPattern: newsletterBaseUrl + "/public/remotebox.jsp?uemail={uemail}",
			params : ['uemail'],
			isVolatileUrl: true,
			width: 685, height: 483
		},
		
		newsletterConfirm: {
			urlPattern: newsletterBaseUrl + "/public/subscribe.jsp"
				+ "?tsp={tsp}&uid={uid}&tan={tan}&sig={sig}&action={action}&email={email}&errorPage=/public/subscribe_err.jsp",
			params : ['tsp', 'uid', 'tan', 'sig', 'action', 'email'],
			isVolatileUrl: true,
			width: 685, height: 301
		},
		newsletterUnsubscribe: {
			urlPattern: newsletterBaseUrl + "/public/unsubscribe_i.jsp"
				+ "?uid={uid}&mid={mid}&sig={sig}&jid={jid}&gid={gid}&email={email}",
			params : ['uid', 'mid', 'sig', 'jid', 'gid', 'email'],
			isVolatileUrl: true,
			width: 685, height: 486
		},
		newsletterRecommend: {
			urlPattern: newsletterBaseUrl + "/public/forward_message.jsp"
				+ "?uid={uid}&mid={mid}&gid={gid}&sig={sig}",
			params : ['uid', 'mid', 'sig', 'gid'],
			isVolatileUrl: true,
			width: 685, height: 707
		},
		newsletterProfileEdit: {
			urlPattern: newsletterBaseUrl + "/public/profile_edit.jsp"
				+ "?tsp={tsp}&custid={custid}&uid={uid}&sig={sig}&for_mid={for_mid}&mid={mid}&gid={gid}",
			params : ['tsp', 'custid', 'uid', 'sig', 'for_mid', 'mid', 'gid'],
			isVolatileUrl: true,
			width: 685, height: 485
		}
	};
	
	function doOpenLayer(settings) {
		var modalSettingsDefault = {
			forceType: 'iframe',
			titleFromIframe: false, 
			endFillContent: function() { $('.modalWrap').css({ position: 'absolute' }); }
		};
		
		var modalSettings = $.extend({}, modalSettingsDefault, settings);
		
		if(settings.isVolatileUrl) {
			modalSettings.url = 'about:blank';
			modalSettings.realUrl = settings.url;
			modalSettings.endFillContent = function(modal, currentSettings) {
				var cw = $('iframe', modal.contentWrapper)[0].contentWindow;
				cw.location.href = currentSettings.realUrl;
				
				$('.modalWrap').css({ position: 'absolute' });
			};
		}
		
		$.nyroModalManual(modalSettings);
	};
	
	function parseUrl() {
		var queryStr = "";
		try {
			queryStr = window.location.search.substr(1);
			
			if(queryStr == "") { return {}; }
			
			var params = {};
			var parts = queryStr.split('&');
			for(var i=0; i<parts.length; i++) {
				var m = /([^=]+)=(.*)/.exec(parts[i]);
				if(!m) { params[parts[i]] = true; }
				else { params[m[1]] = m[2]; }
			}
			return params;
		} catch(e) { return {}; }
	}
	
	function startup() {	
		var urlParams = parseUrl();
		var layerName = urlParams.openLayer;
		var now = new Date();
//		var dateBeginShippingFree = new Date(2009,11,17, 8,0,0); // 0=janauray, 9=october
//		var dateExpireShippingFree = new Date(2009,11,20, 21,0,0);
		// automatischer Aufruf der herbstaktion
//		if(!layerConfig[layerName] && (dateBeginShippingFree.getTime() < now.getTime()) && (now.getTime() < dateExpireShippingFree.getTime())) {		
//			layerName = 'freeshippingcharge';
//		}
		
		if(!layerConfig[layerName]) return;
		if(layerName == 'bannerKampagne') {
			if(document.cookie.match(/bannerKampagne2Shown/)) { return; }
			document.cookie = "bannerKampagne2Shown=1; expires=Sun, 31 Jan 2010 23:00:00 GMT";
		}
			
//		if(layerName == 'freeshippingcharge') {
			
//			if(document.cookie.match(/freeshippingchargeLayerShown/)) { return; }
//			if(dateBeginShippingFree.getTime() > now.getTime() || now.getTime() > dateExpireShippingFree.getTime()) { return; }
						
//			document.cookie = "freeshippingchargeLayerShown=1; expires=Sat, 01 Jan 2010 08:00:00 GMT";
//		}
		
		// user comes out of newsletter. Layer should not be shown!
		if(layerName == 'herbstaktion_nl') {
			document.cookie = "herbstaktionLayerShown=1; expires=Sat, 01 Nov 2009 08:00:00 GMT";
			return false;
		}
		
		var cfg = layerConfig[layerName];
		cfg.url = cfg.urlPattern;
		for(var i=0; i<cfg.params.length; i++) {
			cfg.url = cfg.url.replace( "{"+cfg.params[i]+"}", urlParams[cfg.params[i]] );
		}
		
		doOpenLayer(cfg);
	};
	
	$(startup);
})(jQuery);
