/**
 * Auslesen der Parameter mithilfe einer JQuery-Funktion.
 * Dabei wird das Framework um die Funktion getParameter(s) erweitert.
 */
jQuery.getParameter = function(s) {
	var r = {};
	if (s) {
		var q = s.substring(s.indexOf('?') + 1); // remove everything up to the ?
		q = q.replace(/\&$/, ''); // remove the trailing &
		jQuery.each(q.split('&'), function() {
			var splitted = this.split('=');
			var key = splitted[0];
			var val = splitted[1];
			// convert numbers
			if (/^[0-9.]+$/.test(val)) val = parseFloat(val);
			// convert booleans
			if (val == 'true') val = true;
			if (val == 'false') val = false;
			// ignore empty values
			if (typeof val != 'undefined') {
				if (typeof val == 'number' || typeof val == 'boolean' || val.length > 0) r[key] = val;
			}
		});
	}
	return r;
};

var getParams = $.getParameter(document.location.search);

var actLinkElem;
		
function setActive(linkElem) {
	$(linkElem).attr("class","active");
}

function setNoActive(){
	$("#newsList").find("li > a.active").each(function(){
		$(this).removeAttr("class");
	});
}

function doAction() {
	$("#newsList").find("li > a").each(function(){
		$(this).click(function(){
			actLinkElem = $(this);
			$("#contentContainer").slideUp("slow",function(){
				setNoActive();
				showNews($(actLinkElem).attr("href"));
			});
			return false;
		})
	});
}
	
function showFirst() {
	setActive($("#newsList > li:first-child > a"));
	$("#contentContainer > #inner").html($("#newsList > li:first-child > .newsBlock").html());
	$("#contentContainer").slideDown("slow");
	$("a.lightwindow").fancybox();
}
		
function showNews(news){
	var a_elem = $("#newsList").find("li > a[href$="+news+"]");
	setActive(a_elem);
	$("#contentContainer > #inner").html($(a_elem).parent().find("div.newsBlock").html());
	$("#contentContainer").slideDown("slow");
	$("#contentContainer a.lightwindow").fancybox();
}
		
$(document).ready(function() {
	$("#contentContainer").hide();
	if(getParams.showNews != undefined) {
		showNews(getParams.showNews);
	} else {
		showFirst();
	}
	doAction();
});
