// JavaScript Document
addLoadEvent(loadUserGames);

//var dtUrl = "http://www.mlb.com/mlb/fantasy/homepage/games.do";
//var dtUrl = "/mlb/fantasy/homepage/games.do";
var dtUrl = "/mlb/fantasy/teamcenter/games.do"
var am = new AjaxManager();

function loadUserGames() {
	am.request(dtUrl, onDataReady);
}

function onDataReady(amResp) {
	if(amResp.success) {
		var xmlObj = toJSObject(amResp.xml);
		//Build all the bts nodes
		if(xmlObj.nickname) {
			var nick = xmlObj.nickname;
			if(!isEmpty(nick)) {
				var login = getObject("login");
				if(login != null) {
					login.className = "leftNavGame";
					login.innerHTML = "<b>Welcome " + nick + "</b>! ";
					var notLink = new SimpleLink("http://www.mlb.com/enterworkflow.do?flowId=fantasy.fantasy.fantasylogout", "Not " + nick + "?");
					login.innerHTML += notLink.toHTML();
					login = null;
				}
			}
		}
		if(xmlObj.bts != null) {
			if (xmlObj.bts.length) {
				var btsLen = xmlObj.bts.length - 1;
				do {				
					buildBTS(xmlObj.bts[btsLen]);			
				} while(btsLen--);
			} else {
				buildBTS(xmlObj.bts);
			}
		}
		//Build wsfb
		if(xmlObj.wsfb) {
			buildWSFB(xmlObj.wsfb);	
		}
	}
}

function buildBTS(obj) {
	var placeHldr = getObject(obj.bts);
	if(placeHldr != null) {
		var tbl = new SimpleTable(1, 3);
			tbl.width = "100%";
		var hdr = tbl.Cell(0, 0);
			hdr.setAttribute("class", "header");
			switch(obj.bts) {
				case "hit":	hdr.value = new SimpleLink("http://www.mlb.com/mlb/fantasy/bts/y2009/", "MLB.com Beat the Streak").toHTML();				
					setOnClick(placeHldr, "http://www.mlb.com/enterworkflow.do?flowId=fantasy.bts.btsloginregister&forwardUrl=/mlb/fantasy/bts/y2009/");
					break;
				case "hr":	hdr.value = new SimpleLink("http://www.mlb.com/mlb/fantasy/btshr/y2009/", "Home Run Beat the Streak").toHTML();
					setOnClick(placeHldr, "http://www.mlb.com/enterworkflow.do?flowId=fantasy.bts.btsloginregister&forwardUrl=/mlb/fantasy/btshr/y2009/");
					break;
				case "st":	hdr.value = new SimpleLink("http://www.mlb.com/mlb/fantasy/survivor/y2009/", "MLB.com Survivor").toHTML();
					setOnClick(placeHldr, "http://www.mlb.com/enterworkflow.do?flowId=fantasy.bts.btsloginregister&forwardUrl=/mlb/fantasy/survivor/y2009/");
					break;			
			}
			var cell1, cell2;
			var aLen = obj._attributes.length - 1
			//do {
				//if(obj._attributes[aLen] != "bts") {
					cell1 = tbl.Cell(0, 1);
					cell1.value = formatName("todays_pick") + ": " + obj.todays_pick;
					cell2 = tbl.Cell(0, 2);
					cell2.value = formatName("current_streak") + ": " + obj.current_streak;
				//}
			//} while(aLen--);
		placeHldr.innerHTML = tbl.toHTML();
		placeHldr = null;
	}
			
	function formatName(attr) {
		var out = "";
		switch(attr) {
			case "highest_streak":
				out = "Highest Streak";
				break;
			case "todays_pick":
				out = "Today's Pick";
				break;
			case "current_streak":
				out = "Current Streak";
				break;
			case "yesterdays_pick":
				out = "Yesterday's Pick";
				break;
		}
		return out;
	}
}

function buildWSFB(nodes) {
	var placeHldr = getObject("wsfb");
	if(placeHldr != null) {
		var rLen = (nodes.team.length)?nodes.team.length:2;
		var tbl = new SimpleTable(2, rLen+1);
			tbl.width = "100%";
			if(rLen < 3) {
				placeHldr.setAttribute("valign", "top");
				placeHldr.style.height = "52px";
			}
		var hdr = tbl.Cell(0, 0);
			hdr.setAttribute("nowrap", "nowrap");
			hdr.setAttribute("colspan", "2");	
			hdr.value = new SimpleLink("http://www.mlb.com/mlb/fantasy/wsfb/index.jsp", "MLB.com Fantasy Baseball").toHTML();
		var cell1, cell2;
		if(nodes.team.length) {
			var nLen = rLen - 1;			
			var p = 0;
			do {
				cell1 = tbl.Cell(0, p+1);				
				cell2 = tbl.Cell(1, p+1);
				
				cell1.value = nodes.team[p].name;				
				cell2.setAttribute("width", "40");
				cell2.setAttribute("align", "right");
				cell2.value = "(" + nodes.team[p].wins + "-" + nodes.team[p].losses + ")";
			} while(p++ < nLen);
		} else {
			cell1 = tbl.Cell(0, 1);				
			cell2 = tbl.Cell(1, 1);
			
			cell1.value = nodes.team.name;
			cell2.setAttribute("width", "40");
			cell2.setAttribute("align", "right");
			cell2.value = "(" + nodes.team.wins + "-" + nodes.team.losses + ")";
		}		
		placeHldr.innerHTML = tbl.toHTML();
		setOnClick(placeHldr, "http://www.mlb.com/mlb/fantasy/wsfb/index.jsp");
		placeHldr = null;
	}
}

function setOnClick(obj, url) {	
	obj.setAttribute("link", url);	
}

function clickABox(div) {
	var url = div.getAttribute("link");
	window.open(url, "_self");	
}
