var currentGPOffset = 0;
var now;

function getNow()
{
	now = new Date();
//	now.setTime(now.getTime() + 96 * 60 * 60 * 1000);
}

getNow();

function GPArrayItem(n, d, l, bGP)
{
	this.name = n;
	this.start = new Date(d);
	this.end = new Date;
	this.end.setTime(this.start.getTime() + l * 60 * 60 * 1000);
	this.countdown = new Date(0);
	this.inProgress = false;
	this.isGP = bGP;
	
	return this;
}

function GPCurrent()
{
	var i, ret;
		
	for (i = currentGPOffset; i < grand_prix.length; i++)
	{
		ret = grand_prix[i];
		
		if (ret.start > now)
		{
			ret.inProgress = false;
			break;
		}
		else if (ret.end >= now)
		{
			ret.inProgress = true;
			break;
		}
	}
	currentGPOffset = i;

	for (; i < grand_prix.length; i++)
	{
		if ( grand_prix[i].isGP )
		{
			ret.countdown = new Date(grand_prix[i].start - now);
			break;
		}
	}
	return ret;
}

var grand_prix = new Array();
grand_prix[0] = new GPArrayItem("australian", "March 09, 2003 03:00:00 GMT", 2, true);
grand_prix[1] = new GPArrayItem("malaysian", "March 23, 2003 07:00:00 GMT", 2, true);
grand_prix[2] = new GPArrayItem("brazilian", "April 06, 2003 17:00:00 GMT", 2, true);
grand_prix[3] = new GPArrayItem("san_marino", "April 20, 2003 12:00:00 GMT", 2, true);
grand_prix[4] = new GPArrayItem("spanish", "May 04, 2003 12:00:00 GMT", 2, true);
grand_prix[5] = new GPArrayItem("austrian", "May 18, 2003 12:00:00 GMT", 2, true);
grand_prix[6] = new GPArrayItem("monaco", "June 01, 2003 12:00:00 GMT", 2, true);
grand_prix[7] = new GPArrayItem("canadian", "June 15, 2003 17:00:00 GMT", 2, true);
grand_prix[8] = new GPArrayItem("european", "June 29, 2003 12:00:00 GMT", 2, true);
grand_prix[9] = new GPArrayItem("french", "July 06, 2003 12:00:00 GMT", 2, true);
grand_prix[10] = new GPArrayItem("british", "July 20, 2003 12:00:00 GMT", 2, true);
grand_prix[11] = new GPArrayItem("german", "August 03, 2003 12:00:00 GMT", 2, true);

grand_prix[12] = new GPArrayItem("hungarian_Fri_Test", "August 22, 2003 06:30:00 GMT", 2, false);
grand_prix[13] = new GPArrayItem("hungarian_Fri_Prac", "August 22, 2003 09:00:00 GMT", 1, false);
grand_prix[14] = new GPArrayItem("hungarian_Fri_Qual", "August 22, 2003 12:00:00 GMT", 1, false);
grand_prix[15] = new GPArrayItem("hungarian_Sat_Prac1", "August 23, 2003 07:00:00 GMT", 0.75, false);
grand_prix[16] = new GPArrayItem("hungarian_Sat_Prac2", "August 23, 2003 08:15:00 GMT", 0.75, false);
grand_prix[17] = new GPArrayItem("hungarian_Sat_WmUp", "August 23, 2003 11:30:00 GMT", 0.25, false);
grand_prix[18] = new GPArrayItem("hungarian_Sat_Qual", "August 23, 2003 12:00:00 GMT", 1, false);
grand_prix[19] = new GPArrayItem("hungarian", "August 24, 2003 12:00:00 GMT", 2, true);

grand_prix[20] = new GPArrayItem("italian_Fri_Test", "September 12, 2003 06:30:00 GMT", 2, false);
grand_prix[21] = new GPArrayItem("italian_Fri_Prac", "September 12, 2003 09:00:00 GMT", 1, false);
grand_prix[22] = new GPArrayItem("italian_Fri_Qual", "September 12, 2003 12:00:00 GMT", 1, false);
grand_prix[23] = new GPArrayItem("italian_Sat_Prac1", "September 13, 2003 07:00:00 GMT", 0.75, false);
grand_prix[24] = new GPArrayItem("italian_Sat_Prac2", "September 13, 2003 08:15:00 GMT", 0.75, false);
grand_prix[25] = new GPArrayItem("italian_Sat_WmUp", "September 13, 2003 11:30:00 GMT", 0.25, false);
grand_prix[26] = new GPArrayItem("italian_Sat_Qual", "September 13, 2003 12:00:00 GMT", 1, false);
grand_prix[27] = new GPArrayItem("italian", "September 14, 2003 12:00:00 GMT", 2, true);

grand_prix[28] = new GPArrayItem("united_states_Fri_Test", "September 26, 2003 13:30:00 GMT", 2, false);
grand_prix[29] = new GPArrayItem("united_states_Fri_Prac", "September 26, 2003 16:00:00 GMT", 1, false);
grand_prix[30] = new GPArrayItem("united_states_Fri_Qual", "September 26, 2003 19:00:00 GMT", 1, false);
grand_prix[31] = new GPArrayItem("united_states_Sat_Prac1", "September 27, 2003 13:00:00 GMT", 0.75, false);
grand_prix[32] = new GPArrayItem("united_states_Sat_Prac2", "September 27, 2003 14:15:00 GMT", 0.75, false);
grand_prix[33] = new GPArrayItem("united_states_Sat_WmUp", "September 27, 2003 17:30:00 GMT", 0.25, false);
grand_prix[34] = new GPArrayItem("united_states_Sat_Qual", "September 27, 2003 18:00:00 GMT", 1, false);
grand_prix[35] = new GPArrayItem("united_states", "September 28, 2003 18:00:00 GMT", 2, true);

grand_prix[36] = new GPArrayItem("japanese_Fri_Test", "October 09, 2003 23:30:00 GMT", 2, false);
grand_prix[37] = new GPArrayItem("japanese_Fri_Prac", "October 10, 2003 02:00:00 GMT", 1, false);
grand_prix[38] = new GPArrayItem("japanese_Fri_Qual", "October 10, 2003 05:00:00 GMT", 1, false);
grand_prix[39] = new GPArrayItem("japanese_Sat_Prac1", "October 11, 2003 00:00:00 GMT", 0.75, false);
grand_prix[40] = new GPArrayItem("japanese_Sat_Prac2", "October 11, 2003 01:15:00 GMT", 0.75, false);
grand_prix[41] = new GPArrayItem("japanese_Sat_WmUp", "October 11, 2003 04:30:00 GMT", 0.25, false);
grand_prix[42] = new GPArrayItem("japanese_Sat_Qual", "October 11, 2003 05:00:00 GMT", 1, false);
grand_prix[43] = new GPArrayItem("japanese", "October 12, 2003 05:30:00 GMT", 2, true);

grand_prix[44] = new GPArrayItem("australian", "March 07, 2004 03:00:00 GMT", 2, true);




function getClockPath(s)
{
	return "/images/clock/img" + s + ".gif";
}

function getClockImage(s)
{
	var x;
	x = new Image();
	x.src = getClockPath(s);
	
	return x;
}
	
var clockImage = new Array();
for (i = 0; i <= 9; i++)
	clockImage[i] = getClockImage(i);


function drawCountdown()
{
var s,x,y,c;

	s = '\
			<span id=raceon style="display:none">\
			<table cellpadding="0" cellspacing="0" width="374">\
				<tr>\
				     <td align="left" valign="top" colspan="4"><img src="http://www.formula1.com/images/bottom_nav/top_strip_blank.gif" width="374" height="3" alt="" border="0" /></td>\
				</tr>\
				<tr>\
					<td align="left" valign="top"><a href="/livetiming/"><img src="http://www.formula1.com/images/bottom_nav/live_timing_now_on.gif" width="319" height="22" alt="Official Live Timing Now On" border=0 /></a></td>\
					<td align="left" valign="top"><img src="http://www.formula1.com/images/bottom_nav/break_line.gif" width="11" height="22" alt="" /></td>\
					<td align="left" valign="top"><img src="http://www.formula1.com/images/clock/green_animated_clock.gif" width="21" height="22" alt="" /></td>\
					<td align="left" valign="top"><img src="http://www.formula1.com/images/bottom_nav/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="374">\
				<tr>\
				     <td align="left" valign="top" colspan="4"><img src="http://www.formula1.com/images/bottom_nav/top_strip_blank.gif" width="374" height="3" alt="" border="0" /></td>\
				</tr>\
				<tr>\
					<td align="left" valign="top"><img src="http://www.formula1.com/images/bottom_nav/season_over.gif" width="319" height="22" alt="Season Over" /></td>\
					<td align="left" valign="top"><img src="http://www.formula1.com/images/bottom_nav/break_line.gif" width="11" height="22" alt="" /></td>\
					<td align="left" valign="top"><img src="http://www.formula1.com/images/countdown_clock/red_animated_clock.gif" width="21" height="22" alt="" /></td>\
					<td align="left" valign="top"><img src="http://www.formula1.com/images/bottom_nav/right_end_curve.gif" width="23" height="22" alt="" /></td>\
				</tr>\
			</table>\
			</span>\
			<span id=countdown style="display:block">\
			<table width="374" border="0" cellpadding="0" cellspacing="0">\
				<tr>\
				     <td align="left" valign="top" colspan="17"><img src="http://www.formula1.com/images/bottom_nav/top_strip.gif" width="385" height="3" alt="" border="0" /></td>\
				</tr>\
				<tr>\
					<td align="left" valign="top"><img src="http://www.formula1.com/images/bottom_nav/countdown.gif" width="118" height="22" alt="Countdown" /></td>'

	for ( x = 0; x < 4; x++ )
	{
		s += '<td align="left" valign="top">';
		switch(x)
		{
			case 0:
				s += '<img src="http://www.formula1.com/images/bottom_nav/days.gif" width="27" height="22" alt="days" />'
				c = 'd';
				break;
			case 1:
				s += '<img src="http://www.formula1.com/images/bottom_nav/hrs.gif" width="25" height="22" alt="hrs" />'
				c = 'h';
				break;
			case 2:
				s += '<img src="http://www.formula1.com/images/bottom_nav/mins.gif" width="31" height="22" alt="mins" />'
				c = 'm';
				break;
			case 3:
				s += '<img src="http://www.formula1.com/images/bottom_nav/secs.gif" width="30" height="22" alt="secs" />'
				c = 's';
				break;
		}
					
		for ( d = (x==0)?3:2 ; d > 0 ; d--)
		{
			s += '</td><td align="left" valign="top" width=11>';
			for ( y = 0; y < 10; y++ )
				s += '<img style="display:' + (y==0?"inline":"none") + '" src="http://www.formula1.com/images/clock/img' + y + '.gif" width="11" height="22" alt="" id="cd' + c + d + '-' + y + '"/>';
		}

		s += '</td>';
	}
	
	s += 				'<td align="left" valign="top"><img src="http://www.formula1.com/images/bottom_nav/break_line.gif" width="11" height="22" alt="" /></td>\
					<td align="left" valign="top"><img src="http://www.formula1.com/images/clock/red_animated_clock.gif" width="21" height="22" alt="" /></td>\
					<td align="left" valign="top"><img src="http://www.formula1.com/images/bottom_nav/right_end_curve.gif" width="23" height="22" alt="" /></td>\
				</tr>\
			</table>\
			</span>'

	return s;


}

function setVisibility(o, b)
{
	if (document.getElementById)
		v = document.getElementById(o).style;
	else if (document.all)
		v = document.all[o].style;
	else
		return;
		
	var s = (b)?"block":"none";
		
	if (v.display != s)
		v.display = s;
}

function setNumber( s, m )
{
	var v,n,o,ss;
	for ( n = 0; n < 10; n++ )
	{
		o = s + n;
		if (document.getElementById)
			v = document.getElementById(o).style;
		else if (document.all)
			v = document.all[o].style;
		else
		continue;

		
		ss = (n==m)?"inline":"none";
		
		if (v.display != ss)
			v.display = ss;
	}
}

function updateClock()
{
	getNow();
	
	var gp = GPCurrent();
	
	if (gp)
	{
		if (!gp.inProgress)
		{	var x, y, n;
			setVisibility("raceon", false);
			setVisibility("endseason", false);
			setVisibility("countdown", true);
			
			x = Math.floor(gp.countdown / 1000);
			y = x % 60;
			setNumber( "cds2-", Math.floor(y / 10) );
			setNumber( "cds1-", y % 10 );

			x = Math.floor(x / 60);
			y = x % 60;
			setNumber( "cdm2-", Math.floor(y / 10) );
			setNumber( "cdm1-", y % 10 );

			x = Math.floor(x / 60);
			y = x % 24;
			setNumber( "cdh2-", Math.floor(y / 10) );
			setNumber( "cdh1-", y % 10 );

			x = Math.floor(x / 24);
			y = x % 365
			setNumber( "cdd3-", Math.floor(y / 100) );
			setNumber( "cdd2-", Math.floor(y / 10) % 10 );
			setNumber( "cdd1-", y % 10 );
		}
		else
		{
			setVisibility("raceon", true);
			setVisibility("endseason", false);
			setVisibility("countdown", false);
		}
	}
	else
	{	
		//document.images.cdgp.src = "http://www.formula1.com/images/spacer.gif";
		
		setVisibility("raceon", false);
		setVisibility("endseason", true);
		setVisibility("countdown", false);
	}

	setTimeout("updateClock()", 1000);
}

function setStyle(gp)
{

    if (document.getElementById)
            document.getElementById(gp).className = 'last';
    else if (document.all)
            document.all[gp].className="last";
    else
        return;
									
}

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