/********************************************************************************/
/***** REL EXTERNAL ************************************************************/
/******************************************************************************/

Cufon.replace('.home-footer .left p');


/********************************************************************************/
/***** IMAGE ROTATOR ***********************************************************/
/******************************************************************************/


datetoday = new Date();
timenow = datetoday.getTime();
datetoday.setTime(timenow);
thehour = datetoday.getHours();

if(thehour >= 5 && thehour < 12) { activeImg = 0; }
if(thehour >= 12 && thehour < 18) { activeImg = 1; }
if((thehour >= 18 && thehour < 24) || (thehour >= 0 && thehour < 5)) { activeImg = 2; }


var startDelay 	= 1650;
var timeFade	= 2000;
var timeActive	= 3000;
var timeActive2 = 5000;
var timerStart;
var transition 	= 'off';

$(function() {
	injectGuardianItem(function() {
		nextStep = activeImg+1
		timerStart = setTimeout('imageRotator('+nextStep+')', startDelay);
	});
});

/***** GET THE FUNCTIONS STARTED *****/
// LM: Sorry, again. No time.
function injectGuardianItem(callback) {
	$(".image-rotator div.guardian").clone().hide().insertAfter($(".image-rotator div:not(.guardian):not(:last)"));
	callback();
}

function imageRotator(id) {
	if(transition == 'off') {
		transition = 'on';
		
		clearTimeout(timerStart);

		var imageList = $('.image-rotator div');
		
		if((id) == imageList.length) {
			var nextStep = 0;
		} else {
			var nextStep = id;
		}
		
		var imageActive	= $(imageList[activeImg]);
		var imageNext	= $(imageList[nextStep]);
		
		$(imageActive).css({'zIndex':'3'});
		$(imageNext).css({'zIndex':'1'});
		
		$(imageNext).show().css({'opacity':0});
		
		// LM: Sorry. Didn't have much time.
		if($(imageNext).hasClass("guardian")) {
			pauseTime = timeActive2;
		} else {
			pauseTime = timeActive;
		}

		$(imageActive).animate({'opacity':0},timeFade)
		.add($(imageNext).animate({'opacity':0.999},timeFade, function() {
				var nextStep2 = nextStep + 1;
				$(imageActive).hide();
				activeImg = nextStep;
				transition = 'off';
				timerStart = setTimeout('imageRotator(' + nextStep2 + ')', pauseTime);
			})
		);
	}
}
