jQuery(document).ready(function(){

	// Switches the work case
	function switchCase(){
		jQuery(".case:gt(0)").hide();
		jQuery("#more-projects").css({display:'block'});
		jQuery(".case:eq(0)").wrap('<div id="current-project"></div>');
		
		var currentProject = jQuery(".case:eq(0)").attr("class").split(" ");
		
		jQuery("body").data("current-project",currentProject[1]);
		jQuery("#more-projects a").live("click",function(e){
			var project = jQuery(this).attr("class").split(" ");
			var viewingProject = jQuery("#current-project div.case");
			
			if(jQuery("body").data("current-project") != project[0]) {
				jQuery("body").data("current-project", project[0]);
				jQuery('html, body').animate({
					scrollTop:jQuery("#work").offset().top
				},300,function(){
					viewingProject.fadeOut(300,function(){
						jQuery("#current-project").append(jQuery("div.case." + project[0]));
						jQuery(viewingProject).insertAfter("#current-project");
						jQuery("#current-project div.case").fadeIn(300);
					});
				});
			}
			return false;
			e.preventDefault();
		});
	}
	switchCase();

	// Menu stuff
	jQuery('#main-navigation').addClass('js hide');
	var activePosition;
	var menuPosition;	
	var updatePositions = function() {
		activePosition = jQuery('#main-navigation li.active a').offset();
		menuPosition = jQuery('#main-navigation').offset();
		activePosition.left -= menuPosition.left;
	}
	updatePositions();

	jQuery(window).resize(function(e){
		menuPosition = jQuery('#main-navigation').offset();
	});
	
	jQuery('#main-navigation li a').hover(function(e){
		var position = jQuery(this).offset();
		position.left -= menuPosition.left;		
		jQuery(this).parents('#main-navigation').stop().animate({ 'backgroundPosition': position.left + 'px 0px'}, 300, function(){
		});
	}, function(){
		jQuery(this).parents('#main-navigation').stop().animate({ 'backgroundPosition': activePosition.left + 'px 0px'});
	});
	
	jQuery('#main-navigation li a').click(function(e){
		jQuery(this).parents('#main-navigation').find('li').each(function(){
			jQuery(this).removeClass('active');
		});
		jQuery(this).parent().addClass('active');
		updatePositions();
	});	


	// Removes the br from Contact Form 7
	jQuery('span + br').remove();

	// Kickstarts the fancybox
	jQuery(".slider a").fancybox({
		'overlayShow'	: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'fade',
		'overlayShow' 	: true,
		'overlayColor'	: '#fff',
		'overlayOpacity': '0.7',
		'showNavArrows'	: true
	});

	// Show the values of the shortfact list	
	jQuery('#short-facts li').hover(function(){
		jQuery(this).find('.key').hide();
		jQuery(this).find('.value').show();
	}, function(){
		jQuery(this).find('.key').show();
		jQuery(this).find('.value').hide();		
	});	
	
	// Smoothscrolling
	function navigationScroll(){
		jQuery("a").live("click",function(){
			var destination = jQuery(this).attr("href");
			jQuery('html, body').animate({
				scrollTop:jQuery(destination).offset().top
			}, 800, 'easeInOutExpo');
			return false;
		});
	}
	navigationScroll();

	// Scroll detector
	jQuery('#start-primary').waypoint(function(event, direction) {
		if (direction === 'down') {
			jQuery('.to-top').animate({ 'top': '20px' }, 300);
		}
		else {
			jQuery('.to-top').animate({ 'top': '-40px' }, 300);
			jQuery('#main-navigation li').each(function(){
				jQuery(this).removeClass('active');
			});
			jQuery('#main-navigation').stop().animate({ 'backgroundPosition': '0px 0px'});
			jQuery('#main-navigation li:first-child').addClass('active');
			updatePositions();			
		}	
	}, { offset: '8%' });	

});
