// Startup
$(document).ready(function() {
	// initialize fancy stuff only when not in edit mode
	if (typeof wEditMode == "undefined"){
		initMenuLayer();
		initTextWidths();
		initAnimatedHeadlines();
	}
	else
	{
		// inactivate menu clicks when in editor
		$("#header a,#menu# a,#footer a").bind("click", function() {
			return false;
		});
	}
});

// ----- BEGIN layer functionality -----
function initMenuLayer() {
	$('#current2level.haslayer').bind("mouseover", function(e){
	  $('#menulayer').fadeIn("fast");
	  $('.fadeable').fadeTo("fast", 0.15);
	  //return false;
    });

	// note: menulayer needs a z-index for this to work
	$('#menulayer,.center').bind("mouseleave", function(){
			if($('#menulayer').css("display") != "none") {
      	$('#menulayer').fadeOut("fast");
	  		$('.fadeable').fadeTo("fast", 1, function() {
	  			// IE 7/8 fix for not losing its cleartype
					if(jQuery.browser.msie)
						$(this).get(0).style.removeAttribute('filter');
	  		 });
	  	}
	  //return false;
    });
}
// ----- END layer functionality -----

function initTextWidths() {
	$(".person-name-width").width($("#person-name").width());
}

function initAnimatedHeadlines() {
	$(".anim-headline.clickable").bind("click", function() {
		$(".anim-link").fadeOut("slow");
		$(this).fadeOut("slow", function() {
			var id_parts = $(this).attr("id").split("_");
			var nextText=$("#"+id_parts[0]+"_"+(id_parts[1]*1+1));
			var nextLink=$("#anim-link_"+(id_parts[1]*1+1));
			// adapt footer position, if neccessary
			$("#anim-helper").html(nextText.html());
			$(".homepage-content").height(Math.max($("#anim-helper").height(), $(".homepage-content").height()));
			// fade in the text
			nextText.fadeIn("slow");
			nextLink.fadeIn("slow");
		});
	});
	
	// german site shows animated texts, english one only mission statements
	if(location.href.match(/\/de\/i/)) {
		$("#anim-headline_1").fadeIn("slow");
	}
	else
	{
		$("#anim-headline_4").fadeIn("slow");			
	}
}

// ----- BEGIN spam protection -----
function UnCryptMailto(s) {
	var n=0;
	var r="";
	for(var i=0;i<s.length;i++) { 
		n=s.charCodeAt(i);
		// 45 is * which allows special treatment of the letters x to z
		if (n == 45)
		{
			n=s.charCodeAt(i+1);
			r += String.fromCharCode(n);
			++i;
		}
		else
		{
			if (n>=8364) {n = 128;}
			r += String.fromCharCode(n-3);
		}
	}
	return r;
}
function linkTo_UnCryptMailto(s)	{
	location.href=UnCryptMailto(s);
}

// ----- END spam protection -----