if ((!begin) && (begin != 0)) {    // position in the schedule array to start showing data from
    begin = 3;   // 3 = 3 days back, today, 3 days forward
}                // 6 = today and 6 days forward
                 // set in clubsite_sub_index_X.jsp from schedule_start property

var today = new Date();
today.setHours(0,0,0);
var start;
var start = new Date(today.valueOf() - (begin * (1000 * 60 * 60 * 24)));  // start the schedule based on the begin value

// create a table cell for a game occuring in the past
function createPastDay(day) {
    var dayStrings = ['SUN','MON','TUE','WED','THU','FRI','SAT'];
    var html = "<td class=\"past\">";

    // if the game is flagged "TBD" its a double header and ID it as just "Game 2"
    if (day.isTBD) {
        html += "Game 2";
    } else { 
        html += dayStrings[day.date.getDay()];  
    }
    if (day.team.code) {
        try { 
            html += "<div class=\"vs\">";
            if (day.homeaway == "away") { 
                html += " @ ";
            } else {
                html += " vs ";
            }
            html += "<a href=\"/clubs/index.jsp?cid=" + day.team.id + "\" title=\"" + day.team.name + "\">" + day.team.code + "</a> ";
            html += "</div>";

            // if the game is flagged "PO" for postponed, then just write PPD,
            // otherwise attempt to write the result with a link to the box score.
            if (day.game.status == "PO") { 
                html += "<span title=\"Game Postponed\">PPD</span>";
            } 
             else if (day.game.status == "S") { 
                html += "<span title=\"Suspended\">SUSP</span>";
            }
			else if (day.result) {
                html += "<a href=\"/milb/stats/stats.jsp?sid=" + sid + "&t=g_box&gid=" + day.gameId.replace(/\//g,"_").replace(/-/g,"_") + "\" title=\"View Boxscore\">"; 
                if (day.result.charAt(0) == "L") { 
                    html += "<span class=\"loss\">";
                } else if (day.result.charAt(0) == "W") { 
                    html += "<span class=\"win\">";
                } else {
                    html += "<span class=\"tie\">";
                }
                html += day.result;
                html += "</span></a>";
            }
        } catch (e) { }
    }
    html += "</td>";
    return html;
}

// create a table cell for a game occuring today or in the future
function createFutureDay(day, isToday) {
    var dayStrings = ['SUN','MON','TUE','WED','THU','FRI','SAT'];
    var hour = 0;
    var minute = 0;
    var ampm = "pm";
    var dayType = "future";
    if (isToday) { 
        dayType = "today";
    }
    var html = "<td class=\"" + dayType + "\">";
    if (day.isTBD) {
        html += "Game 2";
    } else { 
        html += dayStrings[day.date.getDay()];  
    }
    if (day.team && day.game && day.team.code && day.game.time) {
        try {

            html = "<td class=\"" + dayType + " " + day.homeaway + "\">"; // reset the class if we find info
            if (day.isTBD) {
                html += "&nbsp;";
            } else { 
                html += dayStrings[day.date.getDay()];  
            }

            if (day.homeaway == "away") { 
                html += "<div class=\"vs\"> @ ";
            } else {
                html += "<div class=\"vs\"> vs ";
            }
            html += "<a href=\"/clubs/index.jsp?cid=" + day.team.id + "\" title=\"" + day.team.name + "\">" + day.team.code + "</a></div>";

            if (day.ticketurl && day.homeaway) { 
                html += "<a href=\"javascript:void(openTIXXWindow('" + day.ticketurl + "', '" + day.homeaway + "'));\" title=\"Purchase Tickets\"><img src=\"/images/schedule/icon_ticket.gif\" border=\"0\" height=\"13\" width=\"13\" ></a>&nbsp;";
            }
			
			if (day.promourl && day.homeaway) { 
                html += "&nbsp;<a href=\"" + day.promourl + "\"><img src=\"/images/schedule/icon_promotions.gif\" border=\"0\" height=\"13\" width=\"13\" ></a>";
            }
			
			if ((day.ticketurl || day.promourl) && day.homeaway) { 
                html += "<br />";
            }
			
		   if (day.game.status != "PO" && day.game.status != "S") { 
 			 hour = day.game.time.getHours();
             if (hour > 12) {
                 hour = hour - 12;
                 ampm = "p";
             } else if (hour == 12) {
                 ampm = "p";
             } else { 
                 ampm = "a";
             }

             minute = day.game.time.getMinutes();
             if (minute < 10) { minute = "0" + minute; } 

             if (day.isTBD) {
                 html += "Game 2";
             } else { 
                 html += hour + ":" + minute + ampm; 
            }
           }
		   else if (day.game.status == "S") { 
                html += "<span title=\"Suspended\">SUSP</span>";
           }
		   else {
                html += "<span title=\"Game Postponed\">PPD</span>";			
		  }
        } catch (e) { }
    }
    html += "</td>";
    return html;
}

function create7daySchedule(schedule, today) {
    var html = "<table class=\"sevenDaySched\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr class=\"schedDate\">";

    // build the date headers for the schedule table
    for (var k=0; k<7; k++) {
        html += "<td>";
        if (schedule.days[k]) {
            if (!schedule.days[k].isTBD) { // dont write date headers for the second of a double header
                try { 
                    var dateString = (schedule.days[k].date.getMonth() +1) + "/" + (schedule.days[k].date.getDate());
                    html += dateString;
                } catch (e) { }
            } else { 
                html += "&nbsp;"
            }
        }
        html += "</td>";
    }
    
    html += "</tr><tr class=\"schedInfo\">";

    // build the game content for each day
    for (var i=0; i<7; i++) {
        var obj = schedule.days[i];
        if (obj) {
            if (obj.game.time.valueOf() < today.valueOf()) {
                html += createPastDay(obj);
            } else if (obj.game.time.valueOf() > (today.valueOf() + (1000 * 60 * 60 * 24))) {
                html += createFutureDay(obj, false);
            } else if (obj.game.time.valueOf() >= today.valueOf()) {
                html += createFutureDay(obj, true);
            }
        }
    }

    html += "</tr></table>";
    
    return html;
}

// build a schedule data object from the raw data, beginning at a
// customizable offset from the actual start of the data

function buildScheduleObject(raw, start) {
    var schedule = { days: [] };

    var localDate = new Date();
    var localOffset = localDate.getTimezoneOffset() * 60 * 1000;

    for (var l=0; l < raw.length; l++) {
        if (start.valueOf() < parseInt(raw[l][1])) {
            schedule.days[schedule.days.length] = new Object();

            var time = parseInt(raw[l][1]);
            var offset = parseInt(raw[l][2]);
            if (offset) { 
                offset = offset * 60 * 60 * 1000;
            } else { offset = 0; } 
            time = time + offset + localOffset;

            var objDay = schedule.days[(schedule.days.length-1)];

            objDay.date      = new Date();
            objDay.date.setTime(time);

            objDay.team      = { id: raw[l][4], code: raw[l][3], name: raw[l][5] };
            objDay.offset    = raw[l][2];
            objDay.result    = raw[l][6];
            objDay.ticketurl = raw[l][19];
            objDay.homeaway  = raw[l][20];
            objDay.gameId    = raw[l][21];
            objDay.promourl   = raw[l][24];
            if (raw[l][22] == '1') {
                objDay.isTBD = true;
            } else { 
                objDay.isTBD = false;
            }
            objDay.game      = { time: new Date(), status: raw[l][23] };

            objDay.game.time.setTime(time);
        }
    }
    return schedule;
}

// initialize the schedule object from the raw data
var schedule = buildScheduleObject(raw, start);

document.write(create7daySchedule(schedule, today));
