function showToolTip(evt){
	if(evt.target){
		oElm=evt.target;
	}else if(evt.srcElement){
		oElm=evt.srcElement;
	}
	if(oElm.nodeName=="A"){
		oDiv=document.createElement('div');
		oDiv.className="race-tool-tip";
		oElm.parentNode.appendChild(oDiv);
		oTitle=oElm.title;
		oDiv.innerHTML=oTitle;
		oElm.title="";
		oElm.onmouseout=function(){
			oElm.parentNode.removeChild(oDiv);
			oElm.title=oTitle;
		}
	}
}



// Set up variables (populated on page load)
var currentTime;
var countdown;
var countdownRace;
var countdownDays;
var countdownHours;
var countdownMins;
var countdownSecs;
var raceCount = 0;
var higlightFlag;

// Create new race object
function F1Race(fn, t) {
	this.num = raceCount + 1;
	raceCount = raceCount + 1;
	this.fullname = fn;
	this.start = new Date(t);
	this.end = new Date;
	this.end.setTime(this.start.getTime() + 120 * 60 * 1000);
	return this;
}

// Set the current time
function getCurrentTime() {
	currentTime = new Date();
}

// Return the current race
function getCurrentRace() {
	return new findCurrentRace();
}

// Find the current race for the countdown
function findCurrentRace() {
	seasonFinished = false;
	var currentRace;

	for (var i=0; i < F1RaceArray.length; i++) {
		currentRace = F1RaceArray[i]
		if (currentRace.end >= currentTime){
			highlightFlag=i;
			break;
		}
	}

	if (currentRace.start > currentTime) {
	  currentRace.inProgress = false;
	}
	else if (currentRace.end >= currentTime) {
		currentRace.inProgress = true;
	}
	else {
	  seasonFinished = true;
	}
	currentRace.countdown = new Date(currentRace.start - currentTime);
	return currentRace;
}

// Runs every second to refresh the countdown timer
function refreshTimer() {
  getCurrentTime();
	var currentRace = getCurrentRace();

	if (currentRace)	{
  	if (seasonFinished) {  // Season has finished
    	countdown.innerHTML = "Season finished";
    	setTimeout("refreshTimer()", 30000);
  	}
  	else if (!currentRace.inProgress) {  // Race coming up
    	countdownRace.innerHTML = currentRace.fullname ;

    	secondsLeft = Math.floor((currentRace.countdown) / 1000);
    	daysLeft = Math.floor(secondsLeft / 60 / 60 / 24);
    	secondsLeft -= daysLeft * 60 * 60 * 24;
    	hoursLeft = Math.floor(secondsLeft / 60 / 60);
    	secondsLeft -= hoursLeft * 60 * 60;
      minutesLeft = Math.floor(secondsLeft / 60);
      secondsLeft -= minutesLeft * 60;

      countdownDays.innerHTML = formatTimer(daysLeft);
      countdownHours.innerHTML = formatTimer(hoursLeft);
      countdownMins.innerHTML = formatTimer(minutesLeft);
      countdownSecs.innerHTML = formatTimer(secondsLeft);
      highlightList(highlightFlag);
      setTimeout("refreshTimer()", 1000);
	  }
	  else {  // Race in progress
		// countdownDiv.innerHTML = currentRace.name + " race in progress";
		// var trackImg = document.getElementById('track_' + currentRace.shortname);
		//trackImage.className = "currentTrack";
		//  setTimeout("refreshTimer()", 30000);
	  }
  }
}

// Format time numbers with leading zero
function formatTimer(number) {
  number = number < 10 ? "0" + number : number;
  return number;
}

function highlightList(num){
	oAllLi=oFlags.getElementsByTagName('li');
	for(i=0;i<oAllLi.length;i++){
		oAllLi[i].className="upcoming-race";
	}
	oAllLi[num].className="current-race";
}

function populateFlagTitles(){
	oAllLi=oFlags.getElementsByTagName('li');
	for(i=0;i<oAllLi.length;i++){
			oTit=F1RaceArray[i].fullname;
			oTit=oTit.split('|');
			oTitle="<strong>"+oTit[0]+"</strong><br/>"+oTit[1];
			oAllLi[i].getElementsByTagName('a')[0].title=oTitle;
	}
}
