var timeouts = [];
function sunIn () {
	$('#sun li').slice(5).each(function (i) {
		(function (el, i) {
			timeouts[i] = setTimeout(function () {
				$(el).fadeIn('fast');
			}, i * 200);
		})(this, i);
	});
	sunInTimeout = setTimeout(function () {
		sunOut();
	}, 1200);
}
function sunOut () {
	$('#sun li').slice(5).each(function (i) {
		(function (el, i) {
			timeouts[i] = setTimeout(function () {
				$(el).fadeOut('fast');
			}, (5-i) * 200);
		})(this, i);
	});
	sunOutTimeout = setTimeout(function () {
		sunIn();
	}, 1200);
}
function reset () {
	for (var i = 0, len = timeouts.length; i < len; i++) {
		clearTimeout(timeouts[i]);
	}
	clearTimeout(sunInTimeout);
	clearTimeout(sunOutTimeout);
}
		
function homeAnimation() {

	$('#home #petals li, #home #text li.last, #home #sun li, #home #replay').hide();
	$('#home #pencil').css({top: '581px'});
	$('#home #hills').css({bottom: '-191px'});

	$('#pencil').animate({top: '246px'}, 2000);

	window.setTimeout(function() {
		$('#petals li').each(function(i) {
			$(this).delay(1250*i).fadeIn(1250);
		});
	}, 2000);

	/*window.setTimeout(function() {
		var li = $('#links li:first, #petals li:first').addClass('hover');
		var chainInterval = setInterval(function () {
			li.removeClass('hover');
			if (li.next('li')) {
				li = li.next().addClass('hover');
			} else {
				clearInterval(chainInterval);
			}
		}, 500);
	}, (2400*4)+1400);*/

	window.setTimeout(function() {
		$('#text li:not(.last)').each(function(i) {
			$('#text-overlay').delay(600*i).fadeIn(600, function() {
				$('#text li:eq(' + i + ')').show().siblings().hide();
			});
			$('#text-overlay').fadeOut(600);
		});
	}, 1400);
	window.setTimeout(function() {
		$('#text-overlay').fadeIn(600, function() {
			$('#text li.last').show().siblings().hide();
			$('#text-overlay').fadeOut(600);
		});
	}, (2400*4)+1400);

	window.setTimeout(function() {
		$('#hills').animate({bottom: '0'}, 2000);
	}, (2400*4)+1400+600);

	window.setTimeout(function() {
		$('#sun li').each(function(i) {
			$(this).delay(100*i).fadeIn(100);
		});
	}, (2400*4)+1400+1400);

	window.setTimeout(function() {
		sunIn();
	}, 13000);

	window.setTimeout(function() {
		$('#replay').fadeIn();
	}, 14000);

}
		
function homePulse() {
	sunIn();
}

$(document).ready(function() {

	$('html').addClass('js');
	
	$('a.ext').attr('target','_blank').attr('title','Opens new window');

	if($('#home').length) {
	
		var theReferrer = document.referrer;
		if(theReferrer.match(/c21.local/) || theReferrer.match(/c21.jamessui.com/) || theReferrer.match(/c-21.co.uk/)) {
			homePulse()
		} else {
			homeAnimation();
		}		
		
		$('#home #replay').click(function() {
			reset();
			homeAnimation();
			return false;
		});

		$('#map area').mouseover(function() {
			var thisIndex = $(this).index('#map area');
			$('#links li:eq(' + thisIndex + '), #petals li:eq(' + thisIndex + ')').addClass('hover');
		}).mouseout(function() {
			var thisIndex = $(this).index('#map area');
			$('#links li:eq(' + thisIndex + '), #petals li:eq(' + thisIndex + ')').removeClass('hover');
		});

		$('#sun li, #text li.last').addClass('linked').click(function() {
			window.location = '/about-us/';
			return false;
		});

	}

	/*$('#people #listing article:first-child').siblings().hide();
	$('#people .menu li:first-child').addClass('on');
	$('#people .menu li').click(function() {
		if(!$(this).hasClass('on')) {
			$(this).addClass('on').siblings().removeClass('on');
			var theItem = $(this).index('#people .menu li');
			$('#people #listing article:eq(' + theItem + ')').slideDown('slow').siblings().slideUp('slow');
		}
		return false;
	});*/
	$('#people #listing .images').mouseover(function() {
		$(this).find('img.alt').stop(true, true).fadeOut();
	}).mouseleave(function() {
		$(this).find('img.alt').stop(true, true).fadeIn();
	});

	$('#work-menu ul.nav li a').click(function() {
		$(this).parent().find('ul').fadeIn();
		$(this).parent().siblings().find('ul').hide();
		$('#work-menu ul.nav li ul li').removeClass('on');
		$(this).parent().find('li:first-child').addClass('on');
		$(this).parent().addClass('on').siblings().removeClass('on');
		var moveX = $(this).data('x');
		var moveY = $(this).data('y');
		$('#work-panel').scrollTo({
			top: moveX,
			left: moveY
		}, 800);
		$('#work-panel .holder .drag').animate({
			top: '0',
			left: '0'
		});
		return false;
	});

	$('#work-panel').scrollTo({
		top: '2560px',
		left: '2390px'
	}, 0);

	$('#work-panel a.media').media({caption: false});

	$('#work-panel .holder .drag').draggable({cancel: '.no-drag'});
	
	$('#work-panel .holder .items .item .rotate').cycle();
	
	$("#work-panel a.colorbox").colorbox();

});

Cufon.replace('#about-us h1, #work h1, #people h1 strong, #contact h1, #brand-science h1, #blog h1.blog, #blog h1.news');
