// *************************************************************************************
// *** COUNTDOWN SCRIPT
// *** THIS SCRIPT WAS MODIFIED FROM A COUNTDOWN SCRIPT TAKEN FROM DYNAMICDRIVE.COM
// *************************************************************************************

// Create an array that will store all of the images used to represent numbers in the countdown
var clockNumbers = new Array(10);
	clockNumbers[0]="http://www.windowworldracing.com/images/image-countdown-number-0.gif";
	clockNumbers[1]="http://www.windowworldracing.com/images/image-countdown-number-1.gif";
	clockNumbers[2]="http://www.windowworldracing.com/images/image-countdown-number-2.gif";
	clockNumbers[3]="http://www.windowworldracing.com/images/image-countdown-number-3.gif";
	clockNumbers[4]="http://www.windowworldracing.com/images/image-countdown-number-4.gif";
	clockNumbers[5]="http://www.windowworldracing.com/images/image-countdown-number-5.gif";
	clockNumbers[6]="http://www.windowworldracing.com/images/image-countdown-number-6.gif";
	clockNumbers[7]="http://www.windowworldracing.com/images/image-countdown-number-7.gif";
	clockNumbers[8]="http://www.windowworldracing.com/images/image-countdown-number-8.gif";
	clockNumbers[9]="http://www.windowworldracing.com/images/image-countdown-number-9.gif";
// Modified Countdown Script Code
function cdtime(targetdate){
	if (!document.getElementById) return
	this.currentTime=new Date()
	this.targetdate=new Date(targetdate)
	this.timesup=false
	this.updateTime()
}
cdtime.prototype.updateTime=function(){
	var thisobj=this
	this.currentTime.setSeconds(this.currentTime.getSeconds()+1)
	setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
}
cdtime.prototype.displaycountdown=function(baseunit){
	this.baseunit=baseunit
	this.showresults()
}
cdtime.prototype.showresults=function(){
	var thisobj=this
	var timediff=(this.targetdate-this.currentTime)/1000 //difference btw target date and current date, in seconds
	if (timediff<0){ //if time is up
		this.timesup=true
		timeToImage(0, 0, 0)
		return
	}
	var oneMinute=60 //minute unit in seconds
	var oneHour=60*60 //hour unit in seconds
	var oneDay=60*60*24 //day unit in seconds
	var dayfield=Math.floor(timediff/oneDay)
	var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour)
	var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute)
	var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute))
	if (this.baseunit=="hours"){ //if base unit is hours, set "hourfield" to be topmost level
		hourfield=dayfield*24+hourfield
		dayfield="n/a"
	}
	else if (this.baseunit=="minutes"){ //if base unit is minutes, set "minutefield" to be topmost level
		minutefield=dayfield*24*60+hourfield*60+minutefield
		dayfield=hourfield="n/a"
	}
	else if (this.baseunit=="seconds"){ //if base unit is seconds, set "secondfield" to be topmost level
		var secondfield=timediff
		dayfield=hourfield=minutefield="n/a"
	}
	timeToImage(dayfield, hourfield, minutefield)
	setTimeout(function(){thisobj.showresults()}, 1000) //update results every second
}
// Print the results in a predefined location on the page
function timeToImage(myDays, myHours, myMinutes) {
	myDays = myDays.toString();
	myHours = myHours.toString();
	myMinutes = myMinutes.toString();
	if(myDays.length==1) { myDays = "00"+myDays; }
	if(myDays.length==2) { myDays = "0"+myDays; }
	if(myHours.length==1) { myHours = "0"+myHours; }
	if(myMinutes.length==1) { myMinutes = "0"+myMinutes; }
	document.getElementById("d1").innerHTML = '<img src="'+clockNumbers[myDays.charAt(0)]+'" height="28" width="20" />';
	document.getElementById("d2").innerHTML = '<img src="'+clockNumbers[myDays.charAt(1)]+'" height="28" width="20" />';
	document.getElementById("d3").innerHTML = '<img src="'+clockNumbers[myDays.charAt(2)]+'" height="28" width="20" />';
	document.getElementById("h1").innerHTML = '<img src="'+clockNumbers[myHours.charAt(0)]+'" height="28" width="20" />';
	document.getElementById("h2").innerHTML = '<img src="'+clockNumbers[myHours.charAt(1)]+'" height="28" width="20" />';
	document.getElementById("m1").innerHTML = '<img src="'+clockNumbers[myMinutes.charAt(0)]+'" height="28" width="20" />';
	document.getElementById("m2").innerHTML = '<img src="'+clockNumbers[myMinutes.charAt(1)]+'" height="28" width="20" />';
}