$(window).load(init);

function init() {
	try {
		getContent($("#footer"), "../content/footer.html");
		logger.log("footer", "info");
		getContent($("#header"), "../content/header.html");
		logger.log("header", "info");
		getMenue();
		callExtension();
		CryptMailto();
	} catch (e) {
		logger.log(e.message, "error");
	}
}

function callExtension() {
	try {
		if (extensionHandler) {
			$('body').bind("extend", extensionHandler());
		}
	} catch (e) {
		logger.log(e.message, "error");
	}
}

function harmonizeSize() {
	setTimeout("equalsHeight()", 50);
}

function equalsHeight() {
	$("#contentLeft").height($("#contentRight").height());
	logger.log("menue-height:" + $("#contentLeft").css("height") + "<>"
			+ $("#contentRight").css("height"), "debug");
}

function changeContent(target) {
	getContent($("#contentRight"), target);
}

function getContent(loadDiv, loadUrl) { // function to fetch external page
	// containing the panel DIVs
	var $contentContainer = loadDiv; // reference empty div on page that will
	// hold menu
	$contentContainer.html("Loading..." + loadDiv.selector);
	$.ajax({
		url : loadUrl, // path to external menu file
		async : true,
		error : function(ajaxrequest) {
			$contentContainer.html('Error fetching content. Server Response: '
					+ ajaxrequest.responseText);
		},
		success : function(content) {
			$contentContainer.html(content);
			logger.log("before harmonize after success", "debug");
			harmonizeSize();
			logger.log("after harmonize after success", "debug");
		},
		complete : function(ajaxrequest) {
			logger.log("before harmonize after complete", "debug");
			harmonizeSize();
			logger.log("after harmonize after complete", "debug");
		}
	});
}

function getMenue() { // function to fetch external page containing the panel
	// DIVs
	var $menucontainer = $('#nav'); // reference empty div on page that will
	// hold menu
	$menucontainer.html("Loading Menu...");
	$.ajax({
		url : '../content/nav.html', // path to external menu file
		async : true,
		error : function(ajaxrequest) {
			$menucontainer.html('Error fetching content. Server Response: '
					+ ajaxrequest.responseText);
		},
		success : function(content) {
			$menucontainer.html(content);
		},
		complete : function(ajaxrequest) {
			dynamizeMenue();
		}
	});
}

var submenuoffset = -10; // Offset of submenus from main menu. Default is -2
// pixels.

function dynamizeMenue() {
	$("#menuediv .second").hide();
	$("#menuediv .third").hide();
	$("#menuediv .firstLevel").click(function() {
		logger.log("firstLevel", "debug");
		$("#menuediv .third:visible").slideUp("slow");
		$("#menuediv .second:visible").slideUp("slow");
		return true;
	});
	$("#menuediv .secondLevel").click(function() {
		logger.log("secondLevel", "debug");
		$("#menuediv .third:visible").slideUp("slow");
		$("#menuediv .second:visible").slideUp("slow");
		if (!$(this).next().is(":visible")) {
			$(this).next().slideDown("slow");
		}
		return true;
	});
	$("#menuediv .thirdLevel").click(function() {
		logger.log("thirdLevel", "debug");
		$("#menuediv .second .third:visible").slideUp("slow");
		if ($(this).next().is(":visible")) {
			$("#menuediv .second .third:visible").slideUp("slow");
		} else {
			$(this).next().slideDown("slow");
		}
		return true;
	});
}


function deMailTo(s) {
	var n = 0;
	var r = "";
	for ( var i = 0; i < s.length; i++) {
		n = s.charCodeAt(i);
		if (n >= 8364) {
			n = 128;
		}
		r += String.fromCharCode(n - 1);
	}
	return r;
}

function ltmt(s) {
	location.href = deMailTo(s);
}

