// Preload the appropriate images

function preload() {
  if (!document.images) return;
  var ar = new Array();
  var arguments = preload.arguments;
  for (var i = 0; i < arguments.length; i++) {
    ar = new Image();
    ar.src = arguments;
  }
}


//-------------------------------------------
// Create an XML document object parser using Javascript
// running in Internet Explorer 5.0 or later create
//-------------------------------------------

var xmlDoc=null;
var isIE= false;
var parsingCompleted=true;
var updateInterval=8000;


stra = window.location.search.substr(1).split("&"); 
height_str = stra[0].split("=");
length_str = stra[1].split("=");

var strliveid = height_str[1];
var recordid  = length_str[1];

var strDataSource= "/mbp/radio/" + recordid + ".xml";

//------------------------------------------------------
// The beginUpdates() function starts by checking which
// browser we're using and trying to get an XML document object to work with
//------------------------------------------------------

function beginUpdates()
{

	if (document.all) {
		isIE=true;
	}

	//determine if this is a standards-compliant browser like Mozilla
   	if (document.implementation && document.implementation.createDocument) {

      	//create the DOM Document
      	xmlDoc = document.implementation.createDocument("","", null);

   	} else if (isIE) {
		//create the DOM Document - IE specific
		xmlDoc = new ActiveXObject("microsoft.xmldom");
   	}

	if (xmlDoc==null) {
		//no supported way of loading the xml document - send user to error page.
		//alert("Your browser does not support XML processing, please upgrade.");

		window.location = "pleaseupgrade.htm"
	}
	else
	{
		//created initial xml doc ok -proceed with loading the xml document for the first time
		loadDoc();
	}

}



//------------------------------------------------------
// The loadDoc() function attaches a callback for handling the loaded xml document, then loads the document itself
// once loading is complete the callback function UpdateValues will be called and the page updated
//
// The XMLDOM parser reloads the same XML file. Because a server will provide this file, the web tags will be reevaluated to provide the latest values.
//------------------------------------------------------


function loadDoc()
{
	if (parsingCompleted==false) return; //skip trying to load the xml document if for some reason we didn't manage to complete parsing last time

	//setup a callback function to invoke (UpdateValues) after the xml document has been successfully loaded
	if (isIE) {
		xmlDoc.async = false;
		xmlDoc.onreadystatechange=UpdateValues; //callback on state change
	}
	else {
		//Mozilla/Standards Compliant callback
		xmlDoc.addEventListener("load", UpdateValues, false);
	}

	//now load the actual xml document
	xmlDoc.load(strDataSource);

}

//------------------------------------------------------
// The UpdateValues() retrieves data from the XML parser and updates the text of SPAN tags in the html web page.
//
// To notify the user of an update, the current time
// is retrieved using GetTime() and copied to the status bar.
//
// page will display
// 'ON' for '100 1' and 'OFF' for '0 0'.
//------------------------------------------------------

function UpdateValues()
{

	if (isIE && xmlDoc.readyState != "4") return;  //IE Specific check - return from callback if not ready to parse data

	if (xmlDoc==null) {
		alert("could not load xml document");
		return;
	}
	if (xmlDoc.documentElement==null) {
		alert("could not load xml document -error in document" + strDataSource);
		return;
	}
	var rootElem = xmlDoc.documentElement;

	//example of copying values from the XML parser to the HTML tags

	var liveid;
	var website;
        var event_date;
        var br_type;
        var venue;        
	var home_name;
        var comp_type;
	var hplayer_name;
	var aplayer_name;
	var team_place;
        var play;


	if (isIE) {

		liveid = rootElem.childNodes.item(0).text;
                website = rootElem.childNodes.item(1).text;
                event_date = rootElem.childNodes.item(2).text;
                br_type = rootElem.childNodes.item(3).text;
                venue = rootElem.childNodes.item(4).text;
		home_name = rootElem.childNodes.item(5).text;
		comp_type = rootElem.childNodes.item(6).text;
		hplayer_name = rootElem.childNodes.item(7).text;
		aplayer_name = rootElem.childNodes.item(8).text;
		team_place = rootElem.childNodes.item(9).text;
		play = rootElem.childNodes.item(10).text;

	}
	else {

		var variableNodes = xmlDoc.getElementsByTagName("liveid");
		if (variableNodes[0].firstChild) liveid=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("website");
		if (variableNodes[0].firstChild) website=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("event_date");
		if (variableNodes[0].firstChild) event_date=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("br_type");
		if (variableNodes[0].firstChild) br_type=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("venue");
		if (variableNodes[0].firstChild) venue=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("home_name");
		if (variableNodes[0].firstChild) home_name=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("comp_type");
		if (variableNodes[0].firstChild) comp_type=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("hplayer_name");
		if (variableNodes[0].firstChild) hplayer_name=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("aplayer_name");
		if (variableNodes[0].firstChild) aplayer_name=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("team_place");
		if (variableNodes[0].firstChild) team_place=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("play");
		if (variableNodes[0].firstChild) play=variableNodes[0].firstChild.nodeValue;


	}


	document.getElementById("liveid").innerHTML=liveid;
	document.getElementById("website").innerHTML=website;
	document.getElementById("event_date").innerHTML=event_date;
	document.getElementById("br_type").innerHTML=br_type;
	document.getElementById("venue").innerHTML=venue;
	document.getElementById("home_name").innerHTML=home_name;
	document.getElementById("comp_type").innerHTML=comp_type;
	document.getElementById("hplayer_name").innerHTML=hplayer_name;
	document.getElementById("aplayer_name").innerHTML=aplayer_name;
	document.getElementById("team_place").innerHTML=team_place;
	document.getElementById("play").innerHTML=play;


	//update the status bar

	window.status = "MES Last Update: " + GetTime();

	parsingCompleted=true;

	//after a completed update, get ready to load the document again in 2 secs
	window.setTimeout("loadDoc()",updateInterval)
}


//------------------------------------------------------
// The GetTime() function retrieves the current date and
// time. The time is then formatted using the IfZero function
// to prefix a 0 for single digit numbers.
//------------------------------------------------------

function GetTime()
{
	var dt = new Date();
	curTime = (IfZero(dt.getHours()) + ":" +
	IfZero(dt.getMinutes()) + ":" + IfZero(dt.getSeconds()));
	return curTime;
}

//------------------------------------------------------
// The IfZero() function prefixes a 0 to a number if it is a
// single digit.
//
// This will guarantee that the hour, second, and minute
// fields are always formatted to hh:mm:ss
//------------------------------------------------------

function IfZero(num)
{
	return ((num <= 9) ? ("0" + num) : num);
}
