/**
* These bits of javascript are written specifically for Zoo
* and are an extension of the Prototype library and rely on Google's feed API.
* http://code.google.com/apis/ajaxfeeds/documentation/
*/
Airport = {
	Version: 			'1.0.0',
	feedURL: 			'http://weather.yahooapis.com/forecastrss',
	cityCode: 			'ASXX0023',
	weatherUnit: 		'c',
	gmtDiff: 			11,
	currTime:			"",
	currDate: 			"",
	forecast: 			"-",
	temperature: 		"-&deg;C", 
	constructURL: 		function(url, parameters)
	{
		return url+'?'+Object.toQueryString(parameters);
	},
	init: 				function()
	{
		var feed = new google.feeds.Feed( Airport.constructURL( Airport.feedURL, { p: Airport.cityCode, u: Airport.weatherUnit }) );
		feed.load(function(result) {
			Airport.processWeather(result);
		});
	},
	processWeather:		function(result)
	{
		if (!result.error) {
			var container = document.getElementById("feed");
			
			for (var i = 0; i < result.feed.entries.length; i++) {
				var entry 			= result.feed.entries[i];
				var parts 			= entry.content.split('<br>');
				var usefulStuff		= parts[2].split(',');
				
				Airport.forecast	= usefulStuff[0].strip();
				Airport.temperature	= usefulStuff[1].strip().replace(' '+Airport.weatherUnit.toUpperCase(), '°'/*+Airport.weatherUnit.toUpperCase()*/);
			}
		}
		Airport.processDate();
		Airport.updateConditions();
	},
	processDate: 		function()
	{
		var thisDate		= new Date();
		var thisDateOffset	= thisDate.getTimezoneOffset()/60;
		// convert to Canberra time
		thisDate.setHours(thisDate.getHours() + thisDateOffset + Airport.gmtDiff);
		var thisDateParts	= thisDate.toString().split(' ');
		
		Airport.currTime	= thisDate.getHours().toPaddedString(2)+":"+thisDate.getMinutes().toPaddedString(2);
		Airport.currDate	= thisDateParts[0]+" "+thisDateParts[1]+" "+thisDateParts[2]+" "+thisDateParts[3];
	},
	updateConditions: 	function()
	{
		var weatherNode		= $$('.conditions .weather span')[0];
		var timeNode		= $$('.conditions .time span')[0];
		var dateNode		= $$('.conditions .date span')[0];
		var forecastNode	= $$('.conditions .forecast span')[0];
		
		if (weatherNode){
			weatherNode.update(Airport.temperature);
		}
		if (timeNode){
			//timeNode.update(Airport.currTime);
		}
		if (dateNode){
			//dateNode.update(Airport.currDate);
		}
		if (forecastNode){
			forecastNode.update(Airport.forecast);
		}

	}
};

google.load("feeds", "1");
google.setOnLoadCallback(Airport.init);
