var scrollTimeOffset = 10;
var scrollTimer = scrollTimeOffset;
var currentScroll = 0;
$(document).ready(function() {
	currentScroll = $(window).scrollTop();
	$('#linkeSpalte')[0].y = $('#linkeSpalte').offset().top;
	setInterval('scrollMenu()', 20);
	setupMenu();
	
	if(!navigator.userAgent.match(/iPhone|iPod|iPad/i)) {
		$('#player').jwplayer({
			flashplayer:'skin/player.swf?'+Math.random(),
			skin:'skin/five/five.xml'
		});
	}
	
	var hash = $(location).attr("hash");
	if(hash) {
		$('.mainNavigation li a[href='+hash+']').click();
	} else {
		$('.mainNavigation li a[href=#_start]').click();
	}
	$('#linkeSpalte').css('opacity', 0);
	$('#linkeSpalte').animate({opacity: 1},{duration:1000, easing:'easeInOutCubic'});
	setupBackImages();
});

function setupBackImages() {
	for(var i=0;i<6;i++) {
		var img=$('#backimage'+i);
		if(!$.browser.msie) img.css('opacity', 0);
		var randX = -Math.random()*20
		var randY = -Math.random()*30
		var shiftY = (1-Math.random()*2)*200
		if (img[0].style.left) {
			img[0].finalLeft = parseInt(img[0].style.left)+randX;
			img.css('left', -(parseInt(img[0].style.width)+50));
		} else if (img[0].style.right) {
			img[0].finalRight = parseInt(img[0].style.right)+randX;
			img.css('right', -(parseInt(img[0].style.width)+50));
		}
		if (img[0].style.top) {
			img[0].finalTop = parseInt(img[0].style.top)+randY;
			img.css('top', img[0].finalTop+shiftY);
		} else if (img[0].style.bottom) {
			img[0].finalBottom = parseInt(img[0].style.bottom)+randY;
			img.css('bottom', img[0].finalBottom+shiftY);
		}
		
		setTimeout('startBackAnimation('+i+')', Math.random()*300+100);
	}
}

function startBackAnimation(i) {
	var img=$('#backimage'+i);
	var speed=Math.random()*200+400;
	if(!$.browser.msie) img.animate({opacity: 1},{duration:1000, easing:'easeOutCubic'});
	var animation={};
	if (img[0].style.left) {
		animation.left = img[0].finalLeft;
	} else if (img[0].style.right) {
		animation.right = img[0].finalRight;
	}
	if (img[0].style.top) {
		animation.top = img[0].finalTop;
	} else if (img[0].style.bottom) {
		animation.bottom = img[0].finalBottom;
	}
	img.animate(animation,{duration:speed, easing:'easeOutCubic'});
}

function scrollMenu() {
	if(currentScroll != $(window).scrollTop()) {
		scrollTimer=0;
	}
	if (scrollTimer >= scrollTimeOffset) {
		var n=$('#linkeSpalte')[0];
		n.y += ($(window).scrollTop()-n.y)*.1;
		n.style.top = n.y+"px";
	} else {
		scrollTimer++;
	}
	currentScroll=$(window).scrollTop();
}
function setupMenu() {
	$('.mainNavigation li a').each(function(i) {
		this.linkID = this.href.split('#').pop();
		$('a[name='+this.linkID+']')[0].parentNode.id = '_'+this.linkID;
		$('a[name='+this.linkID+']').replaceWith($('a[name='+this.linkID+']')[0].innerHTML);
		
		$(this).click(function() {
			$('.mainNavigation li').removeAttr('class');
			var h = '#_'+this.linkID;
			this.parentNode.className = 'mainActive';
			/*
			$('.section').animate(
				{opacity: .4},
				{duration:1000, easing:'easeInOutCubic'}
			);
			*/
			$('html,body').animate({
				scrollLeft: $(h).offset().left,
				scrollTop: $(h).offset().top-50},
				{duration:1000, easing:'easeInOutCubic'}
			);
		});
	});
}
