/** * @fileoverview Pickleview is a live MLB stat-tracking web application. * It tracks any team you choose. * * @author Richard Herrera doctyper.com * @author Ryan Christianson ryancafe.com * @author Estelle Weyl estelleweyl.com * @author Wai Seto * @version 1.0 * * @license Pickleview is released according to the Creative Commons Attribution-Noncommercial-Share Alike 3.0 License. You are free to learn from it, distribute and modify (as long as you share alike). * @license-url http://creativecommons.org/licenses/by-nc-sa/3.0/ */ /** * Get first element by tag name * @param {String} e The element to grab * @param {String} f The parent node. If no parent, use document */ var $E = $E || function(e, f) { f = f || document; return f.getElementsByTagName(e)[0]; }; /** * Shorthand getElementById * @param {String} e The element id */ var $ = $ || function(e) { return document.getElementById(e); }; /** * Set, Get, and Delete Cookies * @class This is the Cookie "class" * @param {String} cookieName The name of the cookie * @param {String} cookieValue The cookie value */ var Cookie = function() { return { get : function(name) { var start = document.cookie.indexOf(name + "="); var len = start + name.length + 1; if ((!start) && (name != document.cookie.substring(0, name.length))) { return null; } if (start == -1) { return null; } var end = document.cookie.indexOf(';', len); if (end == -1) { end = document.cookie.length; } return unescape(document.cookie.substring(len, end)); }, set : function(name, value, expires, path, domain, secure) { var today = new Date(); today.setTime(today.getTime()); if (expires) { expires = expires * 1000 * 60 * 60 * 24; } var expires_date = new Date(today.getTime() + (expires)); document.cookie = name + '=' + escape(value) + ((expires) ? ';expires=' + expires_date.toGMTString() : '') + ((path) ? ';path=' + path : '') + ((domain) ? ';domain=' + domain : '') + ((secure) ? ';secure' : ''); }, del : function(name, path, domain) { if (Cookie.get(name)) { document.cookie = name + '=' + ((path) ? ';path=' + path : '') + ((domain) ? ';domain=' + domain : '') + ';expires=Thu, 01-Jan-1970 00:00:01 GMT'; } } }; }(); /** * Remembers your selected team */ var MyFavoriteTeam = Cookie.get("MyPickleviewTeam") || "LA Angels"; /** * Shorthand for Pickleview */ var $P; /** * The Pickleview object * @class This is the Pickleview "class" * @constructor */ var Pickleview = { myTeam : MyFavoriteTeam, maxChats : 5, currentWidth : 0, init : function() { if (!$("Pickleview")) { return; } else { var nojs = $E("div", $("Pickleview")); if (nojs) { var LoadScores = document.createElement("div"); LoadScores.setAttribute("id", "LoadScores"); LoadScores.innerHTML = '
Loading Latest Game Info...
';
$("Pickleview").replaceChild(LoadScores, nojs);
}
}
if (!$P.loadImagesOnce) {
/**
* Let's preload some images
*/
var images = [
"/img//loader/default.gif",
"/img/bubble/corners.png",
"/img/bubble/edges.png",
"/img/bubble/alt/corners.png",
"/img/bubble/alt/edges.png",
"/img/combo-bg.png",
"/img/options/blueButton.png",
"/img/options/grayButton.png",
"/img/options/whiteButton.png",
"/img/options/whiteButtonMini.png",
"/img/options/grayButtonMini.png",
"/img/popup.png",
"/img/score/old-score.png",
"/img/score/new-score.png",
"/img/score/td-bg.png",
"/img/title-button.png"
];
for (var i = images.length - 1; i >= 0; i--) {
var img = new Image();
img.src = images[i];
}
$P.loadImagesOnce = true;
}
if ($P.trigger) {
clearTimeout($P.trigger);
}
$P.PlayByPlay = true;
$P.url = "php/proxy.php";
var url = $P.url + "?output=" + $P.getDate() + "/miniscoreboard.xml";
var req = new XMLHttpRequest();
req.open("GET", url, true);
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200 || req.status == 304) {
$P.handle(req.responseXML);
$P.GamesXML = url;
} else {
console.log("Error: " + req.status + " on $P.init()");
$P.init();
}
}
};
req.send(null);
var cal = new Date(),
date = cal.getDate().toString(),
day = cal.getDay().toString(),
days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
target = $("date").firstChild;
if (date.length && date.length < 2) {
date = "0" + date;
}
target.nodeValue = days[day] + " " + date;
},
getDate : function(e) {
var theDate = new Date();
theDate = new Date(theDate.valueOf() - 36000000 + (theDate.getTimezoneOffset() * 60000));
var year = theDate.getFullYear().toString(),
month = (theDate.getMonth() + 1).toString(),
day = theDate.getDate().toString(),
daysInMonth = 32 - new Date(year, month, 32).getDate(),
date = theDate.getDay(),
gid;
if (e) {
if (e < daysInMonth) {
$P.day = ($P.day + 1) || ((day * 1) + 1);
} else {
$P.day = 1;
month = (parseInt(month) + 1).toString();
}
}
if (month.length !== 2) {
month = "0" + month;
}
day = $P.day || day;
if (typeof day !== "String") {
day = day.toString();
}
if (day.length !== 2) {
day = "0" + day;
}
$P.date = date;
return "year_" + year + "/" + "month_" + month + "/" + "day_" + day;
},
showLoadingIcon : function() {
var target = $("td2");
if (target) {
if (target.firstChild) {
target.removeChild(target.firstChild);
}
var image = document.createElement("img");
image.setAttribute("src", "/img/loader/white.gif");
image.setAttribute("alt", "");
image.setAttribute("id", "loadScores");
target.className = "active";
target.appendChild(image);
}
},
handle : function(e) {
if ((!e) || (!$E("games", e))) {
// No games at all today...
req = new XMLHttpRequest();
var url = $P.url + "?output=" + $P.getDate(true) + "/" + "miniscoreboard.xml";
req.open("GET", url, true);
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200 || req.status == 304) {
$P.handle(req.responseXML);
$P.GamesXML = url;
// Snap to top onload
if (!$P.TriggerTop) {
setTimeout(function() {
window.scrollTo(0, 1);
}, 3000);
$P.TriggerTop = true;
}
} else {
console.log("Error: " + req.status + " on $P.handle()");
$P.handle();
}
}
};
req.send(null);
return;
}
// I need a game ID, Dont'cha know...
var games = e.getElementsByTagName("games")[0].getElementsByTagName("game"),
file = "miniscoreboard.xml",
gameArray = [], game, g, i, j;
if (!$P.GamesTodayOnly) {
var gamesToday = [];
$P.GamesTodayOnly = true;
}
for (i = 0, j = games.length; i < j; i++) {
g = games[i];
if (gamesToday) {
gamesToday.push(g);
$P.GamesToday = gamesToday;
}
if ((g.getAttribute("home_team_city") === $P.myTeam) || (g.getAttribute("away_team_city") === $P.myTeam) || (g.getAttribute("home_team_city") === "NL All-Stars") || (g.getAttribute("away_team_city") === "NL All-Stars")) {
if ((g.getAttribute("home_team_city") === "NL All-Stars") || (g.getAttribute("away_team_city") === "NL All-Stars")) {
$P.myTeam = g.getAttribute("home_team_city");
} else {
gameArray.push(g);
}
}
}
if (gameArray.length > 1) {
// Doubleheader!
if (((gameArray[0].getAttribute("status") != "Game Over") && (gameArray[0].getAttribute("status") != "Final")) || $P.showFirstOfTwo) {
g = gameArray[0];
$P.showFirstOfTwo = false;
} else {
g = gameArray[1];
}
} else {
g = gameArray[0];
}
if (g) {
var dir = g.getAttribute("gameday_link");
$P.home = g.getAttribute("home_team_city");
$P.away = g.getAttribute("away_team_city");
$P.league = g.getAttribute("league");
// If game is in progress, load miniscoreboard
// Else load linescore
file = (g.getAttribute("status") == "In Progress") ? "miniscoreboard.xml" : "linescore.xml";
if ($P.home === $P.myTeam) {
$P.weAre = {
team : $P.home,
runs : g.getAttribute("home_team_runs"),
link : g.getAttribute("home_preview_link"),
div : g.getAttribute("home_division")
};
$P.theyAre = {
team : $P.away,
runs : g.getAttribute("away_team_runs"),
link : g.getAttribute("away_preview_link"),
div : g.getAttribute("away_division")
};
} else if ($P.away === $P.myTeam) {
$P.weAre = {
team : $P.away,
runs : g.getAttribute("away_team_runs"),
link : g.getAttribute("away_preview_link"),
div : g.getAttribute("away_division")
};
$P.theyAre = {
team : $P.home,
runs : g.getAttribute("home_team_runs"),
link : g.getAttribute("home_preview_link"),
div : g.getAttribute("home_division")
};
}
}
gamesToday = null;
var link, req;
if (!$P.weAre) {
// No game today, so let's try tomorrow
req = new XMLHttpRequest();
req.open("GET", $P.url + "?output=" + $P.getDate(true) + "/" + file, true);
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200 || req.status == 304) {
$P.handle(req.responseXML);
} else {
console.log("Error: " + req.status);
$P.init();
}
}
};
req.send(null);
} else {
// Yay, there's a home game today! Ok, now let's get the attributes we need and parse the directory
// Saving my link to the namespace
$P.linescore = $P.getDate() + "/gid_" + dir + "/" + file;
req = new XMLHttpRequest();
req.open("GET", $P.url + "?output=" + $P.linescore, true);
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200 || req.status == 304) {
$P.game(req.responseXML);
} else {
console.log("Error: " + req.status);
$P.init();
}
}
};
req.send(null);
}
$P.games = games;
},
reload : function() {
if ($P.trigger) {
clearTimeout($P.trigger);
}
$P.showLoadingIcon();
req = new XMLHttpRequest();
req.open("GET", $P.url + "?output=" + $P.linescore, true);
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200 || req.status == 304) {
$P.game(req.responseXML);
} else {
console.log("Error: " + req.status + " on $P.reload()");
$P.reload();
}
}
};
req.send(null);
},
game : function(e) {
var parent = $("Pickleview");
var loader = $("LoadScores");
// I need a game ID, Dont'cha know...
var g = $E("game", e),
batter = $E("batter", e),
pitcher = $E("pitcher", e);
var PickleSplit;
// Splitting out the cities of multi-city teams...
if (($P.away.indexOf("LA ") !== -1) || ($P.away.indexOf("NY ") !== -1) || $P.away.indexOf("Chi ") !== -1) {
PickleSplit = $P.away.split(" ");
$P.away = PickleSplit[1];
if (PickleSplit[2]) {
$P.away += " " + PickleSplit[2];
}
if ($P.myTeam.indexOf($P.away) !== -1) {
$P.myTeam = $P.away;
}
}
if (($P.home.indexOf("LA ") !== -1) || ($P.home.indexOf("NY ") !== -1) || $P.home.indexOf("Chi ") !== -1) {
PickleSplit = $P.home.split(" ");
$P.home = PickleSplit[1];
if (PickleSplit[2]) {
$P.home += " " + PickleSplit[2];
}
if ($P.myTeam.indexOf($P.home) !== -1) {
$P.myTeam = $P.home;
}
}
if (!g || !g.attributes) {
// something went screwy, reload...
throw "No game found";
//$P.trigger = setTimeout($P.reload, 3000);
}
gameInfo = {
away : {
city : $P.away,
abbr : g.getAttribute("away_name_abbrev"),
file : g.getAttribute("away_file_code")
},
home : {
city : $P.home,
abbr : g.getAttribute("home_name_abbrev"),
file : g.getAttribute("home_file_code")
},
time : g.getAttribute("time"),
ampm : g.getAttribute("ampm"),
day : g.getAttribute("day"),
ind : g.getAttribute("ind"),
wrapup : g.getAttribute("wrapup_link")
};
var checkStatus = $E("game_status", g);
if (gameInfo.status && ((checkStatus && checkStatus.getAttribute("status")) || g.getAttribute("status"))) {
var currStatus = checkStatus.getAttribute("status") || g.getAttribute("status");
if (gameInfo.status != currStatus) {
gameInfo.status = currStatus;
$P.init($P.myTeam);
}
}
//var checkStatus = $E("game_status", g);
if (checkStatus) {
gameInfo.status = checkStatus.getAttribute("status");
gameInfo.inning = checkStatus.getAttribute("inning");
gameInfo.outs = checkStatus.getAttribute("o") || 0;
gameInfo.top_inning = checkStatus.getAttribute("top_inning");
}
if (g.getAttribute("status")) {
gameInfo.status = g.getAttribute("status");
gameInfo.inning = g.getAttribute("inning");
gameInfo.outs = g.getAttribute("outs");
gameInfo.top_inning = g.getAttribute("top_inning");
}
if ($E("runner_on_1b", g)) {
gameInfo._1b = $E("runner_on_1b", g).getAttribute("first") + " " + $E("runner_on_1b", g).getAttribute("last");
gameInfo._2b = $E("runner_on_2b", g).getAttribute("first") + " " + $E("runner_on_2b", g).getAttribute("last");
gameInfo._3b = $E("runner_on_3b", g).getAttribute("first") + " " + $E("runner_on_3b", g).getAttribute("last");
gameInfo.pbp = $E("in_game", g).getAttribute("last_pbp");
}
if ($E("r", g)) {
gameInfo.away.runs = $E("r", g).getAttribute("away");
gameInfo.away.hits = $E("h", g).getAttribute("away");
gameInfo.away.erro = $E("e", g).getAttribute("away");
gameInfo.home.runs = $E("r", g).getAttribute("home");
gameInfo.home.hits = $E("h", g).getAttribute("home");
gameInfo.home.erro = $E("e", g).getAttribute("home");
}
if (g.getAttribute("home_team_runs")) {
gameInfo.away.runs = g.getAttribute("away_team_runs");
gameInfo.away.hits = g.getAttribute("away_team_hits");
gameInfo.away.erro = g.getAttribute("away_team_errors");
gameInfo.home.runs = g.getAttribute("home_team_runs");
gameInfo.home.hits = g.getAttribute("home_team_hits");
gameInfo.home.erro = g.getAttribute("home_team_errors");
}
if ($E("post_game", g)) {
gameInfo.away.wins = $E("away_team", g).getAttribute("wins");
gameInfo.away.loss = $E("away_team", g).getAttribute("losses");
gameInfo.home.wins = $E("home_team", g).getAttribute("wins");
gameInfo.home.loss = $E("home_team", g).getAttribute("losses");
}
if (g.getAttribute("home_win")) {
gameInfo.away.wins = g.getAttribute("away_win");
gameInfo.away.loss = g.getAttribute("away_loss");
gameInfo.home.wins = g.getAttribute("home_win");
gameInfo.home.loss = g.getAttribute("home_loss");
}
if (gameInfo.time) {
// Setting to Local time...
var newTime = $P.setLocalTime(gameInfo.time, gameInfo.ampm);
}
if (gameInfo.day) {
// Uncapping, recapping...
gameInfo.day = gameInfo.day.toLowerCase();
gameInfo.day = gameInfo.day.replace(gameInfo.day.charAt(0), gameInfo.day.charAt(0).toUpperCase());
var cal = new Date(),
day = cal.getDay(),
days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var hour = parseInt(newTime.split(":")[0]),
daynight = ((hour > 5) && (hour < 8)) ? "Tonight" : "Today";
switch (gameInfo.day) {
case days[day] :
gameInfo.day = daynight;
break;
case days[day + 1] :
gameInfo.day = "Tomorrow";
break;
}
}
var suffix = $P.buildSuffix(gameInfo.inning);
gameInfo.inning = gameInfo.inning + suffix;
var b_first, b_last, p_first, p_last,
batting = (gameInfo.top_inning == "Y") ? gameInfo.away.file : gameInfo.home.file,
pitching = (gameInfo.top_inning == "Y") ? gameInfo.home.file : gameInfo.away.file;
if (pitcher && pitcher.getAttribute("first") && pitcher.getAttribute("first") != "null") {
p_first = "first";
p_last = "last";
} else if ($E("current_pitcher", g)) {
pitcher = $E("current_pitcher", g);
p_first = "first_name";
p_last = "last_name";
}
if (batter && batter.getAttribute("first") && batter.getAttribute("first") != "null") {
b_first = "first";
b_last = "last";
} else if ($E("current_batter", g)) {
batter = $E("current_batter", g);
b_first = "first_name";
b_last = "last_name";
}
if (p_first && (p_first != "null")) {
gameInfo.pitcher = pitcher.getAttribute(p_first).charAt(0) + ". " + pitcher.getAttribute( p_last);
$P.renderBase("p", pitching);
$P.updateBase("p", pitcher.getAttribute(p_first) + " " + pitcher.getAttribute(p_last), pitcher.getAttribute("id"));
} else {
gameInfo.pitcher = null;
$P.unRenderBase("p");
}
if (p_first && (p_first != "null") && b_first && (b_first != "null")) {
gameInfo.batter = batter.getAttribute(b_first).charAt(0) + ". " + batter.getAttribute(b_last);
$P.renderBase("h", batting);
$P.updateBase("h", batter.getAttribute(b_first) + " " + batter.getAttribute(b_last), batter.getAttribute("id"));
} else {
gameInfo.batter = null;
$P.unRenderBase("h");
}
if ((gameInfo.away.wins) && (gameInfo.home.wins)) {
gameInfo.away.record = "(" + gameInfo.away.wins + "-" + gameInfo.away.loss + ")";
gameInfo.home.record = "(" + gameInfo.home.wins + "-" + gameInfo.home.loss + ")";
} else {
gameInfo.away.record = "";
gameInfo.home.record = "";
}
if (!$("CurrentGame")) {
var dummy = document.createElement("div");
dummy.innerHTML = '| R | H | E | |
|---|---|---|---|
' + $("a_team").innerHTML;
$("h_team").innerHTML = '
' + $("h_team").innerHTML;
}
if (!$P.changeValueIs) {
if (a_runs.nodeValue !== gameInfo.away.runs) {
a_runs.nodeValue = gameInfo.away.runs;
//$P.fade.up($("CurrentGame"));
}
if (h_runs.nodeValue !== gameInfo.home.runs) {
h_runs.nodeValue = gameInfo.home.runs;
//$P.fade.up($("CurrentGame"));
}
$P.changeValueIs = true;
} else {
if (a_runs.nodeValue !== gameInfo.away.runs) {
$P.changeValue.init($("a_runs"), gameInfo.away.runs);
}
if (h_runs.nodeValue !== gameInfo.home.runs) {
$P.changeValue.init($("h_runs"), gameInfo.home.runs);
}
}
a_hits.nodeValue = (a_hits.nodeValue == gameInfo.away.hits) ? a_hits.nodeValue : gameInfo.away.hits;
h_hits.nodeValue = (h_hits.nodeValue == gameInfo.home.hits) ? h_hits.nodeValue : gameInfo.home.hits;
a_erro.nodeValue = (a_erro.nodeValue == gameInfo.away.erro) ? a_erro.nodeValue : gameInfo.away.erro;
h_erro.nodeValue = (h_erro.nodeValue == gameInfo.home.erro) ? h_erro.nodeValue : gameInfo.home.erro;
var half = (gameInfo.top_inning === "Y") ? "Top" : "Bot",
outxt = gameInfo.outs + " Out";
// If 3 outs, we're in between innings
if ((gameInfo.outs === "3") && (gameInfo.top_inning === "Y")) {
half = "Mid";
outxt = " ";
}
tb.nodeValue = (tb.nodeValue === half) ? tb.nodeValue : half;
inning.nodeValue = (inning.nodeValue === gameInfo.inning) ? inning.nodeValue : gameInfo.inning;
if (!outs) {
$("td2").appendChild(document.createTextNode(" "));
outs = $("td2").firstChild;
}
if (outs && outs.nodeName && outs.nodeName.toLowerCase() === "img") {
outs.parentNode.className = "";
outs.parentNode.replaceChild(document.createTextNode(outxt), outs);
} else {
outs.nodeValue = (outs.nodeValue === outxt) ? outs.nodeValue : outxt;
}
if (gameInfo.status === "In Progress") {
var a = gameInfo._1b,
b = gameInfo._2b,
c = gameInfo._3b,
p = gameInfo.pitcher,
h = gameInfo.batter;
if (a == " " || a === null) {
a = gameInfo._1b = null;
}
if (b == " " || b === null) {
b = gameInfo._2b = null;
}
if (c == " " || c === null) {
c = gameInfo._3b = null;
}
if (a) {
$P.renderBase("a", batting);
$P.updateBase("a", gameInfo._1b, $E("runner_on_1b", g).getAttribute("id"));
} else {
$P.unRenderBase("a");
}
if (b) {
$P.renderBase("b", batting);
$P.updateBase("b", gameInfo._2b, $E("runner_on_2b", g).getAttribute("id"));
} else {
$P.unRenderBase("b");
}
if (c) {
$P.renderBase("c", batting);
$P.updateBase("c", gameInfo._3b, $E("runner_on_3b", g).getAttribute("id"));
} else {
$P.unRenderBase("c");
}
var current_pitcher = info1,
current_batter = info2;
current_pitcher.nodeValue = "P: ";
current_batter.nodeValue = "AB: ";
if (gameInfo.pitcher) {
current_pitcher.nodeValue += gameInfo.pitcher;
}
if (gameInfo.batter) {
current_batter.nodeValue += gameInfo.batter;
}
if ($P.PlayByPlay) {
if (gameInfo.pbp && ($P.PlayByPlay !== gameInfo.pbp)) {
$P.PlayByPlay = gameInfo.pbp;
$P.AnnouncePlay();
}
}
$P.node.show([info2.parentNode]);
// There's a game going on. Update B/S/O every 12 seconds.
// On event change, reload
if (!$P.PlayXML.trigger) {
$P.PlayXML("plays.xml");
}
} else {
var warmup = info1;
warmup.nodeValue = "Warm ups...";
$P.node.hide([info2.parentNode]);
// No game yet. Reload normally.
$P.trigger = setTimeout($P.reload, 30000);
}
$P.node.hide([link]);
$P.node.show([tb.parentNode, inning.parentNode, bases, moreInfo]);
// If still here, remove loading icon
if ($("loadScores")) {
$("loadScores").parentNode.className = $("loadScores").parentNode.className.replace("active", "");
$("loadScores").parentNode.removeChild($("loadScores"));
}
break;
case "Preview" :
case "Pre-Game" :
$("CurrentGame").className += " inactive";
gameInfo.away.pchr = {
name : $E("away_probable_pitcher", g).getAttribute("last_name"),
wins : $E("away_probable_pitcher", g).getAttribute("wins"),
loss : $E("away_probable_pitcher", g).getAttribute("losses")
};
gameInfo.home.pchr = {
name : $E("home_probable_pitcher", g).getAttribute("last_name"),
wins : $E("home_probable_pitcher", g).getAttribute("wins"),
loss : $E("home_probable_pitcher", g).getAttribute("losses")
};
link.setAttribute("href", "http://mlb.mlb.com" + $P.weAre.link);
link.firstChild.nodeValue = gameInfo.day + " @ " + newTime;
var ap_stats = info1,
hp_stats = info2;
ap_stats.nodeValue = gameInfo.away.pchr.name + " (" + gameInfo.away.pchr.wins + "-" + gameInfo.away.pchr.loss + ")";
info3.nodeValue = "vs.";
hp_stats.nodeValue = gameInfo.home.pchr.name + " (" + gameInfo.home.pchr.wins + "-" + gameInfo.home.pchr.loss + ")";
tb.nodeValue = "Next: ";
$P.node.hide([inning.parentNode, bases]);
$P.node.show([link, moreInfo]);
// If still here, remove loading icon
if ($("loadScores")) {
$("loadScores").parentNode.className = $("loadScores").parentNode.className.replace("active", "");
$("loadScores").parentNode.removeChild($("loadScores"));
}
if (gameInfo.status === "Pre-Game") {
// If this is not disabled, set timeout
$P.trigger = setTimeout($P.reload, 30000);
}
if ($P.PlayXML.trigger) {
clearTimeout($P.PlayXML.trigger);
$P.PlayXML.trigger = null;
}
// No game in progress, do the Twitter thang...
//startChatUpdates();
break;
}
return;
},
PlayXML : function() {
if ($P.PlayXML.trigger) {
clearTimeout($P.PlayXML.trigger);
}
$P.showLoadingIcon();
var url = $P.linescore,
suffix = new RegExp("linescore.xml").test(url) ? "linescore.xml" : "miniscoreboard.xml";
url = url.replace(suffix, "plays.xml");
req = new XMLHttpRequest();
req.open("GET", $P.url + "?output=" + url, true);
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200 || req.status == 304) {
$P.atbat(req.responseXML);
} else {
console.log("Error: " + req.status + " on $P.PlayXML()");
$P.PlayXML();
}
}
};
req.send(null);
},
atbat : function(e) {
if ($P.PlayXML.trigger) {
clearTimeout($P.PlayXML.trigger);
}
var g = $E("game", e), i;
if (!$P.atbat.status) {
$P.atbat.status = g.getAttribute("event");
}
var batter = $E("batter", e),
pitcher = $E("pitcher", e);
if (batter && pitcher) {
batter = $E("batter", e).getAttribute("boxname");
pitcher = $E("pitcher", e).getAttribute("boxname");
if (batter && batter.indexOf(" ") !== -1) {
batter = batter.split(" ")[1];
}
if (pitcher && pitcher.indexOf(" ") !== -1) {
pitcher = pitcher.split(" ")[1];
}
if (($("info2") && $("info2").firstChild && !(new RegExp(batter).test($("info2").firstChild.nodeValue))) || ($("info1") && $("info1").firstChild && !(new RegExp(pitcher).test($("info1").firstChild.nodeValue)))) {
$P.PlayXML.trigger = setTimeout($P.PlayXML, 12000);
$P.reload();
return;
}
}
if ($P.atbat.status != g.getAttribute("event")) {
// Event has changed, update game
$P.atbat.status = g.getAttribute("event");
$P.PlayXML.trigger = setTimeout($P.PlayXML, 12000);
$P.reload();
return;
}
var bases = [$("a"), $("b"), $("c")];
for (i = 0, j = bases.length; i < j; i++) {
if (bases[i].getElementsByTagName("img")[0] && !bases[i].getElementsByTagName("img")[1]) {
$P.PlayXML.trigger = setTimeout($P.PlayXML, 12000);
$P.reload();
break;
}
}
var _b = g.getAttribute("b") || 0,
_s = g.getAttribute("s") || 0,
_o = g.getAttribute("o") || 0,
strikes = $("strikes").firstChild,
balls = $("balls").firstChild,
outs = $("td2").firstChild,
pitch = $("pitch").firstChild;
var ab = $E("atbat", g);
if (ab) {
var _p = $E("atbat", g).getElementsByTagName("*"),
nodes = [];
if (typeof _p !== "undefined") {
for (i = 0, j = _p.length; i < j; i++) {
nodes.push(_p[i]);
}
_p = nodes[nodes.length - 1];
if ((typeof _p !== "undefined") && (typeof _p.getAttribute("des") !== "undefined")) {
_p = _p.getAttribute("des");
} else {
_p = null;
}
}
}
if ((_b != "undefined") && (balls.nodeValue != _b)) {
balls.nodeValue = _b;
}
if ((_s != "undefined") && (strikes.nodeValue != _s)) {
strikes.nodeValue = _s;
}
if (_p && (pitch.nodeValue != _p)) {
pitch.nodeValue = _p;
} else if (!_p) {
pitch.nodeValue = " ";
}
var outxt = _o + " Out";
if (!$P.previousOut) {
$P.previousOut = outxt;
}
if (!outs) {
$("td2").appendChild(document.createTextNode(" "));
outs = $("td2").firstChild;
}
if (outs && outs.nodeName && outs.nodeName.toLowerCase() === "img") {
outs.parentNode.className = "";
outs.parentNode.replaceChild(document.createTextNode(outxt), outs);
} else {
outs.nodeValue = (outs.nodeValue === outxt) ? outs.nodeValue : outxt;
}
if ($P.previousOut != outxt) {
fade($("td2"));
$P.previousOut = outxt;
}
$P.PlayXML.trigger = setTimeout($P.PlayXML, 12000);
},
AnnouncePlay : function() {
var play = gameInfo.pbp,
target = $("PickleTalk"),
item = document.createElement("li");
item.style.display = "none";
item.className = "ump";
item.innerHTML = '' + play + '
pickleump'; // see how many chats there are if (target.childNodes.length >= $P.maxChats) { // remove the last one var SlideOut = target.childNodes[target.childNodes.length -1]; target.removeChild(target.childNodes[target.childNodes.length -1]); } target.insertBefore(item, target.firstChild); if (item) { $P.AnnouncePlay.timeout = setTimeout(function() { new $P.slide(item); //clearTimeout($P.AnnouncePlay.timeout); $P.AnnouncePlay.timeout = null; }, 2500); } }, slide : function(e) { var that = $P.slide; if (that.timeout) { clearTimeout(that.timeout); } if (!that.doOnce && e.parentNode) { if (e.nextSibling && e.nextSibling.style.height != "auto") { e.nextSibling.style.height = "auto"; } var wrap = document.createElement("div"); wrap.style.overflow = wrap.style.visibility = "hidden"; e.style.display = "block"; wrap.style.height = e.offsetHeight + "px"; e.parentNode.insertBefore(wrap, e); wrap.appendChild(e); e = wrap; if (!that.alt) { $E("li", e).className += " alt"; that.alt = true; } else { that.alt = false; } if (typeof LandscapeMode.logTweets != "undefined" || LandscapeMode.logTweets === 0) { var tweet = parseInt(LandscapeMode.logTweets); if (typeof tweet == "number") { if (tweet >= 5) { tweet = tweet.toString() + "+"; } else { tweet = tweet + 1; } if (LandscapeMode.logTweets != "NaN") { LandscapeMode.logTweets = tweet; if (!(/\bactive\b/.test($("tweet").parentNode.parentNode.className))) { $("tweet").parentNode.parentNode.className += " active"; } $("tweet").firstChild.nodeValue = LandscapeMode.logTweets; } } } that.doOnce = true; } that.oHeight = that.oHeight || e.style.height.split("px")[0]; that.cHeight = that.cHeight || 0; if (that.cHeight < that.oHeight) { if (e.style.visibility == "hidden") { e.style.visibility = "visible"; } e.style.height = that.cHeight + "px"; that.cHeight = (that.cHeight + 15); that.timeout = setTimeout(function() { that(e); }, 40); } else { e.style.height = "auto"; e.style.overflow = "visible"; that.doOnce = that.oHeight = that.cHeight = false; if (that.timeout) { clearTimeout(that.timeout); } } }, renderBase : function(e, file) { var target = $(e), img = $("img_" + e); if (!img) { img = document.createElement("img"); img.setAttribute("src", "/img/team/" + file + ".png"); img.setAttribute("alt", ""); img.setAttribute("id", "img_" + e); img.style.visibility = "hidden"; target.appendChild(img); $P.trickle(img); } else if (!new RegExp(file).test(img.getAttribute("src"))) { img.setAttribute("src", "/img/team/" + file + ".png"); } }, unRenderBase : function(e) { var target = $(e), img = $("img_" + e), o = $(e + "Overlay"); if (img) { img.parentNode.removeChild(img); } $P.clearBase(e); }, updateBase : function(base, player, id) { var target = $(base + "Overlay"); if (!target) { target = document.createElement("div"); target.setAttribute("id", base + "Overlay"); target.className = "overlay"; target.style.display = "none"; $(base).appendChild(target); } var peeps = target.childNodes[1]; if (!peeps || (peeps && peeps.nodeValue.indexOf(player) == -1)) { while (target.hasChildNodes()) { target.removeChild(target.lastChild); } var img = document.createElement("img"); img.className = "mugshot"; img.setAttribute("src", "/php/imgsize.php?w=21&h=25&img=" + "http://gdx.mlb.com/images/gameday/mugshots/mlb/" + id + ".jpg"); target.appendChild(img); target.appendChild(document.createTextNode(player)); target.appendChild(document.createElement("span")); target.parentNode.onclick = function() { var id = this.getAttribute("id"); $P.updateBase.show(this, $(id + "Overlay")); }; } $P.updateBase.show = function(base, target) { if ($P.overlay.timeout) { clearTimeout($P.overlay.timeout); } var divs = $("bases").getElementsByTagName("div"); for (var i = 0, j = divs.length; i < j; i++) { if ((divs[i].className == "overlay") && divs[i] !== target) { if (divs[i].style.display != "none") { divs[i].style.display = "none"; } } } target.style.display = (target.style.display == "block") ? "none" : "block"; }; }, clearBase : function(base) { var target = $(base + "Overlay"); if (target) { $(base).removeChild(target); } }, buildSuffix : function(e) { var suffix; switch (e) { case "1" : suffix = "st"; break; case "2" : suffix = "nd"; break; case "3" : suffix = "rd"; break; default : suffix = "th"; break; } return suffix; }, createGamesToday : function(e) { var games = e, target = $("PickleGames"); target = $E("ul", target) || document.createElement("ul"); if (games.length > 15) { target.setAttribute("id", "moreThan15"); } else if (games.length < 15) { target.setAttribute("id", "lessThan15"); } for (var i = 0; i < games.length; i++) { var g = games[i], home = g.getAttribute("home_team_city").substring(0, 3), away = g.getAttribute("away_team_city").substring(0, 3); switch (g.getAttribute("home_team_city")) { case "San Francisco" : case "San Diego" : case "Tampa Bay" : case "Kansas City" : home = g.getAttribute("home_team_city").split(" "); home = home[0].charAt(0) + home[1].charAt(0); break; case "LA Dodgers" : case "LA Angels" : case "NY Yankees" : case "NY Mets" : home = g.getAttribute("home_team_city").split(" "); home = home[0] + home[1].charAt(0); break; case "Chi White Sox": home = "CWS"; break; case "St. Louis" : home = "StL"; break; } switch (g.getAttribute("away_team_city")) { case "San Francisco" : case "San Diego" : case "Tampa Bay" : case "Kansas City" : away = g.getAttribute("away_team_city").split(" "); away = away[0].charAt(0) + away[1].charAt(0); break; case "LA Dodgers" : case "LA Angels" : case "NY Yankees" : case "NY Mets" : away = g.getAttribute("away_team_city").split(" "); away = away[0] + away[1].charAt(0); break; case "Chi White Sox": away = "CWS"; break; case "St. Louis" : away = "StL"; break; } var time; switch (g.getAttribute("status")) { case "Preview" : case "Pre-Game" : time = $P.setLocalTime(g.getAttribute("time"), g.getAttribute("ampm")); break; case "In Progress" : case "Warmup" : time = ((g.getAttribute("top_inning") == "Y") ? "Top" : "Bot") + " " + g.getAttribute("inning") + $P.buildSuffix(g.getAttribute("inning")); break; case "Game Over" : time = "Final"; break; case "Delayed Start" : time = "Delayed"; break; default : time = g.getAttribute("status"); break; } var exists = $(g.getAttribute("gameday_link")); if (exists) { var parent = $(g.getAttribute("gameday_link")), home_team = parent.getElementsByTagName("dt")[1].firstChild, away_team = $E("dt", parent).firstChild, home_runs = parent.getElementsByTagName("dd")[1].firstChild, away_runs = $E("dd", parent).firstChild, time_span = $E("span", parent).firstChild; if (home_runs && home_runs.nodeValue !== g.getAttribute("home_team_runs")) { home_runs.nodeValue = g.getAttribute("home_team_runs"); } if (away_runs && away_runs.nodeValue !== g.getAttribute("away_team_runs")) { away_runs.nodeValue = g.getAttribute("away_team_runs"); } if (time_span && time_span.nodeValue !== time) { time_span.nodeValue = time; } } else { var li = document.createElement("li"); li.setAttribute("id", g.getAttribute("gameday_link")); li.setAttribute("rel", g.getAttribute("home_team_city")); li.addEventListener("click", function() { var link = this.getAttribute("id"); if (link.charAt(link.length - 1) == "2") { newLink = link.substring(0, link.length - 2) + "_1"; $(newLink).addEventListener("click", function() { $P.showFirstOfTwo = true; $P.PickYourTeam.Go(this.getAttribute("rel"), true, "second"); }, false); } $P.PickYourTeam.Go(this.getAttribute("rel"), true); window.scrollTo(0, 1); }, false); li.innerHTML = '