function reloadPage(nextStatus, layout) {
    var statusMap = { linescore: "PREGAME", postgame: "LIVE" };
	if (gameStatusCode == statusMap[nextStatus]) {
		var locn = location.href;
		if (locn.indexOf("&forceReload=")>-1) {
			locn = locn.substring(0, locn.indexOf("&forceReload="));
		}
		location.href = locn + "&forceReload=" + new Date().getTime();
	} else {
        var redirPage = nextStatus + ".jsp";
        if ((layout) && (layout > 2)) { 
            redirPage = nextStatus + "_" + layout + ".jsp"
        }
        location.href = redirPage + "?gid=" + gid + "&t=" + new Date().getTime() + "&sid=" + sid + "&cid=" + sid;
	}
}

function firstInitialLastName(fullName) {
	if (fullName.indexOf(" ")==-1) {
		return ""+fullName
	}
	return fullName.substring(0, 1) + "." + fullName.substring(fullName.indexOf(" "), fullName.length);
}

function getPostgamePitchers(style) {

    var html = "";
    try { 
        if (pitcherWinName.length>0) {
            pitcherWinDisplayName = firstInitialLastName(pitcherWinName);
            html += "W: <a style=\"color:#ffffff;\" target=\"_parent\" href=\"/milb/stats/stats.jsp?n="+escape(pitcherWinName)+"&pos=P&sid=" + sid + "&t=p_pbp&did=" + sid + "&pid=" + pitcherWinID + "\">" + pitcherWinDisplayName + "</a> ";
            html += pitcherWinRecord + ", " + pitcherWinERA + "; ";
        }
    
        if (pitcherLossName.length>0) {
            pitcherLossDisplayName = firstInitialLastName(pitcherLossName);
            if (style == "vertical") { html += "<br />"; }
            html += "L: <a style=\"color:#ffffff;\" target=\"_parent\" href=\"/milb/stats/stats.jsp?n="+escape(pitcherLossDisplayName)+"&pos=P&sid=" + sid + "&t=p_pbp&did=" + sid + "&pid=" + pitcherLossID + "\">" + pitcherLossDisplayName + "</a> ";
            html += pitcherLossRecord + ", " + pitcherLossERA + "&nbsp;";
        }
    
        if (pitcherSaveName.length>0) {
            pitcherSaveDisplayName = firstInitialLastName(pitcherSaveName);
            if (style == "vertical") { html += "<br />"; }
            html += "SV: <a style=\"color:#ffffff;\" target=\"_parent\" href=\"/milb/stats/stats.jsp?n="+escape(pitcherSaveDisplayName)+"&pos=P&sid=" + sid + "&t=p_pbp&did=" + sid + "&pid=" + pitcherSaveID + "\">" + pitcherSaveDisplayName + "</a> ";
            html += pitcherSaveRecord + ", " + pitcherSaveERA + "&nbsp;";
        }
        html += "<br/>";

        if (false && homeR.length>0) {
            // if the game was not completed, homeR (and visitingR) will be ""; otherwise:
            html += "&nbsp;" + homers;
        }

    } catch(e) { }

    return html;

}

function getPrevNextLink(layout, mode, gid, sid, cid, nextGameId, lastGameId, date) {
    var html = "";
    var page = mode + ".jsp";

    nextGameId = nextGameId.replace(/\//g,"_").replace(/-/g,"_");
    lastGameId = lastGameId.replace(/\//g,"_").replace(/-/g,"_");

    if ((layout) && (layout > 2)) {
        page = mode + "_" + layout + ".jsp";
    }

    if (nextGameId == gid) {
        prevNextGameId = lastGameId;
        html += ' | <a href="/components/game_status/' + page + '?gid=' + prevNextGameId + '&sid=' + sid + '&cid=' + cid + '" class="prevGame">previous game</a>';
    } else if (lastGameId == gid) {
        if (date != (lastGameId.substring(0,4) + lastGameId.substring(5,7) + lastGameId.substring(8,10))) { 
            html += ' | <a href="/components/game_status/redirector.jsp?sid=' + sid + '" class="prevGame">next game</a>';
        }
    }

    return html;
}

function getGameLinks(wrapAvailable, boxAvailable, logAvailable, gamedayAvailable, gid, sid) {
    var html = "";
    var lnkCreated = false;

    try {
        if (boxAvailable) { 
            if (lnkCreated) { html += "<span class=\"divider\"> | </span>"; }
            html += "<a target=\"_parent\" href=\"/milb/stats/stats.jsp?t=g_box&gid=" + gid + "&did=" + sid + "&sid=" + sid + "\" class=\"wrapBoxLogFont\">Box</a>";
            lnkCreated = true;
        }
    } catch(e) { }

    try {
        if (logAvailable) { 
            if (lnkCreated) { html += "<span class=\"divider\"> | </span>"; }
            html += "<a target=\"_parent\" href=\"/milb/stats/stats.jsp?t=g_log&gid=" + gid + "&did=" + sid + "&sid=" + sid + "\" class=\"wrapBoxLogFont\">Recap</a>";
            lnkCreated = true;
        }
    } catch(e) { }

    try {
        if (gamedayAvailable) { 
            if (lnkCreated) { html += "<span class=\"divider\"> | </span>"; }
            html += "<a target=\"_parent\" href=\"javascript:launchGameday('" + gid + "')\">Gameday</a>";    
            lnkCreated = true;
        }
    } catch(e) { }

    try {
        if (wrapAvailable) {
            if (lnkCreated) { html += "<span class=\"divider\"> | </span>"; }
            html += "<a target=\"_parent\" href=\"/milb/stats/stats.jsp?t=g_wra&gid=" + gid + "&did=" + sid + "&sid=" + sid + "\" class=\"wrapBoxLogFont\">Game Story</a>";
            lnkCreated = true;
        }
    } catch(e) { }

    return html;
}

function getStartTime(gameStartHour, gameStartMinute, gameStartTimeLocal, gameStartTimezone) {
    gameStartHour      = parseInt(trimString(gameStartHour), 10);
    gameStartTimeLocal = (gameStartTimeLocal == "true");

    // if we have a double header, no time
    if ((gameStartHour == 3) && (parseInt(trimString(gameStartMinute), 10) == 33)) {
        return "Game 2";
    }

    var startTime = "";
    var isPM = true;
    if (gameStartHour < 12) {
        isPM = false;
    }

    if (gameStartHour>12) {
        startTime += (gameStartHour-12);
    } else {
        startTime += gameStartHour;
    }

    if ((gameStartTimezone == null) || (gameStartTimezone == undefined)) { 
        gameStartTimezone = (gameStartTimeLocal ? "local" : "ET");
    }
    
    startTime += ":" + gameStartMinute + " " + (isPM?"PM":"AM") + " " + gameStartTimezone;
    startTime = "Game starts at " + startTime;

    return startTime;
}

function writeStatus() {
    // default text if we dont find any value
	var s = "PREGAME";
    var statusColor = "#2D8C07";
    try { 
        // look for game status and upcase it for display
        if (gameStatusMessage) {
            s = gameStatusMessage;
            if (gameStatusMessage.indexOf("Delayed") > -1) { 
                var statusColor = "#CC0000";
            }
            s = s.toUpperCase();
        } 
    } catch (e) { }

    s = "<div style=\"white-space: nowrap; color: #FFFFFF; background-color: " + statusColor + "\"><nobr>&nbsp;" + s + "&nbsp;</nobr></div>";

    // insert it into the proper table cell
	if (document.getElementById) {
		document.getElementById("gStat").innerHTML = s;
	} else if (document.all) {
		document.all["gStat"].innerHTML = s;
	}
} 
