// Test to see if data exists
function dataExists() {
	if (typeof(standings_rs_ale) == "undefined" || typeof(standings_rs_alc) == "undefined" || typeof(standings_rs_alw) == "undefined" ||
		typeof(standings_rs_nle) == "undefined" || typeof(standings_rs_nlc) == "undefined" || typeof(standings_rs_nlw) == "undefined") { 
		standings_rs_ale = new Array();
		standings_rs_alc = new Array();
		standings_rs_alw = new Array();
		standings_rs_nle = new Array();
		standings_rs_nlc = new Array();
		standings_rs_nlw = new Array();
	}
}

// Standings object sets data as ON/OFF
function Standings() {
	this.div = 1;
	this.code = 1;
	this.team = 1;
	this.w = 1;
	this.l = 1;
	this.pct = 1;
	this.gb = 1;
	this.elim = 1;   
	this.wcg = 1;   
	this.wce = 0;   
	this.last10 = 1;
	this.strk = 1;
	this.vsE = 0;
	this.vsC = 0;
	this.vsW = 0;    
	this.interleague = 0;
	this.home = 1;
	this.road = 1;   
	this.vsR = 0;
	this.vsL = 0;
	this.xtr = 0;
	this.onerun = 0;
	this.rs = 0;
	this.ra = 0;
	this.xwl = 0;
	this.lastg = 1;
	this.nextg = 1;
	this.wrap = 0;
	this.pre = 0;
	this.gameid = 0;
	this.status = 0;
	this.favTeam = "";
}
var _stand = new Standings(); // standings object

function buildRows(data) { // build rows and ID each one with a division label, so we can manipulate data
	var dataRow = ""; 
	if (data.length != 0) {
		for (var x=0; x<data.length; x++) {
			dataRow += "\n<tr valign='top' class='data' id='" + data[x].code + "_data'>\n";  
			dataRow += "<td class='' id='" + data[x].div + "_team_" + x + "'></td>\n"; // Team
			dataRow += "<td class='' id='" + data[x].div + "_w_" + x + "'></td>\n"; // W
			dataRow += "<td class='' id='" + data[x].div + "_l_" + x + "'></td>\n"; // L
			dataRow += "<td class='' id='" + data[x].div + "_pct_" + x + "'></td>\n"; // Pct
			dataRow += "<td class='' id='" + data[x].div + "_gb_" + x + "'></td>\n"; // GB
			dataRow += "<td class='' id='" + data[x].div + "_elim_" + x + "'></td>\n"; // Elim
			dataRow += "<td class='' id='" + data[x].div + "_wcg_" + x + "'></td>\n"; // WC GB
			dataRow += "<td class='' id='" + data[x].div + "_wce_" + x + "'></td>\n"; // WC Elim
			dataRow += "<td class='' id='" + data[x].div + "_last10_" + x + "'></td>\n"; // Last 10
			dataRow += "<td class='' id='" + data[x].div + "_strk_" + x + "'></td>\n"; // Streak
			dataRow += "<td class='' id='" + data[x].div + "_vsE_" + x + "'></td>\n"; // vs. E
			dataRow += "<td class='' id='" + data[x].div + "_vsC_" + x + "'></td>\n"; // vs. C
			dataRow += "<td class='' id='" + data[x].div + "_vsW_" + x + "'></td>\n"; // vs. W
			dataRow += "<td class='' id='" + data[x].div + "_interleague_" + x + "'></td>\n"; // Interleague
			dataRow += "<td class='' id='" + data[x].div + "_home_" + x + "'></td>\n"; // Home
			dataRow += "<td class='' id='" + data[x].div + "_road_" + x + "'></td>\n"; // Road
			dataRow += "<td class='' id='" + data[x].div + "_vsR_" + x + "'></td>\n"; // vs. R
			dataRow += "<td class='' id='" + data[x].div + "_vsL_" + x + "'></td>\n"; // vs. L
			dataRow += "<td class='' id='" + data[x].div + "_xtr_" + x + "'></td>\n"; // xtra
			dataRow += "<td class='' id='" + data[x].div + "_onerun_" + x + "'></td>\n"; // 1-Run
			dataRow += "<td class='' id='" + data[x].div + "_rs_" + x + "'></td>\n"; // Runs Scored
			dataRow += "<td class='' id='" + data[x].div + "_ra_" + x + "'></td>\n"; // Runs Against
			dataRow += "<td class='' id='" + data[x].div + "_xwl_" + x + "'></td>\n"; // xtr W-L
			dataRow += "<td class='' id='" + data[x].div + "_lastg_" + x + "'></td>\n"; // Last
			dataRow += "<td class='' id='" + data[x].div + "_nextg_" + x + "'></td>\n"; // Next
			dataRow += "</tr>\n"; 
		}
	}
	else {
		dataRow += "<tr><td colspan='25' class='errorMsg'>Data is currently unavailable.</td></tr>";
	}
	document.write(dataRow);
}

// standingsCustom function takes data from data file and matches it with html table
// that was built with BuildRows
function standingsCustom(data) {
	for (var x=0; x<data.length; x++) {
		var counter = x;
		var currentRow = document.getElementById(data[x].div).rows[counter];
		counter++;		
		// prepend clinch symbol if it exists
		var clinchSymbol = (data[x].clinch=="#"||data[x].clinch=="&"||data[x].clinch=="@"||data[x].clinch=="w"||data[x].clinch=="x"||data[x].clinch=="y"||data[x].clinch=="z") ? data[x].clinch+"&ndash;":"&nbsp;";		
		currentRow.cells[0].innerHTML = clinchSymbol + "<a href='http://" + getTeamDomain(data[x].code) + ".mlb.com/index.jsp?c_id=" + data[x].code + "' title='Go to the " + data[x].league_sensitive_team_name + " homepage'>" + data[x].team + "</a>"; 
		currentRow.cells[1].innerHTML = data[x].w;
		currentRow.cells[2].innerHTML = data[x].l;
		currentRow.cells[3].innerHTML = data[x].pct;
		currentRow.cells[4].innerHTML = data[x].gb;
		currentRow.cells[5].innerHTML = data[x].elim;
		currentRow.cells[6].innerHTML = (data[x].games_back_wildcard == null) ? '-' : data[x].games_back_wildcard;
		currentRow.cells[7].innerHTML = (data[x].elimination_number_wildcard == null) ? '-' : data[x].elimination_number_wildcard;
		currentRow.cells[8].innerHTML = data[x].last10;
		currentRow.cells[9].innerHTML = data[x].strk;
		currentRow.cells[10].innerHTML = data[x].vsE;
		currentRow.cells[11].innerHTML = data[x].vsC;
		currentRow.cells[12].innerHTML = data[x].vsW;
		currentRow.cells[13].innerHTML = data[x].interleague;
		currentRow.cells[14].innerHTML = data[x].home;
		currentRow.cells[15].innerHTML = data[x].road;
		currentRow.cells[16].innerHTML = data[x].vsR;
		currentRow.cells[17].innerHTML = data[x].vsL;
		currentRow.cells[18].innerHTML = data[x].xtr;
		currentRow.cells[19].innerHTML = data[x].onerun;
		currentRow.cells[20].innerHTML = data[x].rs;
		currentRow.cells[21].innerHTML = data[x].ra;
		currentRow.cells[22].innerHTML = data[x].xwl;
	
		if (data[x].lastg != null) {
			if (data[x].wrap != null) currentRow.cells[23].innerHTML = "<a href=\"" + data[x].wrap + "\">" + data[x].lastg + "</a>";
			else currentRow.cells[23].innerHTML = data[x].lastg;
		}
		else currentRow.cells[23].innerHTML = "-";

		if (data[x].nextg != null) {
			if (data[x].status.indexOf("I") != -1) currentRow.cells[24].innerHTML = "<a href='javascript:launchGameday({gid:\"" + data[x].gameid + "\"});'>" + data[x].nextg + "</a> <img src='/images/icons/live.gif' width='12' height='12' alt='LIVE' border='0' align='absmiddle'/>";
			else if (/*(data[x].status.indexOf("P") != -1) && */ data[x].pre != null) currentRow.cells[24].innerHTML = "<a href=\"" + data[x].pre + "\">" + data[x].nextg + "</a>"; 
			else currentRow.cells[24].innerHTML = data[x].nextg; 
		}
		else currentRow.cells[24].innerHTML = "-";
	} 
}
  
// changeData function refers to the Standings object (s) and turns props ON/OFF
// this function runs when page loads
function changeData(val) {
	var cookieStr = "";
	var standingsArr = document.getElementById("standingsContainer").getElementsByTagName("td"); 
	var standingsLength = standingsArr.length;
		
	if (val == "off") { // default 'off' setting on page load
		for (var j in _stand) { // loop thru props
			cookieStr += j + "=" + _stand[j] + "&";
			if (_stand[j] == 0) { // if any of the props are false, hide them
				for (var x=0; x<standingsLength; x++) if (standingsArr[x].id.indexOf(j) != -1) standingsArr[x].className = "permOff"; 
			}  
			if (_stand.favTeam != "") favTeam(_stand.favTeam);
		}
		cookieStr = cookieStr.substring(0,cookieStr.length - 1); 
		SetCookie("customstandings", cookieStr, 365); // set cookie for 365 days
		document.getElementById("editBar").style.display = "none";
		document.getElementById("editPulldown").style.display = "none";
		document.getElementById("customToggle").innerHTML = "<a href=\"javascript:changeData('on');\">Customize this page</a>"; 
	}
	if (val == "noCookie") { // user has no cookie
		for (var j in _stand) { // set default columns until user sets a cookie
			if (_stand[j] == 1) {
				if (document.getElementById(j+"_edit") != null) document.getElementById(j+"_edit").className = "editBarLinkOn"; 
			} else {
				if (document.getElementById(j+"_edit") != null) document.getElementById(j+"_edit").className = "editBarLinkOff"; 
				for (var x=0; x<standingsLength; x++) if (standingsArr[x].id.indexOf(j) != -1) standingsArr[x].className = "permOff"; 
			}
		}
		document.getElementById("editBar").style.display = "block";
		document.getElementById("editPulldown").style.display = "block";
		document.getElementById("customToggle").innerHTML = "<a href=\"javascript:changeData('off');\">Save changes &amp; exit</a>";
	}
	else if (val == "on") {
		document.getElementById("editLimit").innerHTML = "";
		document.getElementById("customToggle").innerHTML = "<a href=\"javascript:changeData('off');\">Save changes &amp; exit</a>";
		document.getElementById("editBar").style.display = "block";
		document.getElementById("editPulldown").style.display = "block";
		for (var j in _stand) { // if any of the columns are on, change link color so user knows it is on
			if (_stand[j] == 1) {
				if (document.getElementById(j+"_edit") != null) document.getElementById(j+"_edit").className = "editBarLinkOn"; 
			}
			else {
				if (document.getElementById(j+"_edit") != null) document.getElementById(j+"_edit").className = "editBarLinkOff"; 
			}
		}
	}
	
}

// changeDataTemp function turns ON/OFF data while user is in edit mode
// it refers to Standings object(s) and uses CSS to display/hide content temporarily
function changeDataTemp(content) {
	var standingsArr = document.getElementById("standingsContainer").getElementsByTagName("td");
	var standingsLength = standingsArr.length;
	
	for (var x=0; x<standingsLength; x++) {
		if (standingsArr[x].id.indexOf(content) != -1) { // if id contains 'content'
			if (standingsArr[x].className == "tempOff") {
				standingsArr[x].className = "";
				_stand[content] = 1;
			} else if (standingsArr[x].className == "permOff") {
				standingsArr[x].className = "";
				_stand[content] = 1;
			} else {
				standingsArr[x].className = "tempOff";
				_stand[content] = 0;
			}
		}
	}
	var counter = 0;
	
	for (var j in _stand) { // if any of the columns are on, change link color so user knows it is on
		if (_stand[j] == 1) {
			if (document.getElementById(j+"_edit") != null) {
				document.getElementById(j+"_edit").className = "editBarLinkOn";
				counter++;
			}
		} else {
			if (document.getElementById(j+"_edit") != null) document.getElementById(j+"_edit").className = "editBarLinkOff"; 
		}
	}
	var remaining = 11 - counter;
	
	if (remaining < 0) {
		document.getElementById("editLimit").innerHTML = "<b>ERROR:</b> You can only display <b><u>11</u></b> columns.";
		document.getElementById(content+"_edit").className = "editBarLinkOff";
		for (var x=0; x<standingsLength; x++) {
			if (standingsArr[x].id.indexOf(content) != -1) standingsArr[x].className = "permOff"; 
		}
		_stand[content] = 0;
	}
	else if (remaining == 0) document.getElementById("editLimit").innerHTML = "&nbsp;";
	else if (remaining == 1) document.getElementById("editLimit").innerHTML = "You can display <u><b>" + remaining + "</b></u> more column.";
	else document.getElementById("editLimit").innerHTML = "You can display <u><b>" + remaining + "</b></u> more columns.";
}	

// get cookie from user
function setStandingsFromCookie() {
	var s_cookie = GetCookie('customstandings');
	if (s_cookie != null) {
		var params = s_cookie.split("&");	
		for (var i=0; i<params.length; i++) _stand[params[i].substring(0,params[i].indexOf("="))] = params[i].slice(params[i].indexOf("=")+1);
		changeData("off");
	} else changeData("noCookie");
}

// shade team when user clicks on table row <tr>
function shadeTeam(id) {
	var standingsArr = document.getElementById("standingsContainer").getElementsByTagName("tr");
	for (var x=0; x<standingsArr.length; x++) {
		if (standingsArr[x].id.indexOf("_data") != -1) {
			if (standingsArr[x].className == "data shadeTeamOn") standingsArr[x].className = "data shadeTeamOff";
		}
	}
	if (document.getElementById(id + "_data").className != "data favTeamOn") document.getElementById(id + "_data").className = "data shadeTeamOn"; 
}

// shade user's favorite team by defining the favTeam property in the Standings object (s)
function favTeam(val) {
	_stand.favTeam = val; // set favTeam prop

	var standingsArr = document.getElementById("standingsContainer").getElementsByTagName("tr");
	var standingsLength = standingsArr.length;
	
	for (var x=0; x<standingsLength; x++) { // loop through all the tr's
		if (standingsArr[x].className == "data favTeamOn") { // if favTeam shading is on already, turn it off
			standingsArr[x].className = "data favTeamOff";
		}
		if (standingsArr[x].id == val + "_data") { // if user selected favTeam matches an id

			if (standingsArr[x].className.indexOf("data") != -1) { // if it's not on, turn on favTeam shading
				standingsArr[x].className = "data favTeamOn"; 
			}
		}
		else if (val == "" && standingsArr[x].id.indexOf("_data") != -1) { // if user resets, turn off favTeam shading
			if (standingsArr[x].className == "data favTeamOn") standingsArr[x].className = "data favTeamOff"; // if favTeam shading is on already, turn it off
		}
	}
}

function buildTitleRows(setTheId) {	
	var writeDiv = ""; 
		if (setTheId.substring(2,3) == "e") writeDiv = "East"; 
		if (setTheId.substring(2,3) == "c") writeDiv = "Central"; 
		if (setTheId.substring(2,3) == "w") writeDiv = "West"; 
	var titleRow = "";
	titleRow += "\n<tr valign='top' class='title' id='title_" + setTheId + "'>\n";
	titleRow += "<td width='120' class='' id='" + setTheId + "_team_title'><b>" + writeDiv + "</b></td>\n";
	titleRow += "<td class='' id='" + setTheId + "_w_title'>W</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_l_title'>L</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_pct_title' id='pctT'>PCT</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_gb_title'>GB</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_elim_title'>E#</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_wcg_title'>WCGB</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_wce_title'>WCE#</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_last10_title'>L10</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_strk_title'>STRK</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_vsE_title'>vs E</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_vsC_title'>vs C</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_vsW_title'>vs W</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_interleague_title'>INT</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_home_title'>HOME</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_road_title'>ROAD</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_vsR_title'>vs R</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_vsL_title'>vs L</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_xtr_title'>XTRA</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_onerun_title'>1-RUN</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_rs_title'>RS</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_ra_title'>RA</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_xwl_title'>X W-L</td>\n";
	titleRow += "<td width='120' class='' id='" + setTheId + "_lastg_title'>LAST GAME</td>\n";
	titleRow += "<td class='' id='" + setTheId + "_nextg_title'>NEXT GAME</td>\n";
	titleRow += "\n</tr>\n";
	document.write(titleRow);
} 

// the init() function calls the populate functions.  in this case there are 6.
// for wild card page, there will be 2.
function init() {
	if (dataExists) {
		standingsCustom(standings_rs_ale);
		standingsCustom(standings_rs_alc);
		standingsCustom(standings_rs_alw);
		standingsCustom(standings_rs_nle);
		standingsCustom(standings_rs_nlc);
		standingsCustom(standings_rs_nlw);
		setStandingsFromCookie();
	}
}

function r(f) {
	var gm = f.m.options[f.m.options.selectedIndex].value;
	if (gm.length == 1) gm = "0" + gm;
	var gd = f.d.options[f.d.options.selectedIndex].value;
	if (gd.length == 1) gd = "0" + gd;
	var gy = f.y.options[f.y.options.selectedIndex].value;
	var destination = "/mlb/standings/index.jsp?ymd=" + gy + gm + gd;
	document.location.href = destination;
}

