$(function()
{
	$('.flexcroll').css('overflow', 'hidden');  
	fix_height();
	cufon();
	menu();
	flexcrollTextCursor();
	nivoSlider();
	nivoCustomNavigation();
	mouseoverScroll();
});

function cufon()
{
	Cufon.replace('#menu a,#side ul li a,#content h3', {hover:true});
}

function mouseoverScroll()
{
	$('.mouseoverscroll').each(function()
	{
		// Variables
		var height = $(this).height();
		// Elements
		var triggers = $(this).find('.up,.down');
		var mouseoverscroll = $(this);
		$(triggers).hover(function()
		{
			var direction;
			if($(this).hasClass('up')) { direction = "up" }else{ direction = "down" } ;
			mover = mouseoverscroll.find('.mover')
			mover.addClass('run-'+direction)
			mouseoverScrollRun(mover, direction);
		},
		function()
		{
			if($(this).hasClass('up')) { direction = "up" }else{ direction = "down" } ;
			mover = mouseoverscroll.find('.mover')
			mover.removeClass('run-'+direction);
		});
	});
}
function mouseoverScrollRun(mover, direction)
{
	if(mover.hasClass('run-'+direction))
	{
		var TOP = parseInt(mover.css('top'));
		if(isNaN(TOP)) TOP = 0;
		var newTop;
		// Nieuwe top berekenen
		if(direction != "up")
		{
			newTop = TOP-10+"px";
		}
		else
		{
			newTop = TOP+10+"px";
		}
		// stop als mover op begin of eind positie zit
		if(parseInt(newTop) > 0) return;
		if(parseInt(newTop) < (parseInt(mover.height()) - parseInt(mover.height())*2)+420) return;
		// animate
		mover.animate({top:newTop}, 100, 'linear', function(){ mouseoverScrollRun(mover, direction); });
	}
}

function nivoCustomNavigation()
{
	if($.browser.msie) { nivoCustomNavigationIE(); return; }
	$('.nivo-customControl li a').each(function()
	{
		$(this).find('img').load(function()
		{
			var width = $(this).width()+1;
			var height = $(this).height()+1;
			var append = $('<a href="#" class="link" />'); // De a element die word toegevoed maar niet ziet, maar wel op kan worden geklikt
			var navTrigger = $(this).parent(); // De a element die is te vinden in de broncode
			append.width(width);
			append.height(height);
			navTrigger.prepend(append);
			append.click(function()
			{
				$(this).blur();
				var li = $(this).parent().parent();
				var lis = $(this).parents('ul').find('li');
				var index = lis.index(li);
				$('#slider .nivo-controlNav a').eq(index).click();
			});
		});
	});
}

function nivoCustomNavigationIE()
{
	$('.nivo-customControl li a').each(function()
	{
		//var width = $(this).width();
		//var height = $(this).height();
		var width = 59;
		var height = 59;
		var append = $('<a href="#" class="link" />');
		append.width(width);
		append.height(height);
		$(this).append(append);
	});
	$('.nivo-customControl li a a.link').click(function()
	{
		$(this).blur();
		var li = $(this).parent().parent();
		var lis = $(this).parents('ul').find('li');
		var index = lis.index(li);
		$('#slider .nivo-controlNav a').eq(index).click();
	});
}

function nivoSlider()
{
	$('#slider').nivoSlider({
			effect:'sliceDown', //Specify sets like: 'fold,fade,sliceDown'
			slices:15,
			animSpeed:800, //Slide transition speed
			pauseTime:4000,
			startSlide:0, //Set starting Slide (0 index)
			directionNav:false, //Next & Prev
			directionNavHide:true, //Only show on hover
			controlNav:true, //1,2,3...
			controlNavThumbs:false, //Use thumbnails for Control Nav
			controlNavThumbsFromRel:false, //Use image rel for thumbs
			controlNavThumbsSearch: '.jpg', //Replace this with...
			controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
			keyboardNav:true, //Use left &amp; right arrows
			pauseOnHover:true, //Stop animation while hovering
			manualAdvance:false, //Force manual transitions
			captionOpacity:0.8, //Universal caption opacity
			beforeChange: function()
			{
				clearTimeout (window.nivoCufonInterval);
				window.textBeforeChange = $('.nivo-caption').text();
				window.nivoCufonInterval = setInterval("nivoCufon()", 10);
			},
			afterChange: function(){},
			slideshowEnd: function(){}, //Triggers after all slides have been shown
			lastSlide: function(){}, //Triggers when last slide is shown
			afterLoad: function(){ Cufon.replace('.nivo-caption'); } //Triggers when slider has loaded
	});
}

function nivoCufon()
{
	if(window.textBeforeChange != $('.nivo-caption').text())
	{
		Cufon.replace('.nivo-caption');
	}
}

function fix_height()
{
	if($.browser.msie && $.browser.version=="6.0")
	{
		$('#background').height($(document).height());
	}
	if($('#background').height() > $('#website').height())
	{
		$('#website').height($('#background').height());
	}
}

function flexcrollTextCursor()
{
	$('.flexcroll p, .flexcroll h4').wrapInner('<span class="cursor-text " />');
}

function menu()
{
	var hoverTerugTimeout = false;
	var sliderTerugTimeout = false;
	
	// Hover classe
	$("#menu > ul > li > a").hover(function()
	{
		$(this).parents('li').addClass('hover');
	}
	,function()
	{
		$(this).parents('li').removeClass('hover');
	});

	// slider toevoegen
	$("#menu").prepend('<div class="slider"></div>');
	var slider = $("#menu .slider");
	
	// animatie regelen
	$("#menu > ul > li").hover(function()
	{
		// slider uitfaden stoppen
		clearTimeout(hoverTerugTimeout);
		clearTimeout(sliderTerugTimeout);
		
		// link van de nmenu
		var menuLink = $(this).find('a:first');
		var left = menuLink.offset().left - $("#menu").offset().left ;
			
		// Nieuwe animatie starten
		slider.show().stop().animate({
			opacity: 100,
			left: left +"px",
			width: menuLink.width() +"px"
		}, 500, 'easeOut');
	},
	function()
	{
		sliderTerugTimeout = setTimeout(function()	{
			slider.show().stop().animate({
				left: 0,
				width: 0
			}, 500, 'easeOut');
			slider.fadeOut()
		}, 1000);
	}
);
}

jQuery.easing['jswing'] = jQuery.easing['swing'];
jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeOut: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
    easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	}
});
