	$(document).ready(function(){
		timestamp = 0;
		counter = 0;
		delay = 4000;
		string = 0;
		text = 0;
		updateMsg();
	});

	function addMessages(xml) {

		if($("status",xml).text() == "2") {
			return;
		} // end if

		timestamp = $("time",xml).text();

		if ($("track",xml).text() == 0) {
			string = "street race";
		} // end if
		else {
			string = "race at the track";
		} // end if

		text = "<table cellspacing=0 cellpadding=4 border=0 width=100%>";
		text += "<tr><td bgcolor=#003300>";

		text += "<b><a href=\"userbio.php?id=" + $("accountID",xml).text() + "\">" + $("name1",xml).text() + "</a></b> would like to challenge you to a " + string + " for <font color=#FF0000><b>" + $("bet_credits",xml).text() + "</b> credits and <b>$" + $("bet_cash",xml).text() + "</b></font>";

		if ($("pink_slip",xml).text() == 1) {
			text += " and <font color=#FF0000><b>pink slips</b></font>";
		} // end if

		text +=  ". &nbsp;&nbsp;[<a href=\"proc_accept_race.php?id=" + $("raceID",xml).text() + "&accept=1\">Accept</a>]&nbsp;[<a href=\"proc_accept_race.php?id=" + $("raceID",xml).text() + "&accept=0\">Decline</a>]";
	
		text += "</td></tr>";
		text += "</table>";

		$("message",xml).each(function(id) {
			message = $("message",xml).get(id);
			$("#messagewindow").append(text).fadeIn('slow');
		});


	} // end addMessages


	function updateMsg() {
		
		if (timestamp == 0) {

			$.get("./xml/xmlRaces.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 if

	} // end updateMsg

