// Variable to store a reference to the timer
var jabberStatusTimer;
var jabberStatusImageSource;
var jabberStatus;

// Function to initialise the timer
function initialiseJabberStatusTimer() 
{
	// Set a timer to periodically check the Jabber status image
	jabberStatusTimer = setTimeout('checkJabberStatus()', 6000);

	// If there is no source URL recorded for the status image, get it
	if (jabberStatusImageSource == null && document.getElementById('jabber-status-icon') != undefined)
	{
		jabberStatusImageSource = document.getElementById('jabber-status-icon').src;
	}
}

// Function to request the update
function checkJabberStatus() 
{
	// Get the new status text
	jabberStatus = getHTTP('http://r1g.org/resources/jabber-status?update=status&' + (new Date()).getTime(), updateJabberStatus);

	// Re-initialise the timer
	initialiseJabberStatusTimer();
}

// Function to update the status
function updateJabberStatus() 
{
	// Reload the image (add timestamp to source) and change the alt and title text
	if(jabberStatus != undefined && jabberStatus.readyState == 4 && jabberStatus.status == 200)
	{
		if(document.getElementById('jabber-status-icon') != undefined)
		{
			document.getElementById('jabber-status-icon').src = jabberStatusImageSource + '?' + (new Date()).getTime();
			document.getElementById('jabber-status-icon').alt = jabberStatus.responseText;
			document.getElementById('jabber-status-icon').title = jabberStatus.responseText;
		}
		
		if(document.getElementById('jabber-status-text') != undefined)
		{
			document.getElementById('jabber-status-text').innerHTML = jabberStatus.responseText;
		}
	}
}
