/***
	luettge.js
***/



// js-session for language
var sarr = Session.get("sarr") || {
	lang: "de"
};

var langs = new Array("de","en","fr");

// jquery
$(document).ready(
	function() {
		
		// redirect root to entry page
		if(window.location.pathname.indexOf("/seite") == -1 || window.location.pathname == "/") {
			if(window.location.pathname == "/") {
				window.location = window.location + "seite/home";
			} else {
				window.location = window.location.href.replace(window.location.pathname, window.location.pathname+"seite/home");
			}
			
			return;
		}
		
		// move langauges before last elment of navigation
		$(".navspacer").hide();
		$(".navspacer").last().html($("#languages-container").html()).addClass("span-12").show();
		$("#languages-container").html("");
		
		// register to events
		registerEvents();
		
		// add hash+showgrid to url to show grid, like http://www.xyz.com/some-content/#showgrid
		var hash = null;
		var hashParts = String(window.location).split('#');
		if(hashParts.length == 2) { hash = String(hashParts[1]); }
		if(hash != undefined) hash = hash.toLowerCase();
		if(hash == "showgrid") {
			$(".container").attr("style", "background: #fff url('../../public/images/grid.png') repeat left top;");
		}
		
		// hide non default languages (IE6 fix)
		$('[lang="en"]').hide();
		$('[lang="fr"]').hide();
		
		// toggle language by hash
		if(hash == "english" || hash == "en") {
			showLanguage("en");
		}
		else if(hash == "francais" || hash == "fr") {
			showLanguage("fr");
		} 
		else if(hash == "deutsch" || hash == "de") {
			showLanguage("de");
		} else {
			// show language known from js-session
			showLanguage(sarr.lang);
		}
		
		// fade in images of content
		$('.article img').each( function(i,val) { $(this).hide(); $(this).fadeIn(100+(15*(i+1))); });
	}
);

function registerEvents() {
	// click folder to toggle its visibility
	$("div.nav-item, div.nav-item-selected").click(function (e) {
		// load item
		window.location = $(this).children("a").attr("href");
	});
	
	// change language
	$("#languages a").click(function (e) {
		if($(this).attr("class") != "language") {
			showLanguage($(this).attr("id"));
		}
		e.preventDefault();
		return false;
	});
	
	// header
	$("#header").click(function (e) {
		window.location = $(this).children("a").attr("href");
		return true;
	});
}

function showLanguage(id) {
	
	for(l in langs) {
		if(langs[l] != id) {
			$('[lang="!'+langs[l]+'"]').show();
		} else {
			$('[lang="!'+langs[l]+'"]').hide();
		}
	}
	
	// is not visible yet
	if($('[lang="'+id+'"]').is(":visible") == false) {
		// page title
		$("title").html( $("#title_"+id).html() + " &sdot; " + $("meta[name='author']").attr("content") );
		
		// localized content
		$('[lang="'+id+'"]').show();
		sarr.lang = id;
		Session.set("sarr", sarr);
		$('[lang="'+$("a.language").attr("id")+'"]').hide();
		//$('[lang!="'+id+'"]').hide();
		$("a.language").removeClass("language");
		$("#"+id).addClass("language");
	}
	
	if(window.location.hash != "#showgrid") {
		window.location.hash = "#"+id;
	}
}
