/*

------------
Installation
------------

Call jaxScoreboardRegister() during your document's onLoad() event to add a 
baseball scoreboard to your document.  The scoreboard will be appended to the 
element specified by you, and will bear dimensions + look and feel as described 
by the jax-scoreboard.css file.  Some steps you'll need to follow to get it working:

	1) include jax-scoreboard.js in your html (via <script> tag),
	2) include common/jax-xmlhttprequest.js  in your html (via <script> tag),
	3) include the contents of jax-scoreboard.css in your html or other css 
	   file (via <style> or <link> tags),
	4) in the body of your html have a tag with an id (preferably a <div> tag),
	5) call jaxScoreboardRegister() on document load,
	6) write a server-side response script that gathers stored content (like
	   from a database) and returns it.

Parameter descriptions for jaxScoreboardRegister():

	contentURL: url that returns an xml feed to populate the scoreboard
	id:         id of the element (preferably div) to append scoreboard to
	
-------
Credits
-------

Authored by Dan Juliano in December of 2006.

*/

var jsOnClickForwardToUrl = null;

function jaxScoreboardRegister(url, id) {
	jaxScoreboardBuildBoard(id);
	xmlRequest(url, null, jaxScoreboardHandleXML);
}
function jaxScoreboardBuildBoard(id) {
	var html = "\n\n";
	var html_innings = "";
	var html_scores_guest = "";
	var html_scores_home = "";
	
	html += "<div class=\"scoreboard\">\n";
	
	// Build a clickable title bar.
	if (jsOnClickForwardToUrl != null) {
		html += "\t<div class=\"scoreboard_title\"><a id=\"scoreboard_link\" class=\"scoreboard_link\" href=\"" + jsOnClickForwardToUrl + "\">SCOREBOARD</a></div>\n";
	} else {
		html += "\t<div class=\"scoreboard_title\">SCOREBOARD</div>\n";
	}
	
	// Build the scoreboard.
	for (var i = 1; i < 8; i++) {
		html_innings += "\t\t\t<td class=\"scoreboard_inning\">" + i + "</td>\n";
		html_scores_guest += "\t\t\t<td class=\"scoreboard_score\">&nbsp;</td>\n";
		html_scores_home += "\t\t\t<td class=\"scoreboard_score\">&nbsp;</td>\n";
	}

	html_innings = "\t\t\t<td class=\"scoreboard_rhe\">&nbsp;</td>\n" + html_innings + "\t\t\t<td>&nbsp;</td>\n";
	html_innings += "\t\t\t<td class=\"scoreboard_inning\">R</td>\n";
	html_innings += "\t\t\t<td class=\"scoreboard_inning\">H</td>\n";
	html_innings += "\t\t\t<td class=\"scoreboard_inning\">E</td>\n";

	html_scores_guest = "\t\t\t<td class=\"scoreboard_rhe\"></td>\n" + html_scores_guest + "\t\t\t<td>&nbsp;</td>\n";
	html_scores_guest += "\t\t\t<td class=\"scoreboard_score\" id=\"scoreboard_guest_r\"></td>\n";
	html_scores_guest += "\t\t\t<td class=\"scoreboard_score\" id=\"scoreboard_guest_h\"></td>\n";
	html_scores_guest += "\t\t\t<td class=\"scoreboard_score\" id=\"scoreboard_guest_e\"></td>\n";

	html_scores_home = "\t\t\t<td class=\"scoreboard_rhe\"></td>\n" + html_scores_home + "\t\t\t<td>&nbsp;</td>\n";
	html_scores_home += "\t\t\t<td class=\"scoreboard_score\" id=\"scoreboard_home_r\"></td>\n";
	html_scores_home += "\t\t\t<td class=\"scoreboard_score\" id=\"scoreboard_home_h\"></td>\n";
	html_scores_home += "\t\t\t<td class=\"scoreboard_score\" id=\"scoreboard_home_e\"></td>\n";
	
	// Build a spot for a short summary of which teams scored what.
	html += "\t<table class=\"scoreboard_table\"><tbody>\n";
	html += "\t\t<tr>\n";
	html += html_innings;
	html += "\t\t</tr>\n";
	html += "\t\t<tr id=\"scoreboard_row_guest\">\n";
	html += html_scores_guest;
	html += "\t\t</tr>\n";
	html += "\t\t<tr id=\"scoreboard_row_home\">\n";
	html += html_scores_home;
	html += "\t\t</tr>\n";
	html += "\t</tbody></table>\n\n";
	
	html += "\t<div id=\"scoreboard_message\">\n";
	html += "\t\t<div id=\"scoreboard_row_guest_total\">&nbsp;</div>\n";
	html += "\t\t<div id=\"scoreboard_row_home_total\">&nbsp;</div>\n";
	html += "\t\t<div id=\"scoreboard_date\">&nbsp;</div>\n\n";
	html += "\t</div>\n";
	
	// A 'scoreboard is loading' div will display while 
	// waiting for ajax request to return.
	html += "<div id=\"scoreboard_loading\">Loading</div>\n";
	html += "</div>\n\n";
	
	document.getElementById(id).style.visibility = "visible";
	document.getElementById(id).innerHTML = html;
	
	jaxCommonLoadingMessageReset("scoreboard_loading", "scoreboard_message");
}

function jaxScoreboardHandleXML(xml) {

	// Check to see if relevant xml data is present.
	jaxCommonLoadingMessageHide("scoreboard_loading");
	if (xml.getElementsByTagName("meta").length == 0) {
		document.getElementById("scoreboard_date").innerHTML = "No game to display";
		return;
	}

	jaxScoreboardShowMeta(xml);
	jaxScoreboardXMLtoBoard(xml, "guest", "scoreboard_row_guest");
	jaxScoreboardXMLtoBoard(xml, "home", "scoreboard_row_home");
}

// Fill in the score by inning for a given team.
function jaxScoreboardXMLtoBoard(xml, name_xml, name_td) {

	var team_xml = xml.getElementsByTagName(name_xml)[0].childNodes;
	var team_td = document.getElementById(name_td).getElementsByTagName("TD");
	var counter = 1; // first td is a spacer
	for (var i = 0; i < team_xml.length; i++) {
		if (team_xml[i].nodeType == 1) {
			if (counter < 8) {
				team_td[counter++].innerHTML = team_xml[i].firstChild.nodeValue;
			}
		}
	}
}

// Meta information: team x scored 5 points, team y scored 3 points.
function jaxScoreboardShowMeta(xml) {

	var meta = xml.getElementsByTagName("meta")[0].childNodes;
	
	document.getElementById("scoreboard_date").innerHTML = "Played: " + xmlDigOneValue(xml, "scheduled_date");
	
	var message = "";
	message += xmlDigOneValue(xml, "guest_name") + ": ";
	message += xmlDigOneValue(xml, "guest_runs");
	document.getElementById("scoreboard_row_guest_total").innerHTML = message;
	
	message = "";
	message += xmlDigOneValue(xml, "home_name") + ": ";
	message += xmlDigOneValue(xml, "home_runs");
	document.getElementById("scoreboard_row_home_total").innerHTML = message;

	document.getElementById("scoreboard_guest_r").innerHTML = xmlDigOneValue(xml, "guest_runs");
	document.getElementById("scoreboard_guest_h").innerHTML = xmlDigOneValue(xml, "guest_hits");
	document.getElementById("scoreboard_guest_e").innerHTML = xmlDigOneValue(xml, "guest_errors");

	document.getElementById("scoreboard_home_r").innerHTML = xmlDigOneValue(xml, "home_runs");
	document.getElementById("scoreboard_home_h").innerHTML = xmlDigOneValue(xml, "home_hits");
	document.getElementById("scoreboard_home_e").innerHTML = xmlDigOneValue(xml, "home_errors");
}

function jaxScoreboardOnClickForwardToUrl(url) {
	jsOnClickForwardToUrl = url;
}
