			
var ClubHome = ClubHome || {};
//Linescore Head to Head Module
bam.extend(ClubHome, {
   HTHLinescore: {
	DataFields: [//Scores	
	"home_team_name",
	"away_team_name",
	"away_team_runs",
	"home_team_runs",
	"inning",
	"outs",
	"top_inning",
	"runner_on_base_status",
	//Stadium/Teams
	"venue",
	"time", "ampm", "time_zone",	
	"away_time", "away_time_zone", "away_ampm",
	"home_time", "home_time_zone", "home_ampm",	
	"home_team_id",
	"away_team_id",
	"away_file_code",
	"home_file_code",
	"home_code",
	"away_code",
	//Player Names (Combined Call)
	"shortPlayerNames",		
	//Standings
	"home_win",
	"home_loss",
	"away_win",
	"away_loss",
	//Links	
	"activeLinks",
	//State
	"gamestateGrouping",
	//gameday flag
	"gameday_sw",
	//granular game status
	"status",
	//postseason tv link
	["individualLink", "postseason_tv"]	
	],
	Linescore: new bam.linescore("ClubHome.HTHLinescore.OnGameLoad"),
	OnGameLoad: function() {
		setTimeout(function() {
			ClubHome.HTHLinescore.Linescore.RetrieveData(ClubHome.HTHLinescore.DataFields, "ClubHome.HTHLinescore.OnDataReady");
		}, 10);
		setTimeout(function() {
			ClubHome.HTHLinescore.Linescore.UpdateRequest(ClubHome.HTHLinescore.DataFields, "ClubHome.HTHLinescore.OnDataReady");			
		}, 10);
	},
	
	
	OnDataReady: function(json) {		
		// initialize homepage videos. This function is in /scripts/homepage/y2009/club.js
		if(ClubHome.HTHLinescore.isInit)ClubHome.initRow1Videos(json.gamestate, json.inning, json.away_code.toLowerCase(), json.home_code.toLowerCase());		
		var _homeWins = (parseInt(json.away_team_runs) < parseInt(json.home_team_runs))?true:false;
		var _isTop = (bam.string.equalsIgnoreCase(json.top_inning, "y"))?true:false;
		//$("#lsState").html(json.gamestate);
		$("#lsState").html("");
		$("#lsLocation").html(json.venue);
		//Teams
		var _awayTeam = json.away_team_name + " " + ((json.away_team_runs==null)?"0":json.away_team_runs);
		var _homeTeam = json.home_team_name + " " + ((json.home_team_runs==null)?"0":json.home_team_runs);
		//$("#lsTeams").html(_awayTeam + " @ " +  _homeTeam);
		$("#lsTeamsLft").html(_awayTeam);
		$("#lsTeamsRgt").html(_homeTeam);
		//$("#lsAwayLogo").attr("src", "/" + json.away_team_name.toLowerCase() + ClubHome.HTHLinescore.BasesLogoPath + "matchup.png");
		//$("#lsHomeLogo").attr("src", "/" + json.home_team_name.toLowerCase() + ClubHome.HTHLinescore.BasesLogoPath + "matchup.png");
		$("#lsAwayLogo").attr("src", "/" + json.away_file_code.toLowerCase() + ClubHome.HTHLinescore.BasesLogoPath + "matchup.png");
		$("#lsHomeLogo").attr("src", "/" + json.home_file_code.toLowerCase() + ClubHome.HTHLinescore.BasesLogoPath + "matchup.png");		
		if(json.away_team_name!="NL" && json.away_team_name!="AL" && !bam.validation.isEmpty(json.away_win)){
			$("#lsAwayScore").html("("+((json.away_win==null)?"0":json.away_win) + " - " + ((json.away_loss==null)?"0":json.away_loss)+")");
		}
		if(json.home_team_name!="NL" && json.home_team_name!="AL" && !bam.validation.isEmpty(json.home_win)){
			$("#lsHomeScore").html("("+((json.home_win==null)?"0":json.home_win) + " - " + ((json.home_loss==null)?"0":json.home_loss)+")");
		}		
		//Load Mugshots
		switch(json.gamestate) {
			case "preview":						
				//Inning
				$("#lsInning, #linescore_outs").html("");
				if(!!json.away_probable_pitcher_id && !bam.validation.isEmpty(json.away_probable_pitcher_id)) {
					$("#lsPitcher").attr("src", ClubHome.HTHLinescore.MugshotsPath + json.away_probable_pitcher_id + ".jpg");
					$("#lsPitcherName").html(json.away_probable_pitcher);				
				} else {
					$("#lsPitcher").attr("src", ClubHome.HTHLinescore.DefaultMugshot);
					$("#lsPitcherName").html("TBD");
				}
				if(!!json.home_probable_pitcher_id && !bam.validation.isEmpty(json.home_probable_pitcher_id)) {
					$("#lsBatter").attr("src", ClubHome.HTHLinescore.MugshotsPath + json.home_probable_pitcher_id + ".jpg");
					$("#lsBatterName").html(json.home_probable_pitcher);				
				} else {
					$("#lsBatter").attr("src", ClubHome.HTHLinescore.DefaultMugshot);
					$("#lsBatterName").html("TBD");
				}				
				//$("#lsState").html(json.gamestate + "<br/>" + (json.time + " " + json.ampm + " " + json.time_zone));
				
				var timeStr = json.home_time + " " + json.home_ampm + " " + json.home_time_zone;				
				if(club===json.away_file_code){
					timeStr = timeStr = json.away_time + " " + json.away_ampm + " " + json.away_time_zone;
				}				
				$("#lsState").html(timeStr);			
				
				$("#lsLeftTag").html("Probable Pitcher");
				$("#lsRightTag").html("Probable Pitcher");
				$("#lsRunnerBase").attr("src", ClubHome.HTHLinescore.BasesLogoPath + "runner_base_grey.gif");
				//Trigger Preview event
				if(typeof(ClubHome.HTHLinescore.OnPreview) == "function") {
					try{ClubHome.HTHLinescore.OnPreview(json);}catch(e){}
				}
				break;
			case "final":
				if(_homeWins) {
					//$("#lsTeams").html(_homeTeam + ", " +  _awayTeam);
					$("#lsTeamsLft").html(_homeTeam);
					$("#lsTeamsRgt").html(_awayTeam);
				} else {
					//$("#lsTeams").html(_awayTeam + ", " +  _homeTeam);
					$("#lsTeamsLft").html(_awayTeam);
					$("#lsTeamsRgt").html(_homeTeam);
				}
				//Inning
				$("#lsInning, #linescore_outs").html("");	
				if(_homeWins) {
					$("#lsRightTag").html("Winning Pitcher");
					$("#lsLeftTag").html("Losing Pitcher");
					//Pictures Now
					if(!!json.winning_pitcher_id) {
						$("#lsBatter").attr("src", ClubHome.HTHLinescore.MugshotsPath + json.winning_pitcher_id + ".jpg");
						$("#lsBatterName").html(json.winning_pitcher);
					} else {
						$("#lsBatter").attr("src", ClubHome.HTHLinescore.DefaultMugshot);
						$("#lsBatterName").html("TBD");
					}
					if(!!json.losing_pitcher_id) {
						$("#lsPitcher").attr("src", ClubHome.HTHLinescore.MugshotsPath + json.losing_pitcher_id + ".jpg");
						$("#lsPitcherName").html(json.losing_pitcher);						
					} else {
						$("#lsPitcher").attr("src", ClubHome.HTHLinescore.DefaultMugshot);
						$("#lsPitcherName").html("TBD");
					}										
				} else {
					$("#lsLeftTag").html("Winning Pitcher");
					$("#lsRightTag").html("Losing Pitcher");
					//Pictures Now
					if(!!json.winning_pitcher_id) {
						$("#lsPitcher").attr("src", ClubHome.HTHLinescore.MugshotsPath + json.winning_pitcher_id + ".jpg");
						$("#lsPitcherName").html(json.winning_pitcher);						
					} else {
						$("#lsPitcher").attr("src", ClubHome.HTHLinescore.DefaultMugshot);
						$("#lsPitcherName").html("TBD");
					}
					if(!!json.losing_pitcher_id) {
						$("#lsBatter").attr("src", ClubHome.HTHLinescore.MugshotsPath + json.losing_pitcher_id + ".jpg");
						$("#lsBatterName").html(json.losing_pitcher);
					} else {
						$("#lsBatter").attr("src", ClubHome.HTHLinescore.DefaultMugshot);
						$("#lsBatterName").html("TBD");
					}										
				}
				$("#lsRunnerBase").attr("src", ClubHome.HTHLinescore.BasesLogoPath + "runner_base_grey.gif");
				//Trigger Final event
				if(typeof(ClubHome.HTHLinescore.OnFinal) == "function") {
					try{ClubHome.HTHLinescore.OnFinal(json);}catch(e){}
				}
				break;						
			default:	//Live					
				//Inning
				$("#lsInning").html(ClubHome.HTHLinescore.FormatInning(json.inning, _isTop));
				
				var pitcherImg = (json.current_pitcher_id == null)?ClubHome.HTHLinescore.DefaultMugshot:ClubHome.HTHLinescore.MugshotsPath + json.current_pitcher_id + ".jpg";
				var batterImg = (json.current_batter_id == null)?ClubHome.HTHLinescore.DefaultMugshot:ClubHome.HTHLinescore.MugshotsPath + json.current_batter_id + ".jpg";
				
				if(_isTop) {
					$("#lsPitcher").attr("src", batterImg);
					$("#lsBatter").attr("src", pitcherImg);
					$("#lsPitcherName").html(json.current_batter);
					$("#lsBatterName").html(json.current_pitcher);
					$("#lsLeftTag").html("Now Batting");
					$("#lsRightTag").html("Now Pitching");
				} else {
					$("#lsPitcher").attr("src", pitcherImg);
					$("#lsBatter").attr("src", batterImg);
					$("#lsPitcherName").html(json.current_pitcher);
					$("#lsBatterName").html(json.current_batter);
					$("#lsLeftTag").html("Now Pitching");
					$("#lsRightTag").html("Now Batting");
				}				
				if(gID) {
					$("#lsRunnerBase").css({cursor:"pointer"}).unbind('click').click(function() {
						try {
							launchGameday({gid: gID, mode: "gameday"});						
						} catch(e) {}
					});
				}
				$("#lsRunnerBase").attr("src", ClubHome.HTHLinescore.BasesLogoPath + "runner_base_" + json.runner_on_base_status + ".gif");
				
				if(json.outs != null) {					
					var nOuts = (json.outs==="1") ? "1 out" : json.outs + " outs";					
					$("#linescore_outs").text(nOuts).show();					
				}			
				
				//Trigger Live event
				if(typeof(ClubHome.HTHLinescore.OnLive) == "function") {
					try{ClubHome.HTHLinescore.OnLive(json);}catch(e){}
				}
		}
		//Image load errors
		$("#lsPitcher").error(function() { this.src = ClubHome.HTHLinescore.DefaultMugshot; });
		$("#lsBatter").error(function() { this.src = ClubHome.HTHLinescore.DefaultMugshot; });
	},
	MugshotsPath: "/images/gameday/mugshots/mlb/",
	DefaultMugshot: "/mlb/fantasy/wsfb/images/logos/62x75_generic.jpg",
	BasesLogoPath: "/images/homepage/y2008/",
	FormatInning: function(ing, _isTop) {
		var _out = "";
		if(bam.object.typeOf(ing) == "string") {
			var _inning = parseInt(ing);					
			switch(_inning) {
				case 1: _out = _inning + "st"; break;
				case 2: _out = _inning + "nd"; break;
				case 3: _out = _inning + "rd"; break;
				default: _out = _inning + "th";
			}
		}
		return (_isTop)?"<span class=\"lsArrows\">&#x25b2;</span>" + _out: "<span class=\"lsArrows\">&#x25bc;</span>" + _out;
	},
	OnPreview: null,
	OnLive: null,
	OnFinal: null,
	isTiebreaker:false,
	isInit: true			
}
});

$(document).ready(function() {	
	try {
	(function(){
		bam.imports(bam.object);		
		ClubHome.HTHLinescore.Linescore.Width = 232;
		ClubHome.HTHLinescore.Linescore.Height = 50;
		ClubHome.HTHLinescore.Linescore.Scale = false;
		ClubHome.HTHLinescore.Linescore.LinescoreFLV = "/flash/mediawall/y2009/nobase_linescore.swf"
		//ClubHome.HTHLinescore.Linescore.DefaultGID = "2008_03_31_kcamlb_detmlb_1";
		ClubHome.HTHLinescore.Linescore.DefaultGID = gID;
		
		/**			
		// url params for setting data source. this is for testing and does not work on prod.
		**/
		var theLoc = document.location;
		var theDomain = theLoc.hostname.toLowerCase();
		var isProd = ( (theDomain.indexOf("beta.") == -1) && (theDomain.indexOf("dev-") == -1) && (theDomain.indexOf("qa.") == -1) && (theDomain.indexOf("qa2.") == -1) ) ? true : false;
		if(!isProd && theLoc.search.indexOf("env=qa")!=-1) ClubHome.HTHLinescore.Linescore.BaseURL = "gd.qa.mlb.com";		
				
		ClubHome.HTHLinescore.Linescore.Init("lsLinescore");
		//Trigers On Preview State
		ClubHome.HTHLinescore.OnPreview = function(json) {
			$("#lsLinks").attr("class", "lsStatePreview mediaLinkContainer"); //mediaLinkContainer class is used for mp4 click origin tracking
			//Put links			
			$("#lsLinks").html("");
			
			// postseason 
			var isPostseason=(json.postseason_tv_link!=null && json.postseason_tv_link!="") ? true:false;	
			
			$("#lsLinks").attr("class", "lsStatePreview ");
			//Put links
			
			var previewLink = (json.preview_link!=null)?json.preview_link:json.gameday_link;				
			if(json.gameday_sw!="N" && previewLink!=null) {
				$("#lsLinks").html("<a href=\"" + previewLink + "\">Preview</a>");
			}											
			
			if(json.mlbtv_link!=null && json.mlbtv_link!="")
				$("#lsLinks").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:" + json.mlbtv_link + "\">" + ((isPostseason)?"MLB.TV":"Watch") + "</a>");			
			if(isPostseason)
				$("#lsLinks").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:" + json.postseason_tv_link + "\">Postseason.TV</a>");	
				
			// display game status banner			
			var gsBanner="", gsString=json.status.toLowerCase().replace(/\s+/g,'');	
			if(json.status=="Preview" || json.status=="Pre-Game")gsString="preview"
			else if(json.status=="Delayed Start"||json.status=="Suspended")gsString+="_preview";					
			gsBanner="/images/homepage/y2009/gs_" + gsString + ".gif";							
			$("#mwContainer .gs_banner").css({ "background" : "transparent url(" + gsBanner + ") no-repeat" });				
			$("#video_corner .gs_banner").css({ "background" : "transparent url(" + "/images/homepage/y2009/gs_" + json.gamestate + "_right.gif" + ") no-repeat" });					
		};
		//Triggers On Live State
		ClubHome.HTHLinescore.OnLive = function(json) {					
			$("#lsLinks").attr("class", "lsStateLive mediaLinkContainer"); //mediaLinkContainer class is used for mp4 click origin tracking
			//Put links			
			$("#lsLinks").html("");			
			// postseason 
			var isPostseason=(json.postseason_tv_link!=null && json.postseason_tv_link!="") ? true:false;			
			if(json.mlbtv_link!=null && json.mlbtv_link!="")
				$("#lsLinks").append("<a href=\"javascript:" + json.mlbtv_link + "\">" + ((isPostseason)?"MLB.TV":"Watch") + "</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");				
			if(club == json.home_file_code){
				if(json.audio_link!=null)	
					$("#lsLinks").append("<a href=\"javascript:" + json.audio_link + "\">Listen</a>&nbsp;&nbsp;&nbsp;&nbsp;");
			}else{
				if(json.audio_link!=null)
					$("#lsLinks").append("<a href=\"javascript:" + json.audio_link + "\">Listen</a>&nbsp;&nbsp;&nbsp;&nbsp;");
			}			
			if(json.gameday_sw!="N")
				$("#lsLinks").append("<a href=\"javascript:launchGameday({gid:'" + gID + "',mode:'gameday'})\">Gameday</a>");				
			if(isPostseason)
				$("#lsLinks").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:" + json.postseason_tv_link + "\">Postseason.TV</a>");				
			
			// display game status banner
			var gsBanner="", gsString=json.status.toLowerCase().replace(/\s+/g,'');	
			if(json.status=="In Progress" || json.status=="Warmup")gsString="live"
			else if(json.status=="Delayed Start")gsString+="_live";						
			gsBanner="/images/homepage/y2009/gs_" + gsString + ".gif";							
			$("#mwContainer .gs_banner").css({ "background" : "transparent url(" + gsBanner + ") no-repeat" });				
			$("#video_corner .gs_banner").css({ "background" : "transparent url(" + "/images/homepage/y2009/gs_" + json.gamestate + "_right.gif" + ") no-repeat" });	
		};
		//Triggers On Final State
		ClubHome.HTHLinescore.OnFinal = function(json) {
			$("#lsLinks").attr("class", "lsStateFinal");						
			//Put links
			if(!!json.wrap_link)				
				$("#lsLinks").html("<a href=\"" + json.wrap_link + "\">Wrap</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href=\"/news/boxscore.jsp?gid=" + gID + "\">Box</a>");
			else
				$("#lsLinks").html("<a href=\"/news/boxscore.jsp?gid=" + gID + "\">Box</a>");
			
			// display game status banner
			var gsBanner="", gsString=json.status.toLowerCase().replace(/\s+/g,'');	
			if(json.status=="Final" || json.status=="Game Over")gsString="final"
			else if(json.status=="Suspended")gsString+="_final";					
			gsBanner="/images/homepage/y2009/gs_" + gsString + ".gif";							
			$("#mwContainer .gs_banner").css({ "background" : "transparent url(" + gsBanner + ") no-repeat" });				
			$("#video_corner .gs_banner").css({ "background" : "transparent url(" + "/images/homepage/y2009/gs_" + json.gamestate + "_right.gif" + ") no-repeat" });	
		};	  
	 })();
	} catch(e) {
		if(ClubHome.HTHLinescore.isInit)ClubHome.initRow1Videos();	
	}			
			   
});