function ReinitializeAddThis() {
	if(window.addthis) {
		window.addthis.ost = 0;
		window.addthis.ready();
	}
}

function loadPage(html) {
	var stringSplitArray = html.split("~~~~~~");
	matches = stringSplitArray[0].match(/<title>(.*?)<\/title>/);
	var titlestr = matches[1];
	document.title = titlestr;
	matches = stringSplitArray[0].match(/<meta name=\"description\" content=\"(.*?)\"/);
	$('meta[name=description]').attr('content', matches[1]);
	matches = stringSplitArray[0].match(/<meta name=\"keywords\" content=\"(.*?)\"/);
	$('meta[name=keywords]').attr('content', matches[1]);
	matches = stringSplitArray[1].match(/images\/(.*)/);

	$('#globalsidebarheader').html(stringSplitArray[2]);
	$('#content').html(stringSplitArray[3]);
	$("#language a").prop("href", stringSplitArray[4]);
	imgcache = $('#sidebarbg').children('img').attr("src");

	$.cacheImage(imgcache, {
		load : showEverything
	});

	$.backstretch(matches[0], {
		speed : 700
	});
}

function loadPagewithLang(html) {

	var stringSplitArray = html.split("~~~~~~");
	matches = stringSplitArray[0].match(/<title>(.*?)<\/title>/);
	var titlestr = matches[1];
	document.title = titlestr;
	matches = stringSplitArray[0].match(/<meta name=\"description\" content=\"(.*?)\"/);
	$('meta[name=description]').attr('content', matches[1]);
	matches = stringSplitArray[0].match(/<meta name=\"keywords\" content=\"(.*?)\"/);
	$('meta[name=keywords]').attr('content', matches[1]);
	matches = stringSplitArray[1].match(/images\/(.*)/);

	$('#globalsidebarheader').html(stringSplitArray[2]);
	$('#content').html(stringSplitArray[3]);
	$("#language a").prop("href", stringSplitArray[4]);
	$('#header').html(stringSplitArray[5]);
	$('#footer').html(stringSplitArray[6]);
	imgcache = $('#sidebarbg').children('img').attr("src");
	$.cacheImage(imgcache, {
		load : showEverything
	});

	$.backstretch(matches[0], {
		speed : 700
	});

}

function showEverything() {

	$("#forvalidation").validate({
		submitHandler : function(form) {
			if($("#newsletteron").length > 0) {
				$.ajax({
					url : 'index.php?ajax=yes',
					type : 'post',
					data : $(form).serialize(),
					success : function() {
						$("#newsletteron").hide();
						$("#newsletteroff").show();
					},
					error : function() {
						alert("error");
					}
				});
				return;
			}
			$('#content').hide();
			$('#globalsidebarheader').hide();
			if(!$('#loading').is(":visible")) {
				$('#loadingsmaller').show();
			}
			$.ajax({
				url : 'index.php?ajax=yes',
				type : 'post',
				data : $(form).serialize(),
				success : loadPage,
				error : function() {
					alert("error");
				}
			});

		}
	});

	$('#loadingsmaller').hide();

	$('#loading').hide()

	$('#header').show();
	activateMenu();
	document.onclick = hideMenu;

	$('#globalsidebarheader').show();
	$('#content').show();
	$("#newsletteron").show();

	$('#footer').show();

	_gaq.push(['_trackPageview']);
	
	
	$("a#graph").fancybox({
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic',
		'speedIn' : 600,
		'speedOut' : 200,
		'overlayShow' : false,
		'padding' : 0
	});
}


$(document).ready(function() {
	$('#header').hide();
	$('#content').hide();
	$('#globalsidebarheader').hide();
	$('#footer').hide();
	$('#loading').show();

});

$(window).load(function() {
	function load(url) {
		if(url == "") {
			showEverything();
			showBackground();
			return;
		}
		if(url.search(/&chlang=yes/) == -1) {
			$('#content').hide();
			$('#globalsidebarheader').hide();
			if(!$('#loading').is(":visible")) {
				$('#loadingsmaller').show();
			}
			url = "index.php?content=" + url + "&ajax=yes";
			$.ajax({
				url : url,
				cache : true,
				success : loadPage,
				error : function() {
					alert("error");
				}
			});
		} else {
			$('#header').hide();
			$('#content').hide();
			$('#globalsidebarheader').hide();
			$('#footer').hide();
			$('#loading').show();
			url = "index.php?content=" + url + "&ajax=yes";
			$.ajax({
				url : url,
				cache : true,
				success : loadPagewithLang,
				error : function() {
					alert("error");
				}
			});
		}

	}


	$.history.init(function(url) {
		load(url);
	}, {
		unescape : "?=,/&"
	});

	$('#menu a').live('click', function(e) {
		hideMenu();
		var url = $(this).attr('href');
		$.history.load(url);
		return false;
	});

	$('#footer a').live('click', function(e) {
		var url = $(this).attr('href');
		$.history.load(url);
		return false;
	});

	$('#content a').live('click', function(e) {
		var url = $(this).attr('href');
		if($(this).attr('target') == '_blank') {
			return true;
		}
		if(url.indexOf("http") != -1) {
			return true;
		}
		if(url.indexOf("images/") != -1) {
			return true;
		}
		$.history.load(url);
		return false;
	});

	$('#logotype a').live('click', function(e) {
		var url = $(this).attr('href');
		$.history.load(url);
		return false;
	});

	$('#globalsidebarheader a').live('click', function(e) {
		var url = $(this).attr('href');
		$.history.load(url);
		return false;
	});

	$('#language a').live('click', function(e) {
		var url = $(this).attr('href') + "&chlang=yes";
		$.history.load(url);
		return false;
	});
});

