$(document).ready( function() {
	var bannerTimer;

	// cancel firefox drag images
	$('.services img').mousedown(function(event) {
		if(event.preventDefault)
			event.preventDefault();
	});

	$('#doBanner').click(function() {
		// cancel the bannerTimer
		clearTimeout(bannerTimer);
		$(this).fadeOut(500, function() {
			$('#servicesWrapper').fadeIn(1000);
		});
	});
  
	$('.bar').click(function() {
		var currentSlide = $('.slide.active');
		animateSlide(currentSlide, $(this).parent());
	});
	
	$('.copy a').click(function(event) {
		event.preventDefault();
		var currentSlide = $('.slide.active');
		if (!$('#service-'+$(this).attr('rel')).hasClass('active'))
		{
			$('html, body').animate({scrollTop:0}, 'fast');
			animateSlide(currentSlide, $('#service-'+$(this).attr('rel')));
		}
	});
	
	// timed animation for the banner
	if ($.query.get('s'))
	{
		$('#doBanner').fadeOut(100, function() {
			$('#servicesWrapper').fadeIn(500); 
		});
	} else {
		bannerTimer = setTimeout('animateBanner()', 600);
	}
});

function animateSlide(oldSlide, newSlide) {
	// hide the old copy, show the old bar
	// show the new copy, hide the new bar

	$(newSlide.children('.bar')[0]).hide('blind',{ direction: 'horizontal' }, 300, function() {
		$(oldSlide.children('.copy')[0]).hide('blind',{ direction: 'horizontal' }, 600, function() {
			$(newSlide.children('.copy')[0]).show('blind', { direction: 'horizontal' }, 600);
			$(oldSlide.children('.bar')[0]).show('blind', { direction: 'horizontal' }, 300, function() {
				oldSlide.removeClass('active').addClass('inactive');
				newSlide.removeClass('inactive').addClass('active');	
			});
		});
	});
}

function activateSlide(slide) {
	slide.children().each( function(i) {
		var self = $(this);
		if (self.hasClass('bar')) {
			self.animate({width: '0px', opacity: 0.0}, 400, 'linear', function(){ self.hide(); });
		}
		if (self.hasClass('copy')) {
			//self.show();
			self.animate({width: '430px', opacity: 1.0}, 600 );
		}
	});
	slide.removeClass('inactive').addClass('active');
}

function deactivateSlide(slide) {
	slide.children('.copy').animate(
		{width: '0px', opacity: 0.0}, 400, 'linear', 
		function(){
			//$(this).hide();
			slide.children('.bar').animate({width: '101px', opacity: 1.0}, 200 );
		}
	);
	slide.removeClass('active').addClass('inactive');
}

function animateBanner() {
	// fade the blocks in randomly
	var pauseTime = 0;
	$.shuffle($('#doBanner .block')).each(function (i) {
		$(this).animate({opactiy: 0}, pauseTime+=600).fadeIn(300);
	});
	$('#doBanner').animate({opactiy: 0}, pauseTime+=2500).fadeOut(500, function() {
		$('#servicesWrapper').fadeIn(1000);
	});
}


/*
 * jQuery shuffle
 *
 * Copyright (c) 2008 Ca-Phun Ung <caphun at yelotofu dot com>
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://yelotofu.com/labs/jquery/snippets/shuffle/
 *
 * Shuffles an array or the children of a element container.
 * This uses the Fisher-Yates shuffle algorithm <http://jsfromhell.com/array/shuffle [v1.0]>
 */
 
(function($){

	$.fn.shuffle = function() {
		return this.each(function(){
			var items = $(this).children().clone(true);
			return (items.length) ? $(this).html($.shuffle(items)) : this;
		});
	}
	
	$.shuffle = function(arr) {
		for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
		return arr;
	}
	
})(jQuery);