$(document).ready(function(){

	var fadeSpeed = 500;


	/* Page build
	-------------------------------------------------------------- */
	
	var contentContainer = $('#content');
	var whiteOverlays = $('#main-overlay, #header-overlay');
	
	// Fade the content in if not a slideshow
	whiteOverlays.delay(150).fadeOut(fadeSpeed);
	
	
	/* Page transition
	-------------------------------------------------------------- */	
	
	// If an anchor is clicked, capture the url and redirect
	var redirectURL;
	
	$('a:not(.no-transition)').click(function(){
	
		redirectURL = $(this).attr('href');
	
		// Fade out the content		
		whiteOverlays.fadeIn(fadeSpeed, function(){
			 document.location.href = redirectURL;
		});
		
		return false;

	});
	
	
	/* Slideshow
	-------------------------------------------------------------- */	
	
	$('#slideshow').cycle({
		fx: 'fade'
	});
	
	
	/* Replace image source on client list hover
	-------------------------------------------------------------- */
	
	var clientImages = $('#client-images li');
	var currentAnchor;
	var firstAnchor = $('#client-list a:first');
	var currentImage = clientImages.first();
	
	clientImages.hide();
	
	// On load we'll just take the first client
	
	currentImage.show();
	
	firstAnchor.addClass('selected');
	
	
	$('#client-list a').mouseover(function(){
	
		currentAnchor = $(this);
		
		// Remove all classes and add to current
		$('#client-list a').removeClass('selected');
		
		// Add selected class to current anchor
		currentAnchor.addClass('selected');
		
		entryID = currentAnchor.data('entry-id');
		
		imageClass = '.image-'+entryID;
		
		currentImage.fadeOut(100);
		
		currentImage = clientImages.filter(imageClass);
		
		currentImage.fadeIn(100);
		
		
	}).click(function(){
	
		return false;
	
	});
	
	
});


$(function () {
	/*
		News Page
	*/
	
	jQuery.easing.def = 'easeOutCubic';
	
	var current = 0;
	var blocks = $('#articles article');
	var blocks_len = blocks.length;
	var animate = {
		'speed': 600
	};

	var faded_opacity = 0.4;
	var hover_opacity = 0.8;
	var margin = ($(document).width() - 600)/2;
	
	var left = -200;
	blocks.each(function (i) {
		$(this).css('left', left + "px");
		left += parseInt($(this).css('width'), 10) + margin;
	});
	
	blocks.show().eq(current).animate({
			'opacity': 1
		}, 0, function(){
			if(jQuery.browser.msie) { $(this).get(0).style.removeAttribute('filter'); }
		}).siblings().css({
		'opacity': faded_opacity
	});
	
	
	$('#articles').css('height', Math.max.apply(Math, $.map(blocks, function (el) {
		return parseInt($(el).css('height'), 10) || 0;
	})));
	
	var moveTo = (function () {
		return $.map(blocks, function (el) {
			return parseInt($(el).css('left'), 10) || 0;
		});
	})();
	
	var opacitize = function () {
		blocks.eq(current).animate({
			'opacity': 1
		}, animate.speed, function(){
			if(jQuery.browser.msie) { $(this).get(0).style.removeAttribute('filter'); }
		}).siblings().animate({
			'opacity': faded_opacity
		}, animate.speed, function(){
			
		});
	};
	
	$('#articles article').each(function (i) {
		$(this).click(function () {
			if (current !== i) { // Not current article
				
				current = i;
				
				$('#articles_wrap').animate({
					'left': ((-1 * moveTo[current]) - 200) + "px"
				}, animate.speed);
				
				opacitize();
				
				return false;
			} else {
				return true;
			}
		});
		
		$(this).hover(function () {
			if (current !== i) {
				$(this).animate({
					'opacity': hover_opacity
				}, animate.speed, function(){
					
				});
			}
		}, function () {
			if (current !== i) {
				$(this).animate({
					'opacity': faded_opacity
				}, animate.speed, function(){
								
				});
			}
		});
	});
	
	
	
});


