$(function() {
	$('.nav select').change(function() {
		window.location = $('.nav select').val();
	});
	
	$('#sheep').append('<li id="magic-line"></li>');
    
	setTimeout("magic_line()", 250);
    
    // Follow it as we move along
    $('#sheep a').hover(function(){
    	var offset = $(this).offset();
    	var width = $(this).width();
    	$('#magic-line').clearQueue();
    	$('#magic-line').animate({
    		left: offset.left,
    		width: width
    	}, 100);
    	//$('#magic-line').width($(this).width());
    	//$('#magic-line').offset({ left: offset.left });
    });
    
    $('.nav').hover(function() {}, function() {
    	var active = $('.active').offset();
    	var magic = $('#magic-line').offset();
    	if (active.left != magic.left)
    	{
    		$('#magic-line').clearQueue();
	    	$('#magic-line').animate({
	    		left: active.left,
	    		width: $('.active').width()
	    	}, 100);
    	}
    });
    
    magic_move();
    
	$('#sheep').click(function(){
		$('.active').removeClass('active');
		$('.current').removeClass('current');
		$(this).parent('li').addClass('active').addClass('current');
	});
	
	// Rollover
	if ( ! Modernizr.touch){
		$('.clients li img').each(function(){
			$(this).attr('src', $(this).attr('data-greyscale'));
		});
		
		$('.clients li').hover(function(){
			$(this).prepend('<img src="'+$(this).children('img.original').attr('data-colour')+'" alt="" style="display:none;z-index:1;" class="colour" />');
			$(this).children('.colour').fadeIn('fast');
		}, function() {
			$(this).children('.colour').fadeOut('fast', function() {
				$(this).children('.colour').remove();
			});
		});
	}
	
	if ($('body').hasClass('welcome') && $('html').hasClass('no-touch') && ( ! $('html').hasClass('oldie')) ){
		$('.banner').delay(150).animate({
			opacity: 1,
			'margin-left': '0'
		}, 1000);
		$('.deck h1').delay(300).animate({
			opacity: 1,
			'margin-left': '0'
		}, 1000);
		$('.deck p').each(function(i) {
			$(this).delay(450).animate({
				opacity: 1,
				'margin-left': '0'
			}, 1000);
		});
	}
	
	$('#top').click(function() {
		$('html, body, .content').animate({scrollTop: 0}, 500);
		return false;
	});
	
	$('.our-work img').parent('p').addClass('shadow');
	
	$('.datepicker').datepicker({
		dateFormat: 'yy-mm-dd'
	});
});

$(window).resize(function(){
	magic_line();
});

function magic_line() {
	$('#magic-line').width($('.current a').width());
	var offset = $('.current').offset();
	$('#magic-line').width($('.current').width());
	$('#magic-line').offset({ left: offset.left });
	$('#magic-line').fadeIn();
}

function magic_move() {
	$('#sheep').parent('nav').hover(function(){}, function(){
		var offset = $('.current').offset();
		$('#magic-line').width($('.current').width());
		$('#magic-line').offset({ left: offset.left });
	});
}
