$(document).ready(onCommonDocReady);

function onCommonDocReady(){
	$('#background_shuffle img').each(
		function(index) {
			$(this).attr({src:$(this).attr("rel")});
		}
	);
	$('#background_shuffle img').bind('load', onBackgroundResize);
	
    $('#background_shuffle').cycle({
		fx: 'fade',
		speed: 200,
		timeout: 5000
	});
    
    $(window).bind('resize', onBackgroundResize);
    onBackgroundResize();
	
    $('#navigation ul li.news a').text("");
	$('#showDesktop').bind('mouseenter', onShowDesktopOver);
	$('#showDesktop').bind('mouseleave', onShowDesktopOut);

	$('#navigation ul li').not('ul li ul li').not('ul li ul li ul li').bind('mouseenter', onMainNavOver);
	$('#navigation ul li').not('ul li ul li').not('ul li ul li ul li').bind('mouseleave', onMainNavOut);
	
	//the main navigation li needs to be clickable with the containing a
	$('#navigation ul li').not('ul li ul li').not('ul li ul li ul li').bind('click', onLiClicked);
	
	// als er subs zijn, add class
	$('#navigation ul li:has(ul)').addClass("withsub");
	
	Cufon.replace('h1');
	Cufon.replace('#tagline');
	Cufon.replace($('#navigation ul li a').not('ul li ul li a'), {hover:true});
}

function onBackgroundResize(e){
	$('#background_shuffle img').each(resizeImage);
}

function resizeImage(){
	var jQueryImg = $(this);
	
	jQueryImg.css('width', 'auto');
	jQueryImg.css('height', 'auto');
	
	var imgWidth =  jQueryImg.width();
	var imgHeight =  jQueryImg.height();
	
	var aspectRatio = imgWidth / imgHeight;
	
	var scaleX = $(window).width() / imgWidth;
	var scaleY = $(window).height() / imgHeight;
	var scale = Math.max(scaleX, scaleY);
	
	jQueryImg.css('width', imgWidth*scale);
	jQueryImg.css('height', imgHeight*scale);
	
	// center the image
	$(this).css('left', (($(window).width()-(imgWidth*scale))/2));
	
	// align right
	//jQueryImg.css('left', $(window).width()-(imgWidth*scale));
}

function onMainNavOver(e){
	$('#navigation ul li a').removeClass('hover');
	$(e.target).children().filter('a').addClass('hover');
	Cufon.refresh($(e.target).children().filter('a'));
}
function onMainNavOut(e){
	$('#navigation ul li a').removeClass('hover');
	Cufon.refresh($('#navigation ul li a'));
}
function onLiClicked(e){
	var link = $(e.target).children().attr('href');
	document.location = link;
}
function onShowDesktopOver(){
	$('#content').fadeOut();
}
function onShowDesktopOut(){
	$('#content').fadeIn();
}