// Start function when DOM has completely loaded


$(document).ready(function() {	
	
	var weatherLoaded = 0;	
	
	// Set today's temperature
	//$.get("/community/WebServices/wsLoader.asmx/LoadExternalService",{serviceName:"Weather_Current"},function(xml){
	$.get("/community/xml/weather.xml",{},function(xml){	
		
		$('conditions',xml).each(function(i) {
			currentTemp = $(this).find("temp_c").text();
			currentWind = $(this).find("wind_speed_kph").text();
			currentWindDirection = $(this).find("wind_dir_compass").text();

			$("#spanCurrentWeather").html(currentTemp + "&deg;");
			$("#spanCurrentWind").html(currentWind);
			$("#imgCurrentWind").attr("src", "/assets/images/weather_wind_" + currentWindDirection + '.gif');
			$("#imgCurrentWind").attr("alt", 'Direction: ' + currentWindDirection);
			$("#imgCurrentWind").attr("title", 'Direction: ' + currentWindDirection);

			weatherLoaded ++;
			checkLoaded();			
		});

		$('forecast',xml).each(function(i) {

			/*
			 *	Unfortunately the web service date string can't be directly converted to a date object. It needs to be split
			 *	into an array to create as year, month, day. Then has to be converted back to comparable strings so that
			 *	the time element of today (dToday) is ignored.
			 *	If only the conditions were included in the current weather web service!
			 */
			/*
			//OLD METHOD
			var strCurrent = $(this).find("date").text();
			var dCurrent = new Date();
			var dToday = new Date();
			var arCurrent = new Array();
			var strToday = dToday.getFullYear() + '-' + (dToday.getMonth()+1) + '-' + dToday.getDate();

			arCurrent = strCurrent.split('-');
			dCurrent.setFullYear(arCurrent[0]);
			dCurrent.setMonth(arCurrent[1]);
			dCurrent.setDate(arCurrent[2]);

			strCurrent = dCurrent.getFullYear() + '-' + (dCurrent.getMonth()) + '-' + dCurrent.getDate();
			*/
			
			//if (strToday == strCurrent){	// Only set for today
			//if ($(this).attr('day') == '0'){	// Only set for today - relies on rollover=24 param in ws call
			if (i == '0'){	// Only set for today - first element i.
				
				visibility = $(this).find("icon").text();
				visibilityImage = $(this).find("icon").attr("filename");
				
				$("#imgCurrentWeather").attr("src", "/assets/images/weather_" + visibilityImage);
				$("#imgCurrentWeather").attr("alt", "" + visibility);
				$("#imgCurrentWeather").attr("title", "" + visibility);

				weatherLoaded ++;
				checkLoaded();
			}
		});
		
	});
	
	function checkLoaded(){		
		if (weatherLoaded == 2) {
			$('#weatherLoad').fadeOut('slow', function() { $('#weatherArea').fadeIn(); });
		}
	}

});
