var waittime=30000;
function update_lastupdate(){
	lastupdatediv = document.getElementById("lastupdate");
	while(lastupdatediv.hasChildNodes()){
		lastupdatediv.removeChild(lastupdatediv.firstChild);
	}
	var newmsg = "";
	if(window.nextgame == 0){
		newmsg = "Updated "+formatTime('%m-%d-%y, %h:%i%p', window.lastupdate*1)+" - Live update every "+(window.waittime/1000)+" seconds!"
	}else if(window.nextgame == -1){
		newmsg = "Updated "+formatTime('%m-%d-%y, %h:%i%p', window.lastupdate*1)+" - All games are over, come back next week.";
	}else{
		var nextgamestring = formatTime('%a at %h:%i%p', window.nextgame*1);
		newmsg = "Updated "+formatTime('%m-%d-%y, %h:%i%p', window.lastupdate*1)+" - Live updates resume on "+nextgamestring+".";
	}
	lastupdatediv.appendChild(document.createTextNode(newmsg));
}
function parsetimestamps(xml){
	//instantiate a new XMLDoc object. Send any errors to the xmlError function
	var objDom = new XMLDoc(xml, xmlError);
	//get the root node
	var objDomTree = objDom.docNode;

	window.waittime = objDomTree.getElements("refreshrate")[0].getText() * 1000;
	if(window.waittime < 3000){
		window.waittime = 3000;
	}
	window.lastupdate = objDomTree.getElements("lastupdate")[0].getText();
	window.live = objDomTree.getElements("live")[0].getText();
	window.nextgame = objDomTree.getElements("nextgame")[0].getText();
	
	update_lastupdate();
	if(window.live == 0){
		setnextrefresh();
	}

}

function xmlError(e) {
//there was an error, show the user
//alert(e);
} //end function xmlError


function ajax_request2(url){http_request2=false;if(window.XMLHttpRequest){http_request2=new XMLHttpRequest();if(http_request2.overrideMimeType){http_request2.overrideMimeType('text/xml');}}else if(window.ActiveXObject){try{http_request2=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{http_request2=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}}}
if(!http_request2){alert('Giving up :( Cannot create an XMLHTTP instance');return false;}
http_request2.onreadystatechange=alertContents2;
http_request2.open('GET',url,true);
http_request2.send(null);}

function alertContents2(){
	if(http_request2.readyState==4){
		if(http_request2.status==200){
			parsetimestamps(http_request2.responseText);
		}
			http_request2 = 0;
	}
}
function setnextrefresh(){
	window.clearInterval(window.interval);
	var d = new Date();
	if( window.nextgame > d.getTime() )
	{
    	window.interval = window.setInterval("read_cont()", window.nextgame - d.getTime());
	}
	else if( window.nextgame == -1 )
	{
		//all games are over, do nothing.
	}
	else
	{
		window.interval = window.setInterval("read_cont()", window.waittime);
	}
}//end function setnextrefresh
