var currentDate = 0;
var theDays = 0;
var theHours = 0;
var theMinuts = 0;
var theSeconds = 0;
function setCurrentDate(milliseconds) {
	currentDate = milliseconds;
}

function countdown_clock(year, month, day, hour, minute, format) {	       
	countdown(year, month, day, hour, minute, format);                
}

function counter() {
	currentDate = currentDate + 1000;
}

function init(milliseconds, year, month, day, hour, minute, format) {
	setCurrentDate(milliseconds);
	window.setInterval('counter()', 1000);
	countdown_clock(year, month, day, hour, minute, format);
}
         
function countdown(year, month, day, hour, minute, format) {
                                 
	Target_Date = (new Date(year, month, day, hour, minute, 00)).getTime();
	//alert(Target_Date + "\n" + currentDate);         
	// -- Find their difference, and convert that into seconds.                  
	Time_Left = Math.round((Target_Date - currentDate) / 1000);
         
	if(Time_Left < 0)
		Time_Left = 0;
         
	switch(format) {
		case 0:
                    //The simplest way to display the time left.
                    document.all.countdown.innerHTML = Time_Left + ' seconds';
                    break;
                case 1:
                    //More datailed.
                    days = Math.floor(Time_Left / (60 * 60 * 24));
                    Time_Left %= (60 * 60 * 24);
                    hours = Math.floor(Time_Left / (60 * 60));
                    Time_Left %= (60 * 60);
                    minutes = Math.floor(Time_Left / 60);
                    Time_Left %= 60;
                    seconds = Time_Left;
                    
                    dps = 's'; hps = 's'; mps = 's'; sps = 's';
                    //ps is short for plural suffix.
                    if(days == 1) dps ='';
                    if(hours == 1) hps ='';
                    if(minutes == 1) mps ='';
                    if(seconds == 1) sps ='';
                    var finalHTML = "";
			// -- document.all.countdown.innerHTML = days + ' day' + dps + ' ';			
			var theDays = getGraphicNumber(days, 3);
			//alert("days length = " + theDays.length + '\nDays = ' + days);
			if(document.all.daysDiv0.innerHTML != theDays[0]) document.all.daysDiv0.innerHTML = theDays[0];
			if(document.all.daysDiv1.innerHTML != theDays[1]) document.all.daysDiv1.innerHTML = theDays[1];
			if(document.all.daysDiv2.innerHTML != theDays[2]) document.all.daysDiv2.innerHTML = theDays[2];

			// -- document.all.countdown.innerHTML += hours + ' hour' + hps + ' ';
			var theHours = getGraphicNumber(hours, 2);
			if(document.all.hoursDiv0.innerHTML != theHours[0]) document.all.hoursDiv0.innerHTML = theHours[0];
			if(document.all.hoursDiv1.innerHTML != theHours[1]) document.all.hoursDiv1.innerHTML = theHours[1];

			// -- document.all.countdown.innerHTML += minutes + ' minute' + mps + ' and ';
			var theMinutes = getGraphicNumber(minutes, 2);
			if(document.all.minutesDiv0.innerHTML != theMinutes[0]) document.all.minutesDiv0.innerHTML = theMinutes[0];
			if(document.all.minutesDiv1.innerHTML != theMinutes[1]) document.all.minutesDiv1.innerHTML = theMinutes[1];

			// -- document.all.countdown.innerHTML += seconds + ' second' + sps;
			var theSeconds = getGraphicNumber(seconds, 2);
			document.all.secondsDiv0.innerHTML = theSeconds[0];
			document.all.secondsDiv1.innerHTML = theSeconds[1];

                    break;
               default: 
                    //document.all.countdown.innerHTML = Time_Left + ' seconds';
         }
               
	//Recursive call, keeps the clock ticking.
	setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + format + ');', 1000);
}

function getGraphicNumber(number, digits) {
	// -- Split the string into each individual character
	var digit_array = Array(digits);
	number = number + '';
	var number_array = number.split("");
	var difference = digits - number_array.length;
	// -- variable to hold the full string of image tags
	var HTML = "";
	if(digits > number_array.length) {
		//digits - number_array.length
		for(var i = 0; i <= digits - number_array.length; i++) {
		//for(var i = (digits - number_array.length); i <= digits - number_array.length; i++) {
			HTML += "<img src=\"images/clock/0.gif\" width=\"27\" height=\"36\">";
			digit_array[i] = '<font style="font-family:arial;font-weight:bold;font-size:36pt;">0</font>';
		}
	}
	for(var i = 0; i < number_array.length; i++) {
		HTML += "<img src=\"images/clock/" + number_array[i] + ".gif\" width=\"27\" height=\"36\">";
		digit_array[difference + i] = '<font style="font-family:arial;font-weight:bold;font-size:36pt;">' + number_array[i] + '</font>';
		//alert('digit_array[' + ( difference + i) + '] = ' + digit_array[i] + '\nand\nnumber_array[' + i + '] = ' + number_array[i]);
		if(number_array[i] == "0") {
			//alert(HTML);
		}
	}
	//alert(HTML);
	return digit_array;
}