var currentGPOffset = 0;
var now;

function getNow()
{
	now = new Date();
//	now.setTime(now.getTime() + 74.88 * 60 * 60 * 1000);
}

getNow();

function GPArrayItem(n, d)
{
	this.name = n;
	this.start = new Date(d);
	this.end = new Date;
	this.end.setTime(this.start.getTime() + 2 * 60 * 60 * 1000);
	this.countdown = new Date(0);
	this.inProgress = false;
	return this;
}

function GPCurrent()
{
	var i, ret;
  //   alert(GPCurrent);
	for (i = currentGPOffset; i < grand_prix.length; i++)
	{
		ret = grand_prix[i];
//		alert(i +'..'+ret.start +'///' + ret.end);
		ret.countdown = new Date(ret.start - now);
//		ret.countdown = days_between(new Date(ret.start) - now)*24*60;
//		alert(i +'..'+ret.countdown +'///' + ret.name +'///' + ret.start +'///' + now);

		if (ret.start > now)
		{
			ret.inProgress = false;
			currentGPOffset = i;
//			alert(i);
			return ret;
		}
		else if (ret.end >= now)
		{
			ret.inProgress = true;
			currentGPOffset = i;
//			alert(i +'*');
			return ret;
		}
	}

	currentGPOffset = i;
}

function days_between(date1, date2) {
  // The number of milliseconds in one day
  var ONE_DAY = 1000 * 60 * 60 * 24

  // Convert both dates to milliseconds
  var date1_ms = date1.getTime()
  var date2_ms = date2.getTime()

  // Calculate the difference in milliseconds
  var difference_ms = Math.abs(date1_ms - date2_ms)

  // Convert back to days and return
  return Math.round(difference_ms/ONE_DAY)
}


var grand_prix = new Array();
//grand_prix[0] = new GPArrayItem("Jana+Victor ", "July 5 , 2004 03:00:00 GMT");
//grand_prix[1] = new GPArrayItem("Jana ", "July 6, 2004 21:00:00 GMT");
//grand_prix[3] = new GPArrayItem("Anna ", "July 17 , 2004 10:00:00 GMT");
//grand_prix[4] = new GPArrayItem("Arthur ", "July 25, 2003 10:00:00 GMT");
//grand_prix[6] = new GPArrayItem("Alex", "Septemer 27, 2003 07:00:00 GMT");

var count_arr=0;
var yr;
Today = new Date();
yr = Today.getFullYear()
var current_date = new Date()

// Store the date of the next New Year's Day
var new_years_date = new Date()
new_years_date.setYear(new_years_date.getFullYear() +1)
new_years_date.setMonth(0)
new_years_date.setDate(1)

// Call the days_between function
var days_left = days_between(current_date, new_years_date)
var add_me =0;
if ( days_left < 20 ) {
	 yr+=1;
}

//alert(days_left);


//alert("October 5, "+ yr +" 07:00:00 GMT");
grand_prix[count_arr++] = new GPArrayItem("Rufa", "January 7, "       + yr +" 07:00:00 GMT");
grand_prix[count_arr++] = new GPArrayItem("Boris G", "March 25, "       + yr +" 07:00:00 GMT");

grand_prix[count_arr++] = new GPArrayItem("Natalia", "May 15, "       + yr +" 07:00:00 GMT");
grand_prix[count_arr++] = new GPArrayItem("Zanna & Victor", "July 4, "+ yr +" 08:00:00 GMT");
grand_prix[count_arr++] = new GPArrayItem("Zanna ", "July 5, "    		+ yr +" 08:00:00 GMT");
grand_prix[count_arr++] = new GPArrayItem("Irina ", "July 21, "   		+ yr +" 08:00:00 GMT");
grand_prix[count_arr++] = new GPArrayItem("Arthur ", "July 25, "   		+ yr +" 10:00:00 GMT");
grand_prix[count_arr++] = new GPArrayItem("Alex", "Septemer 27, "  		+ yr +" 07:00:00 GMT");
grand_prix[count_arr++] = new GPArrayItem("Walter , Hannah", "October 5, "+ yr +" 07:00:00 GMT");


function getClockImage(s)
{
	var x;
	x = new Image();
	x.src = "include/" + s + ".gif";

	return x;
}

var clockImage = new Array();
for (i = 0; i <= 9; i++)
	clockImage[i] = getClockImage("img" + i);

//clockImage[10] = getClockImage("imgspc");

function drawCountdown()
{
return '\
			<span id=raceon style="display:none">\
			<table cellpadding="0" cellspacing="0" width="760">\
				<tr>\
				     <td align="left" valign="top" ><img src="./include/spacer.gif" width="386" height="3" alt="" />..</td>\
					 <td align="left" valign="top" colspan="4"><img src="./include/top_strip_blank.gif" width="374" height="3" alt="" border="0" /></td>\
				</tr>\
				<tr>\
					<td align="left" valign="top" ><img src="./include/spacer.gif" width="386" height="22" alt="" /></td>\
					<td align="left" valign="top"><img src="./include/live_timing_now_on.gif" width="319" height="22" alt="Official Live Timing Now On" /></td>\
					<td align="left" valign="top"><img src="./include/break_line.gif" width="11" height="22" alt="" /></td>\
					<td align="left" valign="top"><img src="./include/green_animated_clock.gif" width="21" height="22" alt="" /></td>\
					<td align="left" valign="top"><img src="./include/right_end_curve.gif" width="23" height="22" alt="" /></td>\
				</tr>\
			</table>\
			</span>\
			<span id=endseason style="display:none">\
			<table cellpadding="0" cellspacing="0" width="760">\
				<tr>\
				     <td align="left" valign="top" ><img src="./spacer.gif" width="386" height="3" alt="" /></td>\
					 <td align="left" valign="top" colspan="4"><img src="./include/top_strip_blank.gif" width="374" height="3" alt="" border="0" /></td>\
				</tr>\
				<tr>\
					<td align="left" valign="top" ><img src="./include/spacer.gif" width="386" height="22" alt="" /></td>\
					<td align="left" valign="top"><img src="./include/break_line.gif" width="11" height="22" alt="" /></td>\
					<td align="left" valign="top"><img src="./include/clock/red_animated_clock.gif" width="21" height="22" alt="" /></td>\
					<td align="left" valign="top"><img src="./include/right_end_curve.gif" width="23" height="22" alt="" /></td>\
				</tr>\
			</table>\
			</span>\
			<span id=countdown style="display:block">\
			<table width="760" border="0" cellpadding="0" cellspacing="0">\
				<tr>\
				     <td align="left" valign="top" colspan="6"><img src="./include/spacer.gif" width="386" height="3" alt="" /></td>\
					 <td align="left" valign="top" colspan="16"><img src="./include/top_strip.gif" width="374" height="3" alt="" border="0" /></td>\
				</tr>\
				<tr>\
					<td align="left" valign="buttom" colspan="6"><img src="./include/spacer.gif" width="200" height="22" alt="" /><font size="+2">Next Big Event </font></td>\
					<td align="left" valign="top"><img src="./include/countdown.gif" width="118" height="22" alt="Countdown" /></td>\
					<td align="left" valign="top"><img src="./include/days.gif" width="27" height="22" alt="days" /></td>\
					<td align="left" valign="top"><img src="./include/spacer.gif" width="11" height="22" alt="" name="cdd0" /></td>\
					<td align="left" valign="top"><img src="./include/spacer.gif" width="11" height="22" alt="" name="cdd1" /></td>\
					<td align="left" valign="top"><img src="./include/spacer.gif" width="11" height="22" alt="" name="cdd2"/></td>\
					<td align="left" valign="top"><img src="./include/hrs.gif" width="25" height="22" alt="hrs" /></td>\
					<td align="left" valign="top"><img src="./include/spacer.gif" width="11" height="22" alt="" name="cdh1"/></td>\
					<td align="left" valign="top"><img src="./include/spacer.gif" width="11" height="22" alt="" name="cdh2"/></td>\
					<td align="left" valign="top"><img src="./include/mins.gif" width="31" height="22" alt="mins" /></td>\
					<td align="left" valign="top"><img src="./include/spacer.gif" width="11" height="22" alt="" name="cdm1"/></td>\
					<td align="left" valign="top"><img src="./include/spacer.gif" width="11" height="22" alt="" name="cdm2"/></td>\
					<td align="left" valign="top"><img src="./include/secs.gif" width="30" height="22" alt="secs" /></td>\
					<td align="left" valign="top"><img src="./include/spacer.gif" width="11" height="22" alt="" name="cds1"/></td>\
					<td align="left" valign="top"><img src="./include/spacer.gif" width="11" height="22" alt="" name="cds2"/></td>\
					<td align="left" valign="top"><img src="./include/break_line.gif" width="11" height="22" alt="" /></td>\
					<td align="left" valign="top"><img src="./include/red_animated_clock.gif" width="21" height="22" alt="" /></td>\
					<td align="left" valign="top"><img src="./include/right_end_curve.gif" width="23" height="22" alt="" /></td>\
				</tr>\
			</table>\
			</span>'



}


function setVisibility(o, b)
{
	if (document.getElementById)
		v = document.getElementById(o).style;
	else if (document.all)
		v = document.all[o].style;
	else
		return;

	v.display = (b)?"block":"none";
}

function updateClock()
{
	getNow();

	var gp = GPCurrent();

	if (gp)
	{
		setVisibility("raceon", false);
		setVisibility("endseason", false);
		setVisibility("countdown", true);
//alert( gp.name);
//	document.images.cdgp.src = "./grands_prix_names/" + gp.name + "_grand_prix.gif";

		if (!gp.inProgress)
		{	var x, y;
			x = Math.floor(gp.countdown / 1000);
			y = x % 60;
			document.images.cds1.src = clockImage[Math.floor(y / 10)].src;
			document.images.cds2.src = clockImage[y % 10].src;

			x = Math.floor(x / 60);
			y = x % 60;
			document.images.cdm1.src = clockImage[Math.floor(y / 10)].src;
			document.images.cdm2.src = clockImage[y % 10].src;

			x = Math.floor(x / 60);
			y = x % 24;
			document.images.cdh1.src = clockImage[Math.floor(y / 10)].src;
			document.images.cdh2.src = clockImage[y % 10].src;

			x = Math.floor(x / 24);
			if ( x> 100 ) {
				y = x / 100;
		 		document.images.cdd0.src = clockImage[Math.floor(y )].src;
		 		}
			y = x % 100;

			document.images.cdd1.src = clockImage[Math.floor(y / 10)].src;
			document.images.cdd2.src = clockImage[y % 10].src;
		}
		else
		{
			setVisibility("raceon", true);
			setVisibility("endseason", false);
			setVisibility("countdown", false);
		}
	}
	else
	{
		//document.images.cdgp.src = "./spacer.gif";

		setVisibility("raceon", false);
		setVisibility("endseason", true);
		setVisibility("countdown", false);
	}
	setTimeout("updateClock()", 8756);
	return gp;
}

document.write(drawCountdown());
gr=updateClock();



