RBR = {
	Countdown : {
		//get output element, race time, set up new output areas and interval call
		createCountdown : function (){
			RBR.Countdown.countdownOut = jQuery('.PE_countdownOut'); //gets element wrapping output in HTML as object
			if(RBR.Countdown.countdownOut.length < 1){return;}
			if(RBR.seasonEndDate < new Date()){
				RBR.Countdown.countdownOut.text(RBR.seasonEndMessage);
			}
			else{
				//extract ISO 8601 date/time from page
				var nextRace = jQuery('.PE_nextRaceEvent').text();
				RBR.Countdown.raceDate = RBR.Countdown.timeParseISO8601(nextRace); //sets commonly used variable
				//create up dom objects for updating later
				RBR.Countdown.daysOut = jQuery('<span></span>');
				RBR.Countdown.hoursOut = jQuery('<span></span>');
				RBR.Countdown.minutesOut = jQuery('<span></span>');
				RBR.Countdown.secondsOut = jQuery('<span></span>');
				//remove current contents, put in countdown elements
				RBR.Countdown.countdownOut.empty();
				RBR.Countdown.countdownOut.append(RBR.Countdown.daysOut);
				RBR.Countdown.countdownOut.append(document.createTextNode(":"));
				RBR.Countdown.countdownOut.append(RBR.Countdown.hoursOut);
				RBR.Countdown.countdownOut.append(document.createTextNode(":"));
				RBR.Countdown.countdownOut.append(RBR.Countdown.minutesOut);
				RBR.Countdown.countdownOut.append(document.createTextNode(":"));
				RBR.Countdown.countdownOut.append(RBR.Countdown.secondsOut);
				//put in data and set up interval updating
				RBR.Countdown.update = setInterval("RBR.Countdown.updateCountdown()",1000);
				RBR.Countdown.updateCountdown();
			}
		},
		//called every second to update the countdown output
		updateCountdown : function (){
			//if we've reached the end of the season (finish of last race) then show message and stop interval
			if(RBR.seasonEndDate < new Date()){
				RBR.Countdown.countdownOut.text(RBR.seasonEndMessage);
				clearInterval(RBR.Countdown.update);
				return;
			}
			var TimeToGo = RBR.Countdown.getCountdownDiff();
			RBR.Countdown.daysOut.text(TimeToGo.days);
			RBR.Countdown.hoursOut.text(TimeToGo.hours);
			RBR.Countdown.minutesOut.text(TimeToGo.minutes);
			RBR.Countdown.secondsOut.text(TimeToGo.seconds);
			//if race is no longer in the future, the count will be 0 until we get a new race time, so stop the interval
			if(TimeToGo.raceStatus !== "future"){
				clearInterval(RBR.Countdown.update);
				return;
			}
		},
		//if the race date in the page is updated dynamically, call this function
		updateRaceDate : function (){
			RBR.Countdown.raceDate = RBR.Countdown.timeParseISO8601(jQuery('.PE_nextRaceEvent').text());
			clearInterval(RBR.Countdown.update);
			RBR.Countdown.update = setInterval("RBR.Countdown.updateCountdown()",1000);
		},
		//convert ISO8601 date to Javascript date object
		timeParseISO8601 : function (sISO) {
			/*
		    ISO 8601:
		    1999-07-03T00:00+0100, - and : are optional
		    JavaScript's Date.parse() format:
		    1999/07/03 00:00:00 UTC+0100
			*/
			var dateRegEx = /(\d{4})\-?(\d{2})\-?(\d{2})T(\d{2}):?(\d{2})/;
			dateRegEx.lastIndex = 0;
			var sDate = dateRegEx.exec(sISO);
			var sYear = sDate[1];
			var sMonth = sDate[2];
			var sDay = sDate[3];
			var sHour = sDate[4];
			var sMinute = sDate[5];
			
			var sZone = "+0000";//default to UTC
			if (sISO.substr(sISO.indexOf("+")) > 0){
				sZone = sISO.substr(sISO.indexOf("+"));
			}
			var sFormat = sYear + "/" + sMonth + "/" + sDay + " " + sHour + ":" + sMinute + ":00 UTC" + sZone;
			return new Date(sFormat);
		},
		//add on '0' to start of numbers under 10
		zeroPadInteger : function (sInt){
			sInt = sInt.toString();
			if(sInt.length < 2){
				sInt = "0" + sInt;
			}
			return sInt;
		},
		//get the time difference between the next race date and now
		//returns an object containing the difference in days, hours, minutes and seconds
		// if given date is less than a second away from now or in the past, returns 0 for all
		//this is called every second, avoid putting DOM calls in here
		getCountdownDiff : function (){
			var dNow = new Date();
			var countMS = RBR.Countdown.raceDate - dNow;
			var days, hours, minutes, seconds, raceStatus;
			//if is less than 1 seconds before or is later than the race, return 00 for all
			if(countMS < 1000){
				days = hours = minutes = seconds = "00";
				raceStatus = "past";
			}
			else{
				var count = new Date(RBR.Countdown.raceDate - dNow);
				seconds = RBR.Countdown.zeroPadInteger(count.getUTCSeconds());
				minutes = RBR.Countdown.zeroPadInteger(count.getUTCMinutes());
				hours = RBR.Countdown.zeroPadInteger(count.getUTCHours());
				days = RBR.Countdown.zeroPadInteger(Math.floor(countMS / (1000*60*60*24)));
				raceStatus = "future";
			}
			//create object with countdown info
			return {
				'days' : days,
				'hours' : hours,
				'minutes' : minutes,
				'seconds' : seconds,
				'raceStatus' : raceStatus
			};
		}
	}
};

jQuery(document).ready(RBR.Countdown.createCountdown);
