jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function removeZeroValues(gameCountVar)
{
	// remove the markers for 0 values
	if (this[gameCountVar + "_remainingdays"] == 0) 
	{
		$('.countdown' + gameCountVar + ' .cd1').fadeOut(800);
		$('.countdown' + gameCountVar + ' .ct1').fadeOut(800);
		
		if (this[gameCountVar + "_remaininghours"] == 0) {
			$('.countdown' + gameCountVar + ' .cd2').fadeOut(800);
			$('.countdown' + gameCountVar + ' .ct2').fadeOut(800);
		}
	}
}

$(document).ready( function(){
	//Preload the countdown Image
	$.preloadImages("/player/g/bespoke/countdown_slidebg.gif");
	$('.c30.cd').each(function(index){
		$.gameCountName = $(this).attr('class').slice(16);
		setVariables($.gameCountName);
		startcountdown($.gameCountName);
	});
});	


function setVariables(gameCountVar){
	/* Populate these vars with variables from HTML page */
	this[gameCountVar + "_secs_til_drawbreak"] 	   = this[gameCountVar + "_cdsecstildrawbreak"];
}

function populateCountDown(gameCountVar)
{	
	//Populate the date into the boxes
	$('.countdown' + gameCountVar + ' .ct1 span').text(this[gameCountVar + "_remainingdays"]);
	$('.countdown' + gameCountVar + ' .ct2 span').text(this[gameCountVar + "_remaininghours"]);
	$('.countdown' + gameCountVar + ' .ct3 span').text(this[gameCountVar + "_remainingmins"]);
}

function changeLabelsForSingulars(gameCountVar)
{
	//Change labels for singular references e.g. day becomes days if there is more than 1 day left
	if(this[gameCountVar + "_remainingdays"] == 1)
	{
		$('.countdown'+gameCountVar+' .cd1').text(dayvar);
	}
	if(this[gameCountVar + "_remaininghours"] == 1)
	{
		$('.countdown'+gameCountVar+' .cd2').text(hourvar);
	}
	if(this[gameCountVar + "_remainingdays"] != 1)
	{
		$('.countdown'+gameCountVar+' .cd1').text(daysvar);
	}
	if(this[gameCountVar + "_remaininghours"] != 1)
	{
		$('.countdown'+gameCountVar+' .cd2').text(hoursvar);
	}
	if(this[gameCountVar + "_remainingmins"] != 1)
	{
		$('.countdown'+gameCountVar+' .cd3').text(minutesvar);
	}		
	if(this[gameCountVar + "_remainingmins"] == 1)
	{
		$('.countdown'+gameCountVar+' .cd3').text(minutevar);
	}	
}


function backGroundInitialiser(gameCountVar, tagNumber)
{	
	// Changes the background position of the countdown gif to simulate an animation
	//alert("Calling bGI");
	$('.countdown' + gameCountVar+' .ct' + tagNumber).animate({
			backgroundPosition: '0 -50px'
		}, 80).animate({
			backgroundPosition: '0 -100px'
		}, 80).animate({
			backgroundPosition: '0 -150px'
		}, 80).animate({
			backgroundPosition: '0 -200px'
		}, 80).animate({
			backgroundPosition: '0 -250px'
		}, 80).animate({
			backgroundPosition: '0 0'
		}, 80);
}

function calculateTimeLeft(gameCountVar)
{
	var drawDate = this[gameCountVar + "_drawdate"];
	var now = new Date().getTime();

	var secondsLeft = Math.floor((drawDate - now) / 1000);
	this[gameCountVar + "_remainingsecs"] = secondsLeft % 60;
	
	var minutesLeft = Math.floor(secondsLeft / 60);
	this[gameCountVar + "_remainingmins"] = minutesLeft % 60;
	
	var hoursLeft = Math.floor(minutesLeft / 60);
	this[gameCountVar + "_remaininghours"] = hoursLeft % 24;
	
	var daysLeft = Math.floor(hoursLeft / 24);
	this[gameCountVar + "_remainingdays"] = daysLeft;
}


function startcountdown(gameCountVar)
{
	var localTime = new Date().getTime();
	var drawDate = localTime + (this[gameCountVar + "_secs_til_drawbreak"] * 1000);
	this[gameCountVar + "_drawdate"] = drawDate;
	
	// recalculate the number of seconds left in this minute
	var tempstring = "initialisecountdown('" + gameCountVar + "')";	
	var timeoutDelay = (this[gameCountVar + "_remainingsecs"] * 1000);
	setTimeout(tempstring, timeoutDelay);
}


function initialisecountdown(gameCountVar)
{
	calculateTimeLeft(gameCountVar);
	
	backGroundInitialiser(gameCountVar, 3);
	if (this[gameCountVar + "_remainingmins"] == 59)
	{
		backGroundInitialiser(gameCountVar, 2);
		if (this[gameCountVar + "_remaininghours"] == 23)
		{
			backGroundInitialiser(gameCountVar, 1)
		}
	}
	
	changeForDrawBreak(gameCountVar);
	populateCountDown(gameCountVar);
	changeLabelsForSingulars(gameCountVar);
	removeZeroValues(gameCountVar);
		
	// recalculate the number of seconds left in this minute
	var tempstring = "initialisecountdown('" + gameCountVar + "')";	
	var timeoutDelay = (60 * 1000);
	//var timeoutDelay = (this[gameCountVar + "_remainingsecs"] * 1000);
	setTimeout(tempstring, timeoutDelay);
}


function changeForDrawBreak(gameCountVar)
{
	if ((this[gameCountVar + "_remainingmins"] == 0) 
		&& (this[gameCountVar + "_remaininghours"] == 0) 
		&& (this[gameCountVar + "_remainingdays"]  == 0))
	{
		$('.countdown'+gameCountVar+'  .countdownMidAlign dt').fadeOut(100);
		$('.countdown'+gameCountVar+'  .countdownMidAlign dd').fadeOut(100);
		$('.countdown'+gameCountVar+'  .countdownTop strong.newtag').fadeOut(800);
		$('.countdown'+gameCountVar+' p.drawstatement').text(this[gameCountVar + "_closetext"]);
		$('.countdown'+gameCountVar+'  .countdownTop p').fadeOut(800);
		$('.countdown'+gameCountVar+'  .countdownBot p').fadeOut(800);
		$('.countdown'+gameCountVar+'  p.drawstatement').fadeIn(800);
	}
}


