$(document).ready(function() 
{	
var lastOffset;
var currentSection;

$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}


function adjustNaviBlackBar(_obj)
{
	var offset = _obj.offset();

	$("#blackNaviLine").stop();
	
	$("#blackNaviLine").animate({
		 "width": offset.left+20
		}, 1000, "easeOutQuint");
};

function setNaviBarWidth(_obj)
{
	currentSection = _obj;
	var offset = _obj.offset();
	lastOffset = offset.left+20;

	adjustNaviBlackBar(_obj);
};

function updateNavi(_name)
{
	var buttonName = _name+"Button";
	var sectionName = currentSection.text();
	//alert(sectionName+".askjgh");
	var linkName = sectionName.toLowerCase();
	linkName = linkName.replace(/\s/g, "");
	var oldButtonName = linkName+"Button";
	oldButtonName = oldButtonName.replace(/\s/g, "");
	
	if(oldButtonName == "homeButton" && buttonName != "homeButton")
	{
		stopRandomImageTimer();
	}
	
	//alert(oldButtonName);
	currentSection.children("span").replaceWith('<div class="naviBackground"></div><a href="'+linkName+'">'+sectionName+'</a>');
	$("#naviBar li."+oldButtonName+" div.naviBackground").css("opacity", "1");
	$("#naviBar li."+oldButtonName+" a").css("color", "#fff");
	$("#naviBar li."+oldButtonName+" a").trigger("mouseout");
	
	currentSection = $("#naviBar li."+buttonName);
	lastOffset = currentSection.offset().left+20;
	adjustNaviBlackBar($("#naviBar li."+buttonName));
	
	var newSectionName = currentSection.text();
	currentSection.replaceWith("<li class="+buttonName+"><span>"+newSectionName+"</span></li>");
	currentSection = $("#naviBar li."+buttonName);
};


//Navi animation functions

function mouseOut(event)
{
	var target = $(event.target);
		$("#blackNaviLine").stop();
		
		$("#blackNaviLine").animate({
			 "width": lastOffset
		}, 1000, "easeOutQuint");
		
		target.stop();
		
		target.animate({
			 "color": "#000"
		}, 400);
		
		$(".naviBackground", target.parent()).stop().animate({
			"opacity": 0
		}, 400, "easeOutQuint");
};

function mouseOver(event)
{
	var target = $(event.target);
		adjustNaviBlackBar(target); 
		
		target.stop();
		
		target.animate({
			 "color": "#fff"
		}, 400);
		
		$(".naviBackground", target.parent()).stop().animate({
			 "opacity": 1
		}, 400, "easeOutQuint");
};

function mouseUp(event)
{
	var target = $(event.target);
	var newHash = target.attr("href");
	window.location.hash = newHash;
	return false;
	//event.preventDefault();
};

function logoMouseUp(event)
{
	window.location.hash = "home";
	return false;
};

$(".arrowLink").live("mouseenter",
	function()
	{
		$(this).stop().animate(
			{
				"color": "#000"
			}, 300);
				
		$(this).prev(".arrowLinkImg").stop().animate(
			{
				"opacity": 0.5,
				"margin-right": "14px"
			}, 200);
	}
);

$(".arrowLink").live("mouseleave",
	function()
	{
		$(this).stop().animate(
			{
				"color": "#ff2222"
			}, 300);
		
		$(this).prev(".arrowLinkImg").stop().animate(
			{
				"opacity": 1,
				"margin-right": "8px"
			}, 200);
	}
);


$(".redArrow").live("click",
	function()
	{
		if($(this).attr("src") == "../img/redArrow.png")
		{
			$(this).attr("src", "../img/redArrowRotated.png");
			$(this).parent("a").parent("h2").next(".hideable").slideToggle("slow", "easeOutQuint", function()
			{
				readjustContentHeights();
			});

		}
		else
		{
			$(this).attr("src", "../img/redArrow.png");
			$(this).parent("a").parent("h2").next(".hideable").slideToggle("slow", "easeOutQuint", function()
			{
				readjustContentHeights();
			});
		}
	}
);


$("#naviBar li a").live("mouseenter", mouseOver);
$("#naviBar li a").live("mouseleave", mouseOut);
$("#naviBar li a").live("click", mouseUp);
$("#logoImage").bind("click", logoMouseUp);



//Impressum
$("#impressumButton").click(
		function(){
			if($("#impressum").css("display") == "none"){
				$("#impressum").slideDown("fast");
			} else {
				$("#impressum").slideUp("fast");
			}
		}
);

$("#hinweiseButton").click(
		function(){
			if($("#rechtlicheHinweise").css("display") == "none"){
				$("#rechtlicheHinweise").slideDown("fast");
			} else {
				$("#rechtlicheHinweise").slideUp("fast");
			}
		}
);

$(".cross").click(
		function(){
				$(this).closest("div").slideUp("fast");
		}
);

$(window).resize(function() {
	setNaviBarWidth(currentSection);
});

//navigation, back button etc.

//adjust div content columns

function readjustContentHeights()
{
	if($("#leftContent").outerHeight() > $("#rightContent").outerHeight())
	{
		$("#leftContent").css("border-right", "1px solid #e8e8e8");
		$("#rightContent").css("border", "0");
		
	} else {
		$("#rightContent").css("border-left", "1px solid #e8e8e8");
		$("#leftContent").css("border", "0");
	}
};

function performLoad(_name, _lastHeight)
{
	var fileName = "pages/"+_name+".html";
	fileName = fileName.replace(/\s/g, "");
	
	if($("#loadImg").length > 0)
	{
		$("#loadImg").remove();	
	}
	
	$("#top").after('<img id="loadImg" src="img/loading.gif" alt="loading" style="position: absolute; top: 50%; left: 50%; z-index: 1000; display: none"/>');
	$("#loadImg").fadeIn(150);
	$.ajax({
	url: fileName,
	dataType: "html",
	cache: true,
	success: function(html){
	$("#loadImg").stop();
	$("#loadImg").fadeOut(75, function()
	{
		$("#loadImg").remove();								
	});
	$("#removeableContent").remove();
	$("#top").after(html);
	$(".noScript").hide();
	var newHeight = $("#removeableContent").height();
	$("#removeableContent").css("height", _lastHeight);
	$("#removeableContent").css("opacity", "0");
	$("#removeableContent").animate(
								{ 
								height: newHeight,
								opacity: 1
								}, 300, function()
	{
		$(this).css("min-height", newHeight);
		$(this).css("height", "auto");
		readjustContentHeights();
		
	});
	updateNavi(_name);
 	}
	});
};

function loadContent(_name)
{	
	if($("#removeableContent").length > 0)
	{
		var lastHeight = $("#removeableContent").height();
		$("#removeableContent").animate({ opacity: 0 }, 300, function()
		{
		 	performLoad(_name, lastHeight);
		});
	}
	else
	{
		performLoad(_name, 0);
	}
};

$(window).hashchange( function()
{
	var hash = location.hash;
	if(hash)
	{
		var hashName = hash.replace( /^#/, '');
		loadContent(hashName);
	} else {
		//window.location.hash = "home";
		//loadContent("home");
	}
	//alert('The hash is ' + ( hashName || 'blank' ) + '.');
});
$("#content").css("display", "none");
$("#content").fadeIn(500);
setNaviBarWidth($("#naviBar li.homeButton"));
$(window).hashchange();
});
