$(document).ready(function(){
	
	timestamp = 0;
	
	if (timestamp == 0) {
		timestamp = Math.round((new Date()).getTime() / 1000);
	} // end if
	
	counter = 0;
	delay = 6000;
	text = 0;
	
	updateMsg();

});

function addMessages(xml) {

	if($("status",xml).text() == "2") {	
		timestamp = $("time",xml).text();	
		//$("#alertContainer").append("Time: " + timestamp + " ").fadeIn('slow');
		return;
	} // end if

	timestamp = $("time",xml).text();
	var str = $("msg",xml).text();
	str = str.replace("##", "&");

	text = '<div id=\"alert\">' + str + '</div>';

	$("message",xml).each(function() {
		$("#alertContainer").append(text).fadeIn('slow');
		$('#alert').delay(6000).fadeOut(400, function () {
			$(this).remove();
		});
	});

} // end addMessages


function updateMsg() {

		$.get("./ajax/getAlerts.php",{ time: timestamp }, function(xml) {
			addMessages(xml);
		});
		
		counter += 1;
		
		if (counter == 10) {

			delay = delay * 2;

			if (delay >= 60000) {
				delay = 60000;
			} // end if

			counter = 0;

		} // end if
		
		setTimeout('updateMsg()', delay);

} // end updateMsg

