
// Start function when DOM has completely loaded
$(document).ready(function(){
	strOutput='';

	// Mt Hotham
	$.get("/community/xml/weather.xml",{},function(xml){

		// Build an HTML string
		myHTMLOutput = '';
	  	panelNum = 0;

		$('validity',xml).each(function() {			
			var tdString = $(this).find('date').text();
            		var tdDate = tdString.slice(8,10) + '-' + tdString.slice(5,7) + '-' + tdString.slice(0,4);	
			$('#todaydatestring').html($(this).find('day_name').text() + ' ' + tdDate);//$(this).find('date').text());
		});
		
		
		$('conditions',xml).each(function() {
			//$('#weatherlast').html($(this).find('obs_time_local').text().replace('T',' at '));
			var wString = $(this).find('obs_time_local').text().replace('T',' at ');
		        var wLen = wString.length;
        		var wDate = wString.slice(8,10) + '-' + wString.slice(5,7) + '-' + wString.slice(0,4) + wString.slice(10,wLen);
			$('#weatherlast').html(wDate);

			$('#current_temp').html($(this).find('temp_c').text() + ' ' + $(this).find("temp_c").attr("units"));
			$('#current_dewpoint').html($(this).find('dp_c').text() + ' ' + $(this).find("dp_c").attr("units"));
			$('#current_humidity').html($(this).find('rh').text() + ' ' + $(this).find("rh").attr("units"));
			$('#current_wind').html($(this).find("wind_dir_compass").text() + ' ' + $(this).find("wind_speed_kph").text() + ' ' + $(this).find("wind_speed_kph").attr("units"));
			$('#current_windchill').html($(this).find('feels_like_c').text() + ' ' + $(this).find("feels_like_c").attr("units"));
			$('#current_rainfall9').html($(this).find('rainfall_mm').text() + ' ' + $(this).find("rainfall_mm").attr("units"));
			// sunrise, sunset - in forecast section
			
		});
		
		// Load radar and satellite images.
		$('location',xml).each(function(i) {

			var links = $(this).find("link");
			links.each( function(i) {
				if ($(this).attr("type") == 'radar animator') $('#radar').attr('src',$(this).attr("url"));
				if ($(this).attr("type") == 'satellite still') $('#satellite').attr('src',$(this).attr("url"));
			});

		});

		// Run the function for each forecast tag in the XML file
		$('forecast',xml).each(function(i) {
			dayName = $(this).find("day_name").text();
			tempMin = $(this).find("temp_min_c").text();
			tempMax = $(this).find("temp_max_c").text();
			precipitation = $(this).find("prob_precip").text();
			visibility = $(this).find("icon").text();
			visibilityImage = $(this).find("icon").attr("filename");
			wind9 = 'NA';
			wind3 = 'NA';	
			
			$('point_forecast',$(this)).each(function() {
				if ($(this).attr('time') == '09:00'){					
					wind9 = $(this).find("wind_dir_compass").text() + ' ' + $(this).find("wind_speed_kph").text() + ' ' + $(this).find("wind_speed_kph").attr("units");
				}else if ($(this).attr('time') == '15:00'){
					wind3 = $(this).find("wind_dir_compass").text() + ' ' + $(this).find("wind_speed_kph").text() + ' ' + $(this).find("wind_speed_kph").attr("units");
				}
			});
			
			if (i == '0'){			
				$('#sunrise').html($(this).find("sunrise").text());
				$('#sunset').html($(this).find("sunset").text());				
			}
				
			panelNum ++;

			// Build row HTML data and store in string
			mydata = BuildPanelHTML(panelNum, dayName,tempMin,tempMax,precipitation,visibility,visibilityImage, wind9, wind3);

			// Update the div called FeedContent with the HTML string
			$("#FeedContent .panel[id*='" + numberToName(panelNum) + "']").html(mydata);
		});

	});
	
	// Mt Hotham Airport
	$.get("/community/xml/weather_ap.xml",{},function(xml){
		$('conditions',xml).each(function(i) {			
			$('#current_ap_temp').html($(this).find('temp_c').text() + ' ' + $(this).find("temp_c").attr("units"));
			$('#current_ap_dewpoint').html($(this).find('dp_c').text() + ' ' + $(this).find("dp_c").attr("units"));
			$('#current_ap_humidity').html($(this).find('rh').text() + ' ' + $(this).find("rh").attr("units"));
			$('#current_ap_wind').html($(this).find("wind_dir_compass").text() + ' ' + $(this).find("wind_speed_kph").text() + ' ' + $(this).find("wind_speed_kph").attr("units"));
			$('#current_ap_windchill').html($(this).find('feels_like_c').text() + ' ' + $(this).find("feels_like_c").attr("units"));
			$('#current_ap_rainfall9').html($(this).find('rainfall_mm').text() + ' ' + $(this).find("rainfall_mm").attr("units"));
		});
	});
	
	// State satellite commentary	
	$.get("/community/xml/weather_vic.xml",{},function(xml){
		$('image',xml).each(function(i) {			
			$('#commentary').html($(this).find('text').text());			
		});
	});
	
});



 function BuildPanelHTML(panelNum,dayName,tempMin,tempMax,precipitation,visibility,visibilityImage, wind9, wind3){

	// Check to see if their is a "post" attribute in the name field

	if ((visibilityImage) != undefined){
		visibilityImageHTML = '<img src="/assets/images/forecast_' + visibilityImage + '" width="43" height="45" alt="' + visibility + '" title="' + visibility + '" align="left" />';;
	}
	else
	{
		visibilityImageHTML = '<img src="/assets/images/pix.gif" width="43" height="45" alt="" align="left" />';
	}


	//"/assets/images/weather_cloudy.gif"

	// Build HTML string and return
	output = '';
	output += '<strong>'+ dayName + '</strong><br />';
	output += visibilityImageHTML;
	output += '<div class="temp">'+ tempMax +'&deg;</div>';
	output += '<div class="clear"></div>';
	output += '<strong>Weather:</strong><br />'+ visibility +'<br />';
	output += '<strong>Minimum:</strong> '+ tempMin +'&deg; C<br />';
	output += '<strong>Maximum:</strong> '+ tempMax +'&deg; C<br />';
	output += '<strong>Prob. of Prec.:</strong> '+ precipitation +'%<br />';
	output += '<strong>Wind 9am:</strong> '+ wind9 +'<br />';
	output += '<strong>Wind 3pm:</strong> '+ wind3 +'<br />';
	
	return output;

}

function numberToName(panelNum){
	if(panelNum == 1) {panelNumString = 'one';}
	if(panelNum == 2) {panelNumString = 'two';}
	if(panelNum == 3) {panelNumString = 'three';}
	if(panelNum == 4) {panelNumString = 'four';}
	if(panelNum == 5) {panelNumString = 'five';}
	if(panelNum == 6) {panelNumString = 'six';}
	if(panelNum == 7) {panelNumString = 'seven';}
	return panelNumString;
}
