// This JavaScript file is AJAX to dynamically pull data from stats.php, passing it a GET variable containing an XML url taken from the Config XML.

// The following function is purely an Internet Explorer workaround, as IE does not support by default multiple parameters to the setTimeout function.

var _st = window.setTimeout;

window.setTimeout = function(fRef, mDelay) { 
		if (typeof fRef == "function") {
			var argu = Array.prototype.slice.call(arguments,2); 
			var f = function() { fRef.apply(null, argu); }; 
			return _st(f, mDelay); 
		}
		return _st(fRef,mDelay);
};

// This function is used to get data from stats.php and set it using DOM to div's on the page calling it (which will always be index.php).
// The parameters passed are the weather station XML url and an integer relating to the div's which should be set on the page.

function ajax(xml_url, div_id, refresh_rate) {

	// This line sets the background of the station currently called to a simple AJAX loader animated gif.

	document.getElementById('station' + div_id).style.background = 'url(images/mini_loader.gif) top right no-repeat';
	
	// The following is a standard AJAX try & catch.
	
	var http;

	try	{
		// Firefox, Opera 8.0+, Safari
		http = new XMLHttpRequest();
	}

	catch (e) {
		// Internet Explorer
		try	{ http = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) {
			try { http = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (e) { document.getElementById('station0').innerHTML = 'Your browser does not support AJAX!'; }
		}
	}
	
	// The file called is stats.php with the GET variable of the weather stations XML file url.
	// An additional GET variable is passed which is a random number, this is to stop the browser Caching results and to keep the statistics up-to-date.

	http.open('GET', 'stats.php?xml=' + xml_url + '&num=' + Math.random());
	http.onreadystatechange = function() {

	  if(http.readyState == 4) {
	  
		// stats.php returns a basic bit of text/xhtml. The span contains the last time the weather stations XML was updated.
		// This is to be inserted on a different area of the page, so the content is split into an array.

		var stats = http.responseText.split('<span>');
		
		// This if statement checks if the information that has just been recieved is any newer than what is on the page.
		// This is to stop unecessary writing to the page, the page is only updated if new weather stats are found.
		// The script determines if the content is name by checking the time on the page against the time in the new data.
		
		if('Stats: ' + stats[1] != document.getElementById('timestamp' + div_id).innerHTML) {
			
			// This line writes the new weather statistics.
			
			document.getElementById('station' + div_id).innerHTML = stats[0];
			
			// This line writes the new update time to the page.
			
			document.getElementById('timestamp' + div_id).innerHTML = 'Stats: ' + stats[1];
		}
		
		// Now that everything has been ran, take away the AJAX loader animated GIF by setting the background to white.
		
		document.getElementById('station' + div_id).style.background = '#FFF';
		
	  }
	};

	http.send(null);
	
	// Now that the whole function has been ran, run it again after the refresh period set in the Config XML.
	// (Note that the XML file contains refresh rate in seconds, hence *1000 to convert it to milliseconds which JavaScript uses.
	
	setTimeout(ajax, refresh_rate*1000, xml_url, div_id, refresh_rate);
}