

var updateDelay      = 5 * 1000;  //this is the standard update frequency for data in this scoreboard (1000 = 1 second)
var finalUpdateDelay = 300 * 1000;//this is the update frequency for the data in the scoreboard if all leagues are final (1000 = 1 second)
var gamesFinal       = false;     //this flag indicates if all leagues are final or not
var updateThread     = null;
var displayedLeague  = null;
function updateScores() {
	var now = new Date();
	var nowSecs = now.getTime(); 
	
	var ds = this;
	jQuery.ajax({
		type: "GET",
		url: "global_scores.body.asp?random=" + nowSecs + (gamesFinal == true ? "&final=true" : ""),
		success: function(xmlBody) {
			var head = new Array(), body = new Array(), league = new Array();
			
			//See if any league in this new set of data is not marked final
			gamesFinal = (jQuery(".score_row[final='false'], .last_row[final='false']", xmlBody).length == 0);
			
			//get the list of leagues that have data in this new update
			jQuery('.container1', xmlBody).each( function() {
				head[head.length] = this;
				league[league.length] = this.id.substring(3);
			});
			
			//get the actual league data (excluding the header) for each league in the update
			jQuery('.switchcontent', xmlBody).each( function() {
				body[body.length] = this;
			});
			
			//get the list of leagues that are currently displayed on the page (if we don't have it from a previous update)
			if (displayedLeague == null) {
				displayedLeague = new Array();
				jQuery('#league-list .container1').each( function() {
					displayedLeague[displayedLeague.length] = this.id.substring(3);
//					var xmlItem = this;
//					var blah = jQuery('.showstate', xmlItem).children();
//					jQuery('#box'+this.id+' .showstate', xmlBody).empty().append(blah.clone());
				});
			}
			
			//loop through all the leagues in the new update and paste their content into the next slot on the page
			for (var index = 0; index < league.length; index++) {
				if (index >= displayedLeague.length) {
					//the previous list of leagues wasn't this long, so add this to the end of the list
					jQuery('#league-list').append(head[index]);
					jQuery('#box'+league[index]+' .showstate').html('<img src="images/minus-icon.jpg" align="right" vspace="5" hspace="5" />');
					jQuery('#league-list').append(body[index]);
				}
				else if (displayedLeague[index] != league[index]) {
					//the previous league in this slot was different, so remove ALL the leagues from here onward so we can add them back expanded
					jQuery('#league-list #box'+displayedLeague[index]).nextAll().remove();
					jQuery('#league-list #box'+displayedLeague[index]).remove();
					//remove the displayedLeague elements too
					displayedLeague = displayedLeague.slice(0, index);
					
					jQuery('#league-list').append(head[index]);
					jQuery('#box'+league[index]+' .showstate').html('<img src="images/minus-icon.jpg" align="right" vspace="5" hspace="5" />');
					jQuery('#league-list').append(body[index]);
				}
				else {
					//the previous league in this slot was the same as this one, so just update the body (so that the expanded/collapsed state is maintained)
//					alert(jQuery(body[index]).children());
					jQuery('#league-list #sc'+displayedLeague[index]).empty().append(jQuery(body[index]).children());
				}
			}
			
			//if this update has fewer leagues than the previous one, delete all the extra leagues
			if (displayedLeague.length > league.length) {
				jQuery('#league-list .container1:eq('+(league.length)+'), #league-list .container1:gt('+(league.length)+'), #league-list .switchcontent:eq('+(league.length)+'), #league-list .switchcontent:gt('+(league.length)+')').remove();
//				jQuery('#league-list #box'+displayedLeague[league.length]).nextAll().remove();  This would mess up if things were reordered?
			}
			
			//reload ccollect (used in Collapse/Expand All) in case the leagues changed
			var alltags=document.all? document.all : document.getElementsByTagName("*");
			ccollect=getElementbyClass(alltags, "switchcontent");
			
			//save the list of leagues that are now on the page for comparison in the next update
			displayedLeague = league;
			
			//if all leagues are final fallback to a slower update frequency
			if (gamesFinal == false)
				updateThread = setTimeout("updateScores()", updateDelay);
			else if (finalUpdateDelay > 0)
				updateThread = setTimeout("updateScores()", finalUpdateDelay);
		},
		error: function(xmlBody, textStatus, errorThrown) {
			updateThread = setTimeout("updateScores()", updateDelay * 2);
		}
	});
}

//when the page loads, start updating the data
$(document).ready(function() {
	updateThread = setTimeout("updateScores()", updateDelay);
});


