
var HOST="";var sportNames=new Array("CFL","MLB","NBA","NCAA M BB","NCAA W BB","NCAA Ftbl","NFL","NHL","Racing","Soccer");var sportAbbreviations=new Array("cfl","mlb","nba","ncaab","ncaabw","ncaaf","nfl","nhl","racing","soccer");var m_games=new Array();var m_selectedSports=new Array();function startup(){loadPreviouslySelectedSports();loadSports();loadData(true);setTimeout(scrollTo,500,0,1);window.setInterval(refresh_start,10000);}
function loadPreviouslySelectedSports(){if(readCookie("selectedSports")==null){m_selectedSports=sportAbbreviations;}else{m_selectedSports=readCookie("selectedSports").split(",");}}
function refresh_start(){document.getElementById("button-refresh").addClass("on");document.getElementById("button-refresh").onclick=function(){};loadData(false);}
function refresh_end(){document.getElementById("button-refresh").removeClass("on");document.getElementById("button-refresh").onclick=refresh_start;}
function options_show(){document.getElementById("options-pane").style.visibility="visible";document.getElementById("button-options").addClass("on");document.getElementById("button-options").onclick=options_hide;}
function options_hide(){loadData(true);document.getElementById("options-pane").style.visibility="hidden";document.getElementById("button-options").removeClass("on");document.getElementById("button-options").onclick=options_show;}
function help_show(){document.getElementById("help-pane").style.visibility="visible";document.getElementById("button-help").addClass("on");document.getElementById("button-help").onclick=help_hide;}
function help_hide(){document.getElementById("help-pane").style.visibility="hidden";document.getElementById("button-help").removeClass("on");document.getElementById("button-help").onclick=help_show;}
function loadSports(){var ulSports=document.getElementById("sports");var a,li;for(var i=0;i<sportNames.length;i++){li=document.createElement("li");li.innerText=sportNames[i];li.abbreviation=sportAbbreviations[i];if(m_selectedSports.contains(sportAbbreviations[i])){li.className="on";}
li.onclick=(function(){var abbr=sportAbbreviations[i];return function(){if(this.className=="on"){this.className="";}else{this.className="on";}
var lis=document.getElementById("sports").getElementsByTagName("li");m_selectedSports=new Array();for(var i=0;i<lis.length;i++){if(lis[i].className=="on"){m_selectedSports.push(lis[i].abbreviation);}}
createCookie("selectedSports",m_selectedSports);}})();ulSports.appendChild(li);}}
function loadData(fullRepaint){if(m_selectedSports.length==0){clearGames();refresh_end();return;}
var xmlhttp=new XMLHttpRequest();xmlhttp.open("GET",getAPIURL("/api/games/compact"),true);xmlhttp.onreadystatechange=function checkReadyState(){if(xmlhttp.readyState==4){processData(fullRepaint,xmlhttp.responseText);}}
xmlhttp.send(null);}
function processData(fullRepaint,gamesData){if(fullRepaint){reloadGames(gamesData);}else{refreshGames(gamesData);}}
function reloadGames(gamesData){clearGames();var rawGames=gamesData.split("\n");var rawGame;for(var i=0;i<rawGames.length;i++){rawGame=rawGames[i].split("|");if(!m_selectedSports.contains(rawGame[1])){continue;}
m_games.push(createGameFromRawData(rawGame));}
var li,div;for(var i=0;i<m_games.length;i++){li=document.createElement("li");li.id="game"+m_games[i].getID();if(m_games[i].isFinished()){li.addClass("finished");}
li.onclick=(function(){var g=m_games[i];var l=li;return function(){expandGame(l,g);}})();div=document.createElement("div");div.className="points "+m_games[i].getPointsLevel();div.innerText=m_games[i].getPoints();li.appendChild(div);div=document.createElement("div");div.className="score";div.innerHTML=m_games[i].getScoreText();if(m_games[i].getRationale()!=null){li.addClass("rationale");}
li.appendChild(div);document.getElementById("games").appendChild(li);}
refresh_end();loadAirings();}
function loadAirings(){var request=new XMLHttpRequest();request.open("GET",getAPIURL("/api/airings?providerID=TX60430&compact"),true);request.onreadystatechange=function checkReadyState(){if(request.readyState==4){var airings=request.responseText.split("\n");var airing;var gameID,callSign,channel,imageID;var li;for(var i=0;i<airings.length;i++){airing=airings[i].split("|");gameID=airing[1];callSign=airing[2];channel=airing[3];imageID=airing[4];for(var j=0;j<m_games.length;j++){if(m_games[j].getID()==gameID){m_games[j].addStation(new Station(callSign,channel,imageID));break;}}
li=document.getElementById("game"+gameID);if(li&&imageID){li.style.backgroundImage="url('/images/stations/"+imageID+"/small?20091121235310')";}}}}
request.send(null);}
function refreshGames(gamesData){var rawGames=gamesData.split("\n");var rawGame;var games=new Array();for(var i=0;i<rawGames.length;i++){rawGame=rawGames[i].split("|");if(!m_selectedSports.contains(rawGame[1])){continue;}
if(((rawGame[1]=="ncaab")&&(rawGame[5]=="")&&(rawGame[10]==""))||((rawGame[1]=="ncaabw")&&(rawGame[5]=="")&&(rawGame[10]==""))||((rawGame[1]=="ncaaf")&&(rawGame[5]=="")&&(rawGame[10]==""))){continue;}
games.push(createGameFromRawData(rawGame));}
var updatedGames=new Array();for(var i=0;i<games.length;i++){for(var j=0;j<m_games.length;j++){if((games[i].getID()==m_games[j].getID())&&games[i].hasChanged(m_games[j])){updatedGames.push(games[i]);break;}}}
if(updatedGames.length==0){refresh_end();return;}else{m_games=games;runUpdate(updatedGames);}}
function runUpdate(updatedGames){for(var i=0;i<updatedGames.length;i++){document.getElementById("game"+updatedGames[i].getID()).style.backgroundColor="#ffff99";}
var game;var ul,li,div,a,img;window.setTimeout(updateScores,2000);function updateScores(){for(var i=0;i<updatedGames.length;i++){game=updatedGames[i];li=document.getElementById("game"+game.getID());li.getElementsByTagName("div")[0].innerText=game.getPoints();li.getElementsByTagName("div")[0].className="points "+game.getPointsLevel();li.getElementsByTagName("div")[1].innerHTML=game.getScoreText();if(game.getRationale()!=null){li.addClass("rationale");}else{li.removeClass("rationale");}
if(game.isFinished()){li.addClass("finished");}else{li.removeClass("finished");}}}
window.setTimeout(unhighlightGames,2500);function unhighlightGames(){for(var i=0;i<updatedGames.length;i++){var obj=document.getElementById("game"+updatedGames[i].getID());obj.hexColor=153;obj.interval=window.setInterval(function(obj){return function(){var hexColor=obj.hexColor+21;obj.style.backgroundColor="rgb(255, 255, "+hexColor+")";obj.hexColor=hexColor;if(hexColor>=255){clearInterval(obj.interval);obj.style.backgroundColor="";}}}(obj),250);}}
window.setTimeout(refresh_end,3500);}
function expandGame(li,game){var xmlhttp=new XMLHttpRequest();xmlhttp.open("GET",getAPIURL("/api/news?gameID="+game.getID()),true);xmlhttp.overrideMimeType("text/xml");xmlhttp.onreadystatechange=function checkReadyState(){if(xmlhttp.readyState==4){var newsStories=xmlhttp.responseXML.getElementsByTagName("description");if(newsStories.length==0){alert("There are no news articles for "+((game.getTeam1Name()==null)?game.getLocation():game.getTeam1Name()+" vs. "+game.getTeam2Name()));}else{var h2=document.createElement("h2");h2.innerHTML=xmlhttp.responseXML.getElementsByTagName("title")[0].firstChild.nodeValue;li.appendChild(h2);var stations=game.getStations();if(stations.length>0){var div,img;div=document.createElement("div");div.addClass("stations");for(var i=0;i<stations.length;i++){if(stations[i].getImageID()!=null){img=document.createElement("img");img.src="/images/stations/"+stations[i].getImageID()+"/small?20091121235310";div.innerText=stations[i].getCallSign()+" ("+stations[i].getChannel()+") ";div.insertBefore(img,div.firstChild);}}
li.appendChild(div);}
var p=document.createElement("p");p.innerHTML=newsStories[0].firstChild.nodeValue.replace(/\n\n/g,"<br/><br/>");li.appendChild(p);li.addClass("expanded");scrollToY(document.body,findElementPosition(li)[1]-4);li.onclick=(function(){var l=li;var g=game;return function(){collapseGame(l,g);}})();}}}
xmlhttp.send(null);}
function collapseGame(li,game){li.removeClass("expanded");li.removeChild(li.getElementsByTagName("h2")[0]);li.removeChild(li.getElementsByTagName("p")[0]);li.onclick=(function(){var l=li;var g=game;return function(){expandGame(l,g);}})();}
function clearGames(){m_games=new Array();while(document.getElementById("games").firstChild){document.getElementById("games").removeChild(document.getElementById("games").firstChild);}}
function scrollToY(obj,toPos){var fromPos=(obj.scrollTop=="")?0:Number(obj.scrollTop.replace("px",""));if(fromPos==toPos){return;}
var currentStep=0;var totalSteps=5;scroll();var timer=setInterval(scroll,0);function scroll(){currentStep=currentStep+1;if(slidePath(currentStep,totalSteps)==1){clearInterval(timer);}
obj.scrollTop=fromPos+((toPos-fromPos)*(slidePath(currentStep,totalSteps)));}}
function slidePath(currentStep,steps){return Math.pow((currentStep/steps),1);}
function getAPIURL(command){return HOST+command+((command.indexOf("?")==-1)?"?":"&")+"appID=iphone&appVersion=1.0";}
function getURL(url){window.open(url);}
function getTime(date){return((date.getHours()%12==0)?"12":date.getHours()%12)+":"+((date.getMinutes()<10)?"0":"")+date.getMinutes()+((date.getHours()>11)?"p":"a");}
function findElementPosition(obj){var curleft=curtop=0;if(obj.offsetParent){curleft=obj.offsetLeft
curtop=obj.offsetTop
while(obj=obj.offsetParent){curleft+=obj.offsetLeft
curtop+=obj.offsetTop}}
return[curleft,curtop];}
function createGameFromRawData(rawGameData){var gameID=rawGameData[0];var sport=rawGameData[1];var points=rawGameData[14];var highPoints=rawGameData[17];var pointsLevel=rawGameData[15].toLowerCase();var date=new Date(rawGameData[19]);var time=null;if(rawGameData[20]){time=new Date(rawGameData[20]);}
var timeLeft=null;if(rawGameData[13]){timeLeft=rawGameData[13];}
var game=new Game(gameID,sport,points,highPoints,pointsLevel,date,time,timeLeft);if((rawGameData[2])&&(rawGameData[7])){if(rawGameData[4]){game.setTeam1Name(rawGameData[4]);}else{game.setTeam1Name(rawGameData[3]);}
game.setTeam1ID(rawGameData[2]);game.setScore1(rawGameData[6]);if(rawGameData[5]){game.setTeam1Ranking(rawGameData[5]);}
if(rawGameData[9]){game.setTeam2Name(rawGameData[9]);}else{game.setTeam2Name(rawGameData[8]);}
game.setTeam2ID(rawGameData[7]);game.setScore2(rawGameData[11]);if(rawGameData[10]){game.setTeam2Ranking(rawGameData[10]);}}
if(rawGameData[18]){game.setRationale(rawGameData[18]);}
if(rawGameData[12]){game.setLocation(rawGameData[12]);}
if(rawGameData[21]){game.setHeadline(rawGameData[22]);}
return game;}
function Station(channel,callSign,name){this._channel=channel;this._callSign=callSign;this._name=name;}
Station.prototype.getChannel=function(){return this._channel;}
Station.prototype.getCallSign=function(){return this._callSign;}
Station.prototype.getName=function(){return this._name;}
function Game(gameID,sport,points,highPoints,pointsLevel,date,time,timeLeft){this._ID=parseInt(gameID);this._sport=sport;this._points=parseInt(points);this._highPoints=parseInt(highPoints);this._pointsLevel=pointsLevel;this._date=date;this._time=time;this._timeLeft=timeLeft;this._rationale=null;this._team1Name=null;this._team1ID=null;this._team2Name=null;this._team2ID=null;this._score1=null;this._score2=null;this._location=null;this._headline=null;this._stations=new Array();}
Game.prototype.getID=function(){return this._ID;}
Game.prototype.getSport=function(){return this._sport;}
Game.prototype.getPoints=function(){return this._points;}
Game.prototype.getHighPoints=function(){return this._highPoints;}
Game.prototype.getPointsLevel=function(){return this._pointsLevel;}
Game.prototype.getRationale=function(){return this._rationale;}
Game.prototype.setRationale=function(rationale){this._rationale=rationale;}
Game.prototype.getDate=function(){return this._date;}
Game.prototype.getTime=function(){return this._time;}
Game.prototype.getTimeLeft=function(){return this._timeLeft;}
Game.prototype.getTeam1Name=function(){return this._team1Name;}
Game.prototype.setTeam1Name=function(team1Name){this._team1Name=team1Name;}
Game.prototype.getTeam1ID=function(){return this._team1ID;}
Game.prototype.setTeam1ID=function(team1ID){this._team1ID=parseInt(team1ID);}
Game.prototype.getTeam1Ranking=function(){return this._team1Ranking;}
Game.prototype.setTeam1Ranking=function(team1Ranking){this._team1Ranking=parseInt(team1Ranking);}
Game.prototype.getTeam2Name=function(){return this._team2Name;}
Game.prototype.setTeam2Name=function(team2Name){this._team2Name=team2Name;}
Game.prototype.getTeam2ID=function(){return this._team2ID;}
Game.prototype.setTeam2ID=function(team2ID){this._team2ID=parseInt(team2ID);}
Game.prototype.getTeam2Ranking=function(){return this._team2Ranking;}
Game.prototype.setTeam2Ranking=function(team2Ranking){this._team2Ranking=parseInt(team2Ranking);}
Game.prototype.getLocation=function(){return this._location;}
Game.prototype.setLocation=function(location){this._location=location;}
Game.prototype.getScore1=function(){return this._score1;}
Game.prototype.setScore1=function(score1){if(!isNaN(parseInt(score1))){this._score1=parseInt(score1);}}
Game.prototype.getScore2=function(){return this._score2;}
Game.prototype.setScore2=function(score2){if(!isNaN(parseInt(score2))){this._score2=parseInt(score2);}}
Game.prototype.getHeadline=function(){return this._headline;}
Game.prototype.setHeadline=function(headline){this._headline=headline;}
Game.prototype.addStation=function(station){this._stations.push(station);}
Game.prototype.getStations=function(){return this._stations;}
Game.prototype.isInProgress=function(){return(this.hasStarted()&&!this.isFinished());}
Game.prototype.hasStarted=function(){return(this.getTimeLeft()!=null);}
Game.prototype.isFinished=function(){return(this.hasStarted()&&(this.getTimeLeft().indexOf("Final")!=-1));}
Game.prototype.hasChanged=function(otherGame){return(this._points!=otherGame.getPoints())||(this._highPoints!=otherGame.getHighPoints())||(this._score1!=otherGame.getScore1())||(this._score2!=otherGame.getScore2())||(this._timeLeft!=otherGame.getTimeLeft())||(this._rationale!=otherGame.getRationale());}
Game.prototype.getScoreText=function(){var scoreNodeText="";if((this._team1Name==null)||(this._team2Name==null)){scoreNodeText=this._location+", ";if(this.getTimeLeft()!=null){scoreNodeText+=this._timeLeft;}else{scoreNodeText+=((this._time!=null)?getTime(this._time):"TBA");}}else{if(this.getScore1()!=null){if(this.getScore2()>this.getScore1()){scoreNodeText=this._team2Name+(this._team2Ranking?"<sup>"+this._team2Ranking+"</sup>":"")+" "+this._score2+", "+this._team1Name+(this._team1Ranking?"<sup>"+this._team1Ranking+"</sup>":"")+" "+this._score1+"    "+this._timeLeft.replace(" Quarter","").replace(" Period","").replace(" Half","").replace(" of","");}else{scoreNodeText=this._team1Name+(this._team1Ranking?"<sup>"+this._team1Ranking+"</sup>":"")+" "+this._score1+", "+this._team2Name+(this._team2Ranking?"<sup>"+this._team2Ranking+"</sup>":"")+" "+this._score2+"    "+this._timeLeft.replace(" Quarter","").replace(" Period","").replace(" Half","").replace(" of","");}}else{scoreNodeText=this._team1Name+(this._team1Ranking?"<sup>"+this._team1Ranking+"</sup>":"")+" vs. "+this._team2Name+(this._team2Ranking?"<sup>"+this._team2Ranking+"</sup>":"")+"    "+((this._time!=null)?getTime(this._time):"TBA");}}
if(this._rationale!=null){scoreNodeText+="<br/><em>"+this._rationale+"</em>";}
return scoreNodeText;}
function Station(callSign,channel,imageID){this._callSign=callSign;this._channel=channel;if(!isNaN(imageID)){this._imageID=imageID;}else{this._imageID=null;}}
Station.prototype.getCallSign=function(){return this._callSign;}
Station.prototype.getChannel=function(){return this._channel;}
Station.prototype.getImageID=function(){return this._imageID;}
Object.prototype.addClass=function(c){if(!this.className.split(" ").contains(c)){this.className+=" "+c;}}
Object.prototype.removeClass=function(c){var classNames=this.className.split(" ");if(classNames.contains(c)){this.className="";for(var i=0;i<classNames.length;i++){this.className+=" "+((classNames[i]!=c)?classNames[i]:"");}}}
Array.prototype.contains=function(item){for(var i=0;i<this.length;i++){if(this[i]==item){return true;}}
return false;};function trim(str){return str.replace(/^\s*|\s*$/g,"");}
function createCookie(name,value){var date=new Date();date.setTime(date.getTime()+(365*24*60*60*1000));var expires="; expires="+date.toGMTString();document.cookie=name+"="+value+expires+"; path=/";}
function readCookie(name){var nameEQ=name+"=";var ca=document.cookie.split(';');for(var i=0;i<ca.length;i++){var c=ca[i];while(c.charAt(0)==' ')c=c.substring(1,c.length);if(c.indexOf(nameEQ)==0)return c.substring(nameEQ.length,c.length);}
return null;}
