addLoadEvent(function() {
	if ($("scoreboard_map")) {
		if (GBrowserIsCompatible()) {
			GVBapp.scoreMap = new GMap2(document.getElementById("scoreboard_map"));
			GVBapp.scoreMap.addControl(new GLargeMapControl());
			GVBapp.scoreMap.setCenter(new GLatLng(39.04576, -78.39844), 1);
			
			GVBapp.scoreOverlay = new GGeoXml($("scoreboard_kml").value + "?" + GVBapp.ts);
			
			GVBapp.scoreMap.addOverlay(GVBapp.scoreOverlay);
		}
		
		$("tab_scores").onclick = GVBapp.switchTab;
		$("tab_map").onclick = GVBapp.switchTab;
		
		GVBapp.switchTab();
		
		window.setTimeout("GVBapp.updateScores()", GVBapp.refreshRate * 1000)
	}
});

GVBapp.updateScores = function() {
	var sackRequest = new sack(GVBapp.blogInfo.scorekeeperPluginUrl + "/refresh.php" );    
	
	sackRequest.execute = 1;
	sackRequest.method = 'GET';
	sackRequest.onError = function() { alert("Ajax error refreshing map") };
	
	sackRequest.runAJAX();
	
	window.setTimeout("GVBapp.updateScores()", GVBapp.refreshRate * 1000)
}

GVBapp.resetOverlay = function(ts) {
	GVBapp.scoreMap.removeOverlay(GVBapp.scoreOverlay);
	
	url = $("scoreboard_kml").value;
	
	if (ts)
		url += "?" + ts;
	
	GVBapp.scoreOverlay = new GGeoXml(url);
	GVBapp.scoreMap.addOverlay(GVBapp.scoreOverlay);
}

GVBapp.updateTable = function(scoreObj) {
	$("total_red_score").innerHTML = scoreObj.score.red;
	$("total_blue_score").innerHTML = scoreObj.score.blue;
	$("time_clock").innerHTML = scoreObj.score.time;
	
	if (!scoreObj.games)
		return;
	
	var tableBody = $("score_table_body");
	var topRow = tableBody.getElementsByTagName("tr")[0];	// TODO handle no-scores case - add a "no scores" table row
	
	var ng = scoreObj.games.length;
	
	// Process the games in order of least recently updated to most recently updated.
	for (i = ng-1; i >=0; i--) {
		var game = scoreObj.games[i];
		var row;
		
		if (!game.id)
			continue;
		
		// is the game listed in the table already?
		if (tmpRow = $("row_" + game.id)) {
			
			var redScore = $("red_" + game.id);
			var blueScore = $("blue_" + game.id);
			
			if (redScore.innerHTML == game.red && blueScore.innerHTML == game.blue)
				continue;	// don't update the table row if the score hasn't been updated
				
			// if it is -- update its row and move it to the top of the table
			var visible = tmpRow.style.display;
			tmpRow.style.display = "none";
			
			redScore.innerHTML = game.red;
			blueScore.innerHTML = game.blue;
			$("time_" + game.id).innerHTML = game.time;
			
			if (tmpRow.id != topRow.id) {
				// move this row to the top
				row = tableBody.removeChild(tmpRow);
				tableBody.insertBefore(row, topRow);
				row.style.display = visible;
				topRow = row;
			} else {
				// we're already on top
				tmpRow.style.display = visible;
				row = tmpRow;
			}
		} else {
			// if it isn't -- add a new row to the top of the table
			row = GVBapp.createScoreRow(game);		
			tableBody.insertBefore(row, topRow);
			
			if (topRow.id == "no_scores")
				$("no_scores").style.display = "none";
				
			topRow = row;
		}
		
		yellowFadeMultiple(row.getElementsByTagName("td")); 
	}
}

GVBapp.switchTab = function() {
	if (this.id == "tab_map") {
		$("tab_scores").className = "";
		this.className = "selected";
		$("wrap_scores").style.display = "none";
		$("wrap_map").style.display = "block";
	} else {
		$("tab_map").className = "";
		this.className = "selected";
		$("wrap_map").style.display = "none";
		$("wrap_scores").style.display = "block";
	}
	return false;
}

GVBapp.createScoreRow = function(game) {
	var row = constructElement("tr", {id: "row_" + game.id});

	// Game update timestamp
	row.appendChild(constructElement("td", {innerHTML: game.time}));
	
	// Avatar table cell
	var avOpts = {className: "gvb_avatar"}
	if (game.avatar) {
		avOpts.innerHTML = "<img src=" + game.avatar + " alt='' />";
	} else {
		avOpts.innerHTML = "&nbsp;";
	}
	row.appendChild(constructElement("td", avOpts));
	
	// Game name
	row.appendChild(constructElement("td", {innerHTML: game.name}));
	
	// Location
	row.appendChild(constructElement("td", {className: "loc", innerHTML: game.location}));
	
	// Red score
	row.appendChild(constructElement("td", {className: "red", id: "red_" + game.id, innerHTML: game.red}));
	
	// Blue score
	row.appendChild(constructElement("td", {className: "blue", id: "blue_" + game.id, innerHTML: game.blue}));
	
	return row;
}

