// Global configuration variable
var timerResolution = 200;	// Less than a second is best, to avoid the appearance of "skipped" seconds.

// Pre-load images
var image1 = new Image();
image1.src = "http://media.xfire.com/images/codmw/0.png";
var image2 = new Image();
image2.src = "http://media.xfire.com/images/codmw/1.png";
var image3 = new Image();
image3.src = "http://media.xfire.com/images/codmw/2.png";
var image4 = new Image();
image4.src = "http://media.xfire.com/images/codmw/3.png";
var image5 = new Image();
image5.src = "http://media.xfire.com/images/codmw/4.png";
var image6 = new Image();
image6.src = "http://media.xfire.com/images/codmw/5.png";
var image7 = new Image();
image7.src = "http://media.xfire.com/images/codmw/6.png";
var image8 = new Image();
image8.src = "http://media.xfire.com/images/codmw/7.png";
var image9 = new Image();
image9.src = "http://media.xfire.com/images/codmw/8.png";
var image10 = new Image();
image10.src = "http://media.xfire.com/images/codmw/9.png";
var image11 = new Image();
image11.src = "http://media.xfire.com/images/codmw/colon.png";
var image12 = new Image();
image12.src = "http://media.xfire.com/images/codmw/days.png";
var image13 = new Image();
image13.src = "http://media.xfire.com/images/codmw/ad_codmw_background_1.jpg";
var image14 = new Image();
image14.src = "http://media.xfire.com/images/codmw/ad_codmw_background_2.jpg";
var image15 = new Image();
image15.src = "http://media.xfire.com/images/codmw/day.png";

// Set up the start/countdown/end times
var adStartTime = new Date();
adStartTime.setFullYear( 2007, 9, 31 );
adStartTime.setHours( 0 );
adStartTime.setMinutes( 0 );
adStartTime.setSeconds( 0 );

var adEndTime = new Date();
adEndTime.setFullYear( 2007, 10, 7 );
adEndTime.setHours( 0 );
adEndTime.setMinutes( 0 );
adEndTime.setSeconds( 0 );

var adCountdownEndTime = new Date();
adCountdownEndTime.setFullYear( 2007, 10, 6 );
adCountdownEndTime.setHours( 0 );
adCountdownEndTime.setMinutes( 0 );
adCountdownEndTime.setSeconds( 0 );

var adCountdownTimerNow = new Date();

// Update the clock, which will set our time and loop until done (via timeouts)
UpdateCODMWClock();

/****** END JS CODE, BEGIN FUNCTIONS *****/

function UpdateCODMWClock()
{
	var adCountdownTimerNow = new Date();	// update the current time
	
	var theAd = document.getElementById( "ad_codmw" );
	var theTimer = document.getElementById( "ad_codmw_timer" );
	var theTimerEnd = document.getElementById( "ad_codmw_timer_end" );
	
	if( !theAd ||
		!theTimer ||
		!theTimerEnd )
	{
		return;
	}
	
	if( adCountdownTimerNow < adStartTime )
	{
		theAd.style.display = "none";
		setTimeout( "UpdateCODMWClock();", timerResolution );	// Check back in a little while to see if it's time yet
		return;
	}
	
	if( adCountdownTimerNow > adEndTime )
	{
		theAd.style.display = "none";
		return;	// NOTE: not restarting the timeout, since the ad is now over.
	}
	
	theAd.style.display = "block";
	
	if( adCountdownTimerNow < adCountdownEndTime )
	{
		// We're still showing the timer
		theTimer.style.display = "block";
		theTimerEnd.style.display = "none";
		
		var day1 = document.getElementById( "ad_codmw_day1" );
		var day2 = document.getElementById( "ad_codmw_day2" );
		var days = document.getElementById( "ad_codmw_days" );
		var hour1 = document.getElementById( "ad_codmw_hour1" );
		var hour2 = document.getElementById( "ad_codmw_hour2" );
		var colon1 = document.getElementById( "ad_codmw_colon1" );
		var minute1 = document.getElementById( "ad_codmw_minute1" );
		var minute2 = document.getElementById( "ad_codmw_minute2" );
		var colon2 = document.getElementById( "ad_codmw_colon2" );
		var second1 = document.getElementById( "ad_codmw_second1" );
		var second2 = document.getElementById( "ad_codmw_second2" );
		
		if( !day1 ||
			!day2 ||
			!hour1 ||
			!hour2 ||
			!colon1 ||
			!minute1 ||
			!minute2 ||
			!colon2 ||
			!second1 ||
			!second2 )
		{
			// Probably better not to show anything if we're puking here.
			theTimer.style.display = "none";
			return;
		}

		// Determine the remaining time to the countdown end
		var timeLeft = new Date();
		timeLeft.setTime( adCountdownEndTime.getTime() - adCountdownTimerNow.getTime() + 1000 );	// +1000 ms so that the countdown ends after 00:00:01, instead of after 00:00:00.
		
		// This next section only works because our time to represent in the countdown will never be larger than 24 hours.
		// If we want a H:M:S format, but might have more than 24 hours, we'd need to do extra conversion for the years/months/days in the object.
		var daysLeft = timeLeft.getUTCDate() - 1;	// 1-based, but we want 0-based
		var hoursLeft = timeLeft.getUTCHours();
		var minutesLeft = timeLeft.getUTCMinutes();
		var secondsLeft = timeLeft.getUTCSeconds();

		day1.className = "ad_codmw_digit_" + Math.floor( daysLeft / 10 );
		day2.className = "ad_codmw_digit_" + daysLeft % 10;
		
		if( daysLeft % 10 == 1 )
			days.className = "ad_codmw_day";
		else
			days.className = "ad_codmw_days";
		
		hour1.className = "ad_codmw_digit_" + Math.floor( hoursLeft / 10 );
		hour2.className = "ad_codmw_digit_" + hoursLeft % 10;
		colon1.className = "ad_codmw_colon";
		minute1.className = "ad_codmw_digit_" + Math.floor( minutesLeft / 10 );
		minute2.className = "ad_codmw_digit_" + minutesLeft % 10;
		colon2.className = "ad_codmw_colon";
		second1.className = "ad_codmw_digit_" + Math.floor( secondsLeft / 10 );
		second2.className = "ad_codmw_digit_" + secondsLeft % 10;
	}
	else
	{
		// The timer has ended, and we're only showing the "timer end" message
		theTimer.style.display = "none";
		theTimerEnd.style.display = "block";
	}
	
	setTimeout( "UpdateCODMWClock();", timerResolution );	// Do it all again in a little while
}
