function RefreshWidget( delayInMins )
{
	// Refresh the weather widget after delayInMins minutes 
	// (delayInMins min * 60 s/min * 1000 ms/s)
	setTimeout( "ShowWeatherWidget();", delayInMins * 60000 );
}

function ShowWeatherWidget()
{
	var refreshInterval = 5;		// minutes
	var timeOutThreshold = 10000;	// 10 sec * 1000 ms/sec
	var fadeOutSpeed = 10000;		// 10 sec * 1000 ms/sec
	
	jQuery.ajax({
		type: "GET",
		dataType: "html",
		url: "/NOAAWeather/FindlayAirport/WeatherWidget.asp",
		data: "",
		timeout: timeOutThreshold,
		success: function( data, textStatus ) {
			$( "#WeatherWidget" ).show().html( data );
			
			// Refresh the weather widget after refreshInterval minutes
			RefreshWidget( refreshInterval );
		},
		error: function( reqObj, textStatus, errorThrown ) {
		    if( textStatus == "timeout" )
		    {
		        $( "#WeatherWidget" )
					.show()
					.html( "<div class='error'>Request for weather data timed out.<br /><br />Will retry later.</div>" )
					.fadeOut( fadeOutSpeed );

		        RefreshWidget( refreshInterval );
		    }
		    else
		    {
				$( "#WeatherWidget" )
					.show()
					.html( "<div class='error'>Request for weather data failed.</div>" )
					.fadeOut( fadeOutSpeed );
		    }
		    return;
		}
	 });		
}

jQuery(document).ready( function() {
	// Display a progress bar when AJAX requests are sent or complete
	$("#Loading").bind("ajaxSend", function(){
		// Show the progress bar
		$(this).show();
		
		// Erase existing HTML in #WeatherWidget
		$( "#WeatherWidget" ).html( "" );
	}).bind("ajaxComplete", function(){
		$(this).hide();
	});	

	// Display NOAA Weather Widget 500 ms after page finishes loading
	setTimeout( "ShowWeatherWidget();", 500 );
});
