/**
 * @author Paul
 */
var RaceInfo = new Class({
	_arrRaceInfo: 			null,
	_fnDateCallback: 		null,
	_fnNextDateCallback: 	null,
	_fnForecastCallback:	null,
	_arrTracks: 			null,
	
	CROFT: 				{strTitle:			"Croft Circuit",
						 strImgPath: 		"/z_images/content/circuits/bg_croft.png",
						 strForecastRSS: 	"http://feeds.bbc.co.uk/weather/feeds/rss/5day/id/1691.xml",
						 strCircuitLink:	"http://www.croftcircuit.co.uk/"},
						 
	KNOCKHILL: 			{strTitle:			"Knockhill Circuit",
						 strImgPath: 		"/z_images/content/circuits/bg_knockhill.png",
						 strForecastRSS: 	"http://feeds.bbc.co.uk/weather/feeds/rss/5day/id/2372.xml",
						 strCircuitLink:	"http://www.knockhill.com/"},
						 
	MALLORY_PARK: 		{strTitle:			"Mallory Park Circuit",
						 strImgPath: 		"/z_images/content/circuits/bg_mallorypark.png",
						 strForecastRSS: 	"http://feeds.bbc.co.uk/weather/feeds/rss/5day/id/2491.xml",
						 strCircuitLink:	"http://www.mallorypark.co.uk/"},
						 
	ANGLESEY: 			{strTitle:			"Anglesey Circuit",
						 strImgPath: 		"/z_images/content/circuits/bg_anglesey.png",
						 strForecastRSS: 	"http://feeds.bbc.co.uk/weather/feeds/rss/5day/id/0385.xml",
						 strCircuitLink:	"http://www.angleseycircuit.com/"},
	
	CADWELL_PARK:		{strTitle:			"Cadwell Park",
						 strImgPath: 		"/z_images/content/circuits/bg_cadwellpark.png",
						 strForecastRSS: 	"http://feeds.bbc.co.uk/weather/feeds/rss/5day/id/2564.xml",
						 strCircuitLink:	"http://www.motorsportvision.co.uk/cadwell-park/circuits/cadwell-park.asp"},
						 
	OULTON_PARK:		{strTitle:			"Oulton Park",
						 strImgPath: 		"/z_images/content/circuits/bg_oultonpark.png",
						 strForecastRSS: 	"http://news.bbc.co.uk/weather/forecast/4270/Next3DaysRSS.rss?area=CW6",
						 strCircuitLink:	"http://www.motorsportvision.co.uk/oulton-park/circuits/oulton-park.asp"},
					 
	initialize: function()
	{
		this._arrRaceInfo 	= new Array();
		this._arrTracks		= new Array(this.CROFT,
										this.KNOCKHILL,
										this.MALLORY_PARK,
										this.ANGLESEY,
										this.CADWELL_PARK,
										this.OULTON_PARK);
	},
	
	getTrack: function(iTrackID)
	{
		return this._arrTracks[iTrackID];
	},
	
	getDates: function(iMonth, iYear, strTimestamp, bIgnoreNext, fnCallback)
	{
		// assign the callback for whenever
		// we're ready to pass back the response
		this._fnDateCallback = fnCallback;
		
		if(this._arrRaceInfo[iMonth+"_"+iYear])
		{
			// call the callback with the cached result
			fnCallback(this._arrRaceInfo[iMonth+"_"+iYear], strTimestamp);
		}
		else
		{
			// dispatch an AJAX request for the info
			var objRequest = {iMonth: 		iMonth,
							  iYear: 		iYear,
							  bIgnoreNext: 	bIgnoreNext,
							  strTimestamp: strTimestamp};
							  
			(new Ajax("../z_ajax/ajax_handler_raceinfo.php", {onComplete: this.ajax_handleDates.bind(this), postBody: objRequest})).request();
		}
	},
	
	ajax_handleDates: function(objResponseText, objResponseXML)
	{
		var strArrKey 	= null;
		var iTimestamp 	= null;
		var arrDates 	= new Array();
		
		if(objResponseXML)
		{
			// go through each child of the response.
			// IE regards the xml header as a child node
			// so we will go through each one just in case
			
			for(var i = 0; i < objResponseXML.childNodes.length; i++)
			{
				// if we have a response
				if(objResponseXML.childNodes[i].nodeName == "response")
				{
					var arrChildren = objResponseXML.childNodes[i].childNodes;
					
					for(var c = 0; c < arrChildren.length; c++)
					{
						switch(arrChildren[c].nodeName)
						{
							case "key": 		strArrKey = arrChildren[c].firstChild.nodeValue;
												break;
							case "date": 		arrDates.push(Json.evaluate(arrChildren[c].firstChild.nodeValue));
												break;
							case "timestamp": 	iTimestamp = arrChildren[c].firstChild.nodeValue;
												break;
						}
					}
				}
			}
			
			// store for later
			if(strArrKey)
				this._arrRaceInfo[strArrKey] = arrDates;
			
			// now call the callback function
			this._fnDateCallback(arrDates, iTimestamp);
		}
	},
	
	getForecast: function(iTrackID, fnCallback)
	{
		// create an empty request - no params required
		var objRequest = {iForecastID:	iTrackID};
		
		// set the callback function
		this._fnForecastCallback = fnCallback;
		
		// dispatch the request
		(new Ajax("../z_ajax/ajax_handler_forecast.php", {onComplete: this.ajax_handleForecast.bind(this), postBody: objRequest})).request();
	},
	
	ajax_handleForecast: function(objResponseText, objResponseXML)
	{
		var arrForecast = new Array();
		
		if(objResponseXML)
		{
			// go through the response XML. We have to do it like
			// this because IE regards the XML header as a node
			// whereas other browsers don't.
			for(var x = 0; x < objResponseXML.childNodes.length; x++)
			{
				if(objResponseXML.childNodes[x].nodeName == "parsererror")
				{
					alert("ERROR: " + objResponseXML.childNodes[x].firstChild.nodeValue.toString());
				}
				
				// if we find a response node
				if(objResponseXML.childNodes[x].nodeName == "rss")
				{
					if($('flickrcontainer'))
						$('flickrcontainer').empty();
					
					if( objResponseXML.childNodes[x].childNodes && 
						objResponseXML.childNodes[x].childNodes.length)
					{
						var arrChannel = objResponseXML.childNodes[x].childNodes;
						for(var c = 0; c < arrChannel.length; c++)
						{
							if(arrChannel[c].nodeName == "channel")
							{
								var arrItem  = arrChannel[c].childNodes;
								for(var i = 0; i < arrItem.length; i++)
								{
									if(arrItem[i].nodeName == "item")
									{
										var arrItemInfo	= arrItem[i].childNodes;
										var strTitle	= "";
										var strLink		= "";
										var objWeather 	= new Object();
										
										for(var g = 0; g < arrItemInfo.length; g++)
										{
											if(arrItemInfo[g].nodeName == "title")
												strTitle 	= arrItemInfo[g].firstChild.nodeValue;
										}
										
										// split the weather on ,
										var arrWeather 				= strTitle.split(",");
										
										// Part 0: Day + Conditions
										var rgpDayCon				= /([A-Za-z]*):\s(.*)/;
										var arrDayCon 				= rgpDayCon.exec(arrWeather[0]);
										
										// 0 = full match, start at 1
										objWeather.strDay			= arrDayCon[1].substring(0, 3);
										objWeather.strCondition		= arrDayCon[2].toString();
										
										objWeather.strCondition		= objWeather.strCondition.replace(/\s/, "").toLowerCase();
										
										// Part 1: Max Temp
										var rgpMaxTemp				= /Max\sTemp:\s([^\s]*)/;
										var arrMaxTemp 				= rgpMaxTemp.exec(arrWeather[1]);
										objWeather.strMaxTemp		= arrMaxTemp[1];
										
										// Part 2: Min Temp
										var rgpMinTemp				= /Min\sTemp:\s([^\s]*)/;
										var arrMinTemp 				= rgpMinTemp.exec(arrWeather[2]);
										objWeather.strMinTemp		= arrMinTemp[1];
										
										arrForecast.push(objWeather);
									}
								}
							}
						}
					}
				}
			}
		}
		
		// now call the callback function with the
		// weather array
		this._fnForecastCallback(arrForecast);
	}
})
