/**
 * Common JS stuff for fellerich.lu
 * 
 * requires jquery to be loaded  
 *  
 */
$(function(){

	/**
	 * Add the language select HTML snipped to a page if none present
	 */
	if ($('#language-select').size()==0) {
		$('body').prepend(
'<ul id="language-select">'+
'	<li><a href="?lb"><img src="/img/flags/mini.lb" alt="LU"> L&euml;tzebuergesch</a></li>'+
'	<li><a href="?de"><img src="/img/flags/mini.de" alt="DE"> Deutsch</a></li>'+
'	<li><a href="?en"><img src="/img/flags/mini.en" alt="GB"> English</a></li>'+
'	<li><a href="?fr"><img src="/img/flags/mini.fr" alt="FR"> Fran&ccedil;ais</a></li>'+
'</ul>'
		);
	}

	/**
	 * Find language selector list, and modify links
	 * 
	 * Instead of relying on the server to set the lang cookie,
	 * set the cookie from JS and then reload the page, letting
	 * the server find the correct language page.	 
	 */
	$('#language-select a').click(function(){
		// Language Changing Link: location + ?lang [ + optional anchor ]
		var q = this.href.match(/\?(\w{2})(#.*)?$/);
		// must have found a 2 letter language code and cookies
		if (!q || !navigator.cookieEnabled) return true;
		// set the cookie to the desired language
		document.cookie = "lang="+q[1]+";path=/";
		// anchor in language link found? then update the anchor
		if (q[2]) window.location.hash = q[2];
		// and then simply refresh the page!
		window.location.reload();
		// prevent default action
		return false;
	});

});

// eof
