


// load dependent libraries
bam.loadSync(bam.homePath+"bam.cookies.js", bam.homePath + 'bam.gameId.js');
// if(!!bam.cookies.get("useNarrowBand"))document.location.replace(bam.mlbhome.narrowband);

// TO DO: verify these bam.cookies commands are working correctly
(function($){
	var bwdetect = setTimeout(function(){
		if(window.confirm("To view the MLB.com broadband page, please click OK.  Or, to view the MLB.com narrowband page, please click cancel.")){
	//		bam.cookies.remove('useNarrowBand');
		}else{
	//		bam.cookies.set('useNarrowBand', true, 365, '/', '.mlb.com');
			document.location.replace(bam.mlbhome.narrowband);
		}
	},30000);	
	$(function() { clearTimeout(bwdetect); });		
})(jQuery);






bam.mlbhome = (function($){	

	var _log=function(msg){
		if (_self.debug && typeof console!=="undefined") {
			if (typeof msg === "string") console.log("bam.mlbhome: "+msg);
			else console.log(msg);			
		}	
	},
	
	
	_properties = {},
	$doc = $(document),
	
	/**
	* 
	*
	* _videoModule
	*
	*
	*/

	_videoModule = {
		init: function(){				
			
			// hack to reposition MLBTV content if the tab has been moved			
			var mlbtvNavIdx = $("#vm_nav li").index($("#vm_nav li:contains('MLB.TV')"));			
			if(mlbtvNavIdx !== 2){
				$("#videoContainer")
					.find(".h_module_content")
					.last()
					.remove()
					.insertBefore("#videoContainer .h_module_content:eq("+mlbtvNavIdx+")") 
					.find(".vm_slide_thumb, .vm_mlbtv img, img.vm_mlbtv").each(function(){					
						if(!$(this).attr("src")) {
							$(this).attr("src",$(this).closest("*[thumb]").attr("thumb"));
						}				
					});					
			}			
								
			$("#videoContainer .vm_slide a").click(function(){	
				var idx = $("#videoContainer .h_module_content").index($(this).parents(".h_module_content")),		
					tabtext = $("#videoContainer #vm_nav li:eq("+idx+")").text();	
				bam.tracking.track({
					async:{
						isDynamic: false,
						compName: "MLB Video Content Module",
						compActivity: "MLB VC " + tabtext + " Video Click",
						actionGen:true
					}
				}, $(this)[0]);
			});			
			$("#videoContainer .vm_ftr a").click(function(){	
				bam.tracking.track({
					async:{
						isDynamic: false,
						compName: "MLB Video Content Module",
						compActivity: "MLB VC Highlights More Highlights Link Click",
						actionGen:true
					}
				}, $(this)[0]);
			});	
			$("#videoContainer .vm_alt2_right_inner img").click(function(){		
				popWin("http://getmlbnetwork.viewerlink.tv/?zip="+$("#videoContainer .vm_alt2_right_inner input").val(),"channel_locator",990,600,"scrollbars=yes,resizable=yes");
				bam.tracking.track({
					async:{
						isDynamic: false,
						compName: "MLB Video Content Module",
						compActivity: "MLB VC: MLB Network Channel Find Click",
						actionGen:true
					}
				}, $(this)[0]);	
			}).css({cursor:"pointer"});			
			// show random ad
			var _rotatingads = $(".vm_rotate"),
				_idx = Math.floor(Math.random()* _rotatingads.length);		
			$(".vm_rotate:eq("+_idx+")").show();			
			delete this.init;	
		},		
		
		setTab: function(e,idx){			
			if (!!_videoModule.heartbeat) clearInterval(_videoModule.heartbeat);					
			if(idx === 2 ) _videoModule.rotateAds();	
			var $activeContent = $(".h_module_content:eq("+idx+")");		
			// download the images only when the tab becomes active	
			$activeContent.find(".vm_slide_thumb, .vm_mlbtv img, img.vm_mlbtv").each(function(){					
				if(!$(this).attr("src")) {
					$(this).attr("src",$(this).closest("*[thumb]").attr("thumb"));
				}				
			});			
		},				
		
		rotateAds: function(){		
			var elems = $(".vm_rotate"),
				next = $(".vm_rotate:visible").next();			
			if(next.length===0) next = $(".vm_rotate:eq(0)");					
			var idx = $(".vm_rotate").index(next);								
			_videoModule.heartbeat = setInterval(
				function(){
					$(".vm_rotate:eq("+idx+")").hide();
					idx++;
					if(idx > elems.length-1)idx=0;				
					var $theElem = $(".vm_rotate:eq("+idx+")");
					if($.browser.safari)$theElem.show("fast");
					else $theElem.fadeIn("slow");	
				}			
			,15000);
		}	
	},

	
	
	
	/**
	* 
	*
	* _loadThumbs
	* 
	* utility function to lazy load images
	*
	*
	*/
	_loadThumbs = function($container){	
		$container.find("img[thumb]").each(function(){	
			var $t = $(this), newSrc = $t.attr("thumb");						
			$t.load(function(){			
				$t.fadeIn();			
			})
			.attr("src", newSrc)
			.removeAttr("thumb");				
		});	
	},
	
	/**
	* 
	*
	* _createCarousel
	* 
	* utility function for initializing a jCarousel
	*
	*
	*/
	_createCarousel = function(props){		
		_log("creating carousel");
		var _c = props.container,
			maxItems = props.maxItems,									
			listItems = _c.find(".carousel_container li");
		_c.find(".carousel_pag li:eq(0)").addClass("pagOn").end().find(".carousel_container").show();
		//Remove any extra li items 			
		if(listItems.length>maxItems){
	   		listItems.slice(maxItems).each(function(){$(this).remove();});
		}								
	 	//Init JCarousel Lite
		$(_c.find(".carousel_container")).jCarouselLite({
			btnPrev   : _c.find(".carousel_prev"),
			btnNext   : _c.find(".carousel_next"),
			scroll    : 1,
			visible   : 1,
			speed     : 350,
			start	  : props.start || 0,
			vertical  : !!props.vertical,			 
			btnGo: [
				_c.find(".carousel_pag li:eq(0)"), 
				_c.find(".carousel_pag li:eq(1)"), 
				_c.find(".carousel_pag li:eq(2)") 
			],
			beforeStart : function(item){
				_c.find(".carousel_pag li").removeClass("pagOn");
	   		},
			afterEnd : function(item, isAutoScroll){										
				var allItems = item.parent().children(),
					allNavItems = _c.find(".carousel_pag li"),
					itemNum = allItems.index(item);
				if(itemNum > allNavItems.length)itemNum=1;
				else if(itemNum===0)itemNum=allNavItems.length;											
				itemNum--;
				_c.find(".carousel_pag li:eq("+itemNum+")").addClass("pagOn");					
				_loadThumbs(item); // lazy load images		
			}
		});		
		_c.find(".carousel_next, .carousel_prev").hover(
			function(){ $(this).addClass("on"); },
			function(){ $(this).removeClass("on"); }
		);				
	},	
	
	
	/**
	* 
	*
	* _twitter
	* 
	*
	*/	
	_twitter = {	
		init: function(){
			this.load();
			//stop the polling for now
			//var that=this;
			//setInterval(function(){ that.load(); }, 30000);					
		},	
		load: function(){
			_log("loading twitter data");
			$.ajax({
				url:		"/gdcross/components/game/mlb/twitter/mlbInsiderTweets.json",
				async:		true,
				timeout: 	5000,		
				dataType:	"json",				
				error: 		function(xhr) {
					_log("Error processing twitter: " + xhr.status + " " + xhr.statusText);			
				},		
				success: 	function(json) {						
					var $container = $("#twitter_container ul.twitter_blurb");
					$container.hide().empty();
					$.each(json,function(n){				
						if(n<2) {		
									
							var htmlStr = "";				
							if(!!this.user.profile_image_url){
								htmlStr += "<div class='image_container'><img src='" + this.user.profile_image_url + "' width='48px' heigh='48px' alt=''></div>";
							}
							if(!!this.user.screen_name){
								htmlStr += "<span><a class='tw_user' href='http://twitter.com/"+this.user.screen_name+"' target='_blank'>@"+this.user.screen_name+":</a> </span>";
							}						
							if(!!this.text){
								htmlStr += "<span class='tw_text'>"+this.text+"</span>";
							}										
							// TO DO: convert time to "X minutes ago"	
							/*
							var time = this.created_at; //"Fri Mar 26 05:02:12 +0000 2010"															
							htmlStr += "<div class='tw_time'>"+time+"</div>"
							*/							
							var $li = $("<li>").html(htmlStr);
							if(n===0)$li.addClass("first");							
							$container.append($li);									
						}				
					});		
					$container.fadeIn();							}		
			});		
		}	
	},
	
	
	
	/**
	* 
	*
	* _blogs
	* 
	*
	*/	
	_blogs = {	
		init: function(){
			// trim xtra li's
			$('#recent-blogs li:gt(5)').remove();				
		}
	},

	/**
	* 
	*
	* _photogalleries
	* 
	*
	*/	
	_photogalleries = {	
		curIndex:0,		
		init: function(){			
			var that=this;			
			$("#ph_prev").hover(
				function(){ $(this).css({ "background-position" :"0px 0px" }); },
				function(){ $(this).css({ "background-position" :"0px -180px" }); }					
			).click(function(){
				that.showphoto(that.curIndex-1);
			});	
			$("#ph_next").hover(
				function(){ $(this).css({ "background-position" :"0px -90px" }); },
				function(){ $(this).css({ "background-position" :"0px -270px" }); }			
			).click(function(){
				that.showphoto(that.curIndex+1);
			});
			that.showphoto(0);
		},		
		showphoto: function(index){
			var strIndex, pics=bam.mlbhome.photogalleries || [];			
			if (index < 0) this.curIndex = pics.length-1;
			else if (index > pics.length-1) this.curIndex = 0;
			else this.curIndex = index;		
			strIndex = this.curIndex+1;					
			var pic = pics[this.curIndex];
			if(pic) {
				$("#ph_slide")
					.attr("href",pic.url)
					.css("background-image","url("+pic.src+")");
				$("#ph_num")
					.html(strIndex.toString() + " of " + pics.length + " galleries");
				if (pic.gameid) {		
					var g         = bam.gameId.game(pic.gameid);
					var gDate     = g.gameDate.formatDate("M/d");
					var gAwayTeam = g.awayTeam.display_code.toUpperCase();
					var gHomeTeam = g.homeTeam.display_code.toUpperCase();		
					$("#ph_caption").html(gDate + ": " + gAwayTeam + " @ " + gHomeTeam);				
					if (g.gameNum === "2") $("#ph_caption").append(", Game 2");
				} else {
					$("#ph_caption").html(pic.title);
				}
			}
		}	
	},
	
	
	/**
	*
	*
	*
	* HEADLINE STACKS
	*
	* components:
	* createStack,  setTab, teamBeats, teamHeadlines
	*
	*	
	**/		
	_headlines = {	
		
		// _headlines.init
		init : function(){	
			_log("_headlines.init");		
			// team Beats
			_headlines.teamBeats.init();			
			// Pulse icon click			
			$doc.selector = "a.pulseicon";
			$doc.live("click", function(e) {
				e.preventDefault();
				var gameid = $(this).attr("gameid"),
					gamepk = $(this).attr("gamepk");
				bam.show_gamepulse(gamepk, gameid);
				bam.tracking.track({
					async:{
						isDynamic:true,
						compName:"MLB Headline Stack",
						compActivity:"MLB Headline Stack: Pulse Click",
						actionGen:true
					}
				}, $(this)[0]);
				return true;
			});			
		},
		
		/*
		* @createStack 	
		*
		* @function
		* @args: 
		* 	url : "/gen/news/headlines.json"	[json]
		* 	container : "ul#someID"				[selector]
		*	endAt : 6							[number]
		* 	callback : function(){}				[function]
		*
		* @requires
		* 	$.template
		*
		* AK: added support for startAt config option used to avoid dupes in team beats
		* 
		*/		
		createStack : function(p) {				
			var tpl = "/mlb/components/home/y2010/headlines_pulse.tpl";	
//			var tpl = "/mlb/components/home/y2010/headlines.tpl";
			$.ajax({
				url:		p.url,				
				type:		"get",
				timeout: 	5000,
				dataType:	"json",						
				error: 		function(xhr) {_log("Error: " + xhr.status + " " + xhr.statusText);},						
				success: 	function(json) {			
					var arr			= bam.util.ensureArray(json.members),											
						$html 		= $.template(tpl,arr),	
						numFloated 	= $html.find(".hl_float").length,	
						startAt		= p.startAt || 0,						
						endAt		= p.endAt + Math.floor(numFloated/2),		// increment number of headlines displayed if there are any floats			
						$headlines = $html.find("dd").slice(startAt, endAt);							
					$(p.container).empty().append($headlines).find(".hl_float:odd").addClass("hl_float_odd").after("<dd class='hl_clear' />");		
					if($.isFunction(p.success)) {
						p.success(json);	
					}											
				},
				complete:	function(){
					if($.isFunction(p.callback)) {
						p.callback();	
					}
				}	
			});	
		},
		
		// _headlines.setTab
		setTab: function(e,idx){		
			var datasources =["/gen/news/headlines.json","/gen/home/featured.json"],
				container = "#headlinesContainer .h_module_content:eq("+idx+") dl.hl_list";			
			if($(container + " li").length === 0 && idx<2) {	
				_headlines.createStack({
					url			:	datasources[idx],
					container	:	container,
					endAt		:	8
				});				
			} else if(idx===2 && !!_headlines.teamHeadlines.init){
				_headlines.teamHeadlines.init();
			}
		},
		
		// _headlines.teamBeats
		teamBeats: {
		
			israndomclub: false,
		
			// _headlines.teamBeats.init
			init: function(){
				// if the mlbteamcookie is not set, load a random club
				var club=bam.cookies.get("mlbteamcookie"),
					randomclubs = ["nyy","nym","chc","bos","la","sf"];				
				if(!club) {	
					club = randomclubs[Math.floor(Math.random()*(randomclubs.length))]; 	
					_headlines.teamBeats.israndomclub = true;
				}				
				_headlines.teamBeats.load(club,_headlines.teamBeats.israndomclub);				
				// tracking
				$("#teams_beat select").change(function(){				
					var teamselect = $(this).find("option:selected").val();
					if(teamselect !== ""){
						_headlines.teamBeats.load($(this).find("option:selected").val());					
						bam.tracking.track({
							async:{
								isDynamic:true,
								compName:'MLB Team Beats',
								compActivity:'MLB Team Beats: Team Click',
								actionGen:true
							}
						}, $(this)[0]);		
					}		
				});			
				$doc.selector = "#teams_beat a";
				$doc.live("click", function() {
					bam.tracking.track({
						async:{
							isDynamic:false,
							compName:"MLB Team Beats",
							compActivity:"MLB Team Beats: Headline Click",
							actionGen:true
						}
					}, $(this)[0]);
					return true;
				});					
			},
			
			// _headlines.teamBeats.load
			load: function(club){			
				// access headlines JSON and populate stack	
				_headlines.createStack({
					url			:	"/gen/"+club+"/news/headlines.json",
					container	:	"#teams_beat dl",
					startAt   	: 	1, // skip first (0th) item since it will be displayed by render()
					endAt		:	4, 
					success		:	function(json){
						_headlines.teamBeats.render(club,json);
					},
					callback	:	function(){
						if(!!_headlines.teamBeats.israndomclub) {
							//set team cookie
							var expireDate=new Date();
							expireDate.setFullYear(expireDate.getFullYear()+1);
							bam.cookies.set({
								name: "mlbteamcookie", 
								value: club, 
								expires: expireDate,
								domain: "mlb.com"
							});
						}
					}
				});			
			},			
			
			/* 
			*
			* _headlines.teamBeats.render
			* @private function
			* renders themain item (the line items are rendered with the cached headlines template
			*
			*/
			render: function(club,json){
				// render the main item
				var CONTAINER = $("#teams_beat"),
					cp = bam.mlbhome.getClubProperties(),				
					cname = cp[club].club_common_name,
					morelink = "http://" +  cp[club].url_cache + "/news/index.jsp?c_id="+club,
					data = bam.util.ensureArray(json.members)[0],
					headlineToStr = $("<div>").html(data.headline).remove().text(),
					imgsrc = "",
					hl = data.headline,
					u = data.url,
					type = data.type,
					thelink="";					
			
				for(i=0;i<data.children.length;i++){				
					if(~u.indexOf("preview_web_away") && data.children[i].defkey === "away_thumbnail") {
						imgsrc = data.children[i].url;
						break;
					} else if(~u.indexOf("preview_web_home") && data.children[i].defkey === "home_thumbnail") {
						imgsrc = data.children[i].url;
						break;
					} else if(data.children[i].defkey === "medium-thumbnail") {
						imgsrc = data.children[i].url;
						break;
					}	
				}	
				
				if(type === "articles") {
					thelink = "<a href='" + u + "'>" + hl + "</a>";
				} else if(type === "content_short") {			
					var numOpenTags = hl.split("<").length, 
						numCloseTags = hl.split(">").length;
					if(numOpenTags===numCloseTags){
						thelink = hl;
					}else{
						thelink = "<a href='" + u + "'>" + hl + "</a>";
					}				
				}		
				CONTAINER
					.find("h4")
					.text(cname)		
					.end().find("dl.related_links").prepend("<dt><a href='"+morelink +"'>More from "+cname+"</a></dt>")										
					.end().find(".image_left").attr({
						"src": imgsrc,
						"alt": headlineToStr		
					})							
					.end().find("h5").html(thelink)
					.end().find("p .more a").attr("href",bam.string.unescapeHTML(u))							
					.end().show();						
			}			
		},
		
		// _headlines.teamHeadlines
		teamHeadlines: {
			// _headlines.teamHeadlines.init
			init: function(){
				_log("init teamHeadlines");
				// check if club preference has already been set
				var club=bam.cookies.get("mlbteamcookie");		
				if(!!club) this.load(club);
				else this.showTeamSelector();					
				// assign change team handlers and tracking				
				$("#myTeamsHeadlinesSelect").change(function(){		
					_headlines.teamHeadlines.load($(this).find("option:selected").val());	
					bam.tracking.track({
						async:{
							isDynamic:true,
							compName:'MLB Headline Stack',
							compActivity:'MLB Headline Stack: My Team: Team Selection',
							actionGen:true
						}
					}, $(this)[0]);				
				});				
				$doc.selector = "#teamHeadlinesChange";
				$doc.live("click", function(e) {
					bam.tracking.track({
						async:{
							isDynamic:false,
							compName:'MLB Headline Stack',
							compActivity:'MLB Headline Stack: My Team: Change Team Link Click',
							actionGen:true
						}
					}, $(this)[0]);
					_headlines.teamHeadlines.showTeamSelector();
				});
				//delete this;
			},
			
			// _headlines.teamHeadlines.load
			load: function(club){
				//set defaults
				$("#teamSelector").hide();				
				$("#teamHeadlines ul").empty();				
				// populate DOM elements with selected club's properties	
				var cp = bam.mlbhome.getClubProperties();					
				$("#teamHeadlines").find("#teamHeadlinesTxt")
					.text(cp[club].club_common_name.toUpperCase() + " HEADLINES")				
					.closest(".h_module_content")
					.find("#hl_ftr_left")
					.attr("href","http://" +  cp[club].url_cache + "/news/index.jsp?c_id="+club)
					.html("More " + cp[club].club_common_name + " Stories").show();				
				$("#teamHeadlines").show();				
				// access headlines JSON and populate stack	
				_headlines.createStack({
					url			:	"/gen/"+club+"/news/headlines.json",					
					container	:	"#teamHeadlines dl",
					endAt		:	6,
					callback	:	function(){
						//set team cookie
						var expireDate=new Date();
						expireDate.setFullYear(expireDate.getFullYear()+1);
						bam.cookies.set({
							name: "mlbteamcookie", 
							value: club, 
							expires: expireDate,
							domain: "mlb.com"
						});
					}
				});	
			},
			
			// _headlines.teamHeadlines.showTeamSelector
			showTeamSelector: function(){
				$("#teamHeadlines").hide().closest(".h_module_content").find("#hl_ftr_left").hide();				
				$("#teamSelector").show(); 	
			}
		
		}

	
	},


	/**
	* 
	*
	* _navbuttons
	* 
	* utility function for setting up generic tabbed navigation for content modules
	*
	*
	*/
	_navbuttons = function(){			
		var isfirstclick=true;		
		var setContent = function(e,idx){
			var $target = $(e.target);	
			var $container = $target.closest(".h_module");
			var $activeContent = $container.find(".h_module_content:eq("+idx+")");		
			var compName = 	$container.find(".navbuttons").attr("compName");
			if(!compName) compName = "MLB Tab Navigation";
			$activeContent.siblings(".h_module_content").hide();
			$activeContent.fadeIn();
			if(!isfirstclick) {
				bam.tracking.track({
					async:{
						isDynamic: true,
						compName: ((compName === "MLB VC Module") ? "MLB Video Content Module" : compName),
						compActivity: compName + ": " + $target.closest("li").find("h3").text() + " Tab Click",						
						actionGen:true
					}
				}, $target);					
			}	
		};		
		var actions = {	"vm_nav" : _videoModule.setTab, "hl_nav": _headlines.setTab };	// add custom functions here. the property name must equal the id of the container ul			
		$doc.selector = "ul.navbuttons li";
        $doc.live("click", function(e) {	
			var $li = $(this);			
			if($li.parent().children().length > 1) {
				var idx = $li.parent().children("li").index($li);								
				$li.find("div").andSelf().addClass("tb_on");
				$li.siblings().removeClass("tb_on").find("li,div").removeClass("tb_on");	
				setContent(e,idx);	
				var key = $li.parent().attr("id");
				var action = actions[key];
				if($.isFunction(action)) {action(e,idx);}			
			}	
        });
		$("ul.navbuttons li").each(function(){				
			var old_html = $(this).html();
			var new_html = "<div class='tb_left'></div><div class='tb_mid'>" + old_html + "</div><div class='tb_right'></div>";					
			$(this).html(new_html).show();				
		});				
		$("ul.navbuttons").each(function(){		
			isfirstclick=true;
			var $container = $(this),
				$items = $container.find("li");			
			if ($items.length > 1) {				
				var $default_tab = $container.find("li.h_module_nav_default");								
				if($default_tab.length>0) {
					$container.find("li").css("cursor","pointer");
					$default_tab.find("div.tb_mid").click();
					isfirstclick=false;
				}else{					
					var rand = Math.floor(Math.random() * $items.length);
					$container.find("li").css("cursor","pointer").end()
					.find("li:eq("+rand+")").find("div.tb_mid").click();
					isfirstclick=false;
				}
			} else {
				$(this).closest(".h_module").find(".h_module_content").fadeIn();	
			}			
		});		
	},
	
	
	
	
	
	/**
	* 
	*
	* _miniStandings
	* 
	* New Dynamic Mini-Standings code <Sam>
	*
	*
	*/
	_miniStandings = (function() {
		var bhp = bam.homePath;
		
		//Load dependencies
		bam.loadSync(bhp + "bam.datetime.js",
					 bhp + "bam.cookies.js",
					 bhp + "bam.datagrid2.js",
					 bhp + "bam.ajaxHelper.js");
		
		// Helper Functions
		//Cached jQuery selector getter
		function $get(selector) {
			return $get.cache[selector] = $get.cache[selector]||$(selector);
		}
		$get.cache = {};
		//Formats a string by replacing positional $n placeholders with arguments
		//ex: formatString("this is a %0", "test") -> this is a test
		function formatString() {   // Args: str, $0, $1 ....
			if(arguments[1]) {
				var args = Array.prototype.slice.apply(arguments, [1]), rx = /%([0-9])/g;
				return arguments[0].replace(rx, function($0, $1) {
					return args[~~+$1] || '';
				});		
			}
		}
		// End Helper Functions 
		
		var RESULTS_MAX = 6,
			SYS_YEAR = 2010, //overwriten in mvc.init
			DEFAULT_TEAM = bam.cookies.get('mlbteamcookie'),
			SEASON_LEAGUES = {
				regular: [103, 104],
				spring: [103, 104, 114, 115]
			},
			LEAGUES = {	AL: 103, NL: 104, CL: 114, GL: 115	},
			DIVISIONS = { 103: { West:200, East:201, Central:202}, //AL
						  104: { West:203, East:204, Central:205}  //NL
						},
			DIVISION_TEAMS = {				
					200: {oak:0,ana:0,sea:0,tex:0},		  		//AL - West
					201: {bos:0,bal:0,nyy:0,tb:0,tor:0}, 		//AL - East
					202: {cws:0,cle:0,det:0,kc:0,"min":0},		//AL - Central
					203: {ari:0,col:0,la:0,sd:0,sf:0},			//NL - West
					204: {was:0,fla:0,nym:0,phi:0,atl:0},		//NL - East
					205: {chc:0,cin:0,hou:0,mil:0,pit:0,stl:0}	//NL - Central
			},
			SHOW_PREFIX_LEGEND = false,
			DIVISION_NAMES = {East:"East", Central:"Central", West:"West", 0:"East", 1:"Central", 2:"West"},
			SEASON_STATUS_URI = "/lookup/named.properties_info_events_season.bam?season=%0&sport_code='mlb'&org_id=1",
			SPRING_SEASON_URI = "/lookup/named.standings_spring_repeater.bam?" +
							"season=%0&" + $.param({league_id: SEASON_LEAGUES.spring},true),
			REGULAR_SEASON_URI = "/lookup/named.standings_all_league_repeater.bam?"+
							"sit_code='h0'&season=%0&standings_all.col_in=division_champ,place,wild_card,playoffs_sw,division_id,division,team_short,file_code,w,l,pct,gb&" + 
							$.param({league_id: SEASON_LEAGUES.regular},true),
			WILD_CARD_URI = "/lookup/named.standings_wild_card_league_repeater.bam?"+
							"sit_code='h0'&season=%0&results=5&standings_wild_card.col_in=team_short,file_code,w,l,pct,gb,wild_card&" + 
							$.param({league_id: SEASON_LEAGUES.regular},true),
			EXHIBITION_LINK = "/mlb/standings/exhibition.jsp",
			NO_DATA_MSG = "Data is currently unavailable",
			ACTIVE = "active", REL = "rel",	XML = "xml",
			COMMON_COL_WIDTH = "25px",
			CLUB_PROPS,
			COLUMN_DEFS = [
				{title: "Team", width:"135px", dataField:"team_short", 
					decorator: function(){	
						var prefixedName = mvc.view.setTeamPrefix.apply(this, arguments),
							file_code = this.rawData.file_code, clubUrl;
						if(CLUB_PROPS && (file_code in CLUB_PROPS)) {
							clubUrl = CLUB_PROPS[file_code].club_common_url;
							prefixedName = "<a href=\"http://"+clubUrl+"\">" + prefixedName + "</a>";
						}
						return prefixedName;
					}
				},
				{title: "W", 	width:COMMON_COL_WIDTH,  dataField:"w"},
				{title: "L", 	width:COMMON_COL_WIDTH,  dataField:"l"},
				{title: "PCT",	width:COMMON_COL_WIDTH,  dataField:"pct"},
				{title: "GB", 	width:COMMON_COL_WIDTH,  dataField:"gb"},
				{title: "wc", 	visible: false, dataField:"wild_card"},
				{title: "dc", 	visible: false, dataField:"division_champ"},
				{title: "po", 	visible: false, dataField:"playoffs_sw"}
			],
			DATAGRID_BASE = {		
				tableClass:"ministandings",
				noResultsMessage: NO_DATA_MSG,
				sortable: false,
				columns: COLUMN_DEFS
			},
			AL_GRID = new bam.datagrid.DataGrid(DATAGRID_BASE),
			NL_GRID = new bam.datagrid.DataGrid(DATAGRID_BASE),
			// CL/GL - to come 
		utils = {
						
			//	Returns a random number from a range of numbers
			//	@from - starting number
			//	@to - ending number
			
			getRandomNumber: function(from, to) {
				from = ~~+from; to = ~~+to;
				return Math.floor(Math.random( ) * to - from + 1) + from;
			},
			
			//	Retrieves objec data nested deep inside an object
			//	@obj - object that contains data (i.e. {foo:[{test:{inside:'data'}]} )
			//	@pattern - members data inline pattern (i.e. "foo[0].test.inside")
			
			getDeep: function(obj, pattern) {
				if(obj && typeof(pattern) === "string") {
					var tokens = pattern.split('.'), rxIndex = /(.+)\[([0-9]+)\]/, parts, index, out;
					$.each(tokens, function(i, token) {
						parts = token.match(rxIndex);
						if(parts) {
							token = parts[1];
							index = ~~+parts[2];
							if(token in (out||obj) && typeof((out||obj)[token][index]) !== "undefined") {
								out	= (out||obj)[token][index];
							}
						} else if(token in (out||obj)){
							out = (out||obj)[token];
						}
					});
					return out;
				}				
			}
		},
		dao = {
			//Lookup service wrapper for Season Schedule
			seasonScheduleLookup: bam.ajaxHelper.createInstance({
				traditional: true,
				cache: {},
				seasonStart: 0,
				springTrainingStart: 0,
				springTrainingEnd: 0,
				allStarEnd: 0,
				dataType: XML,
				load: function(season, cb) {
					var that = this;
					this.url = formatString(SEASON_STATUS_URI, season);
					if($.isFunction(cb)) {				
						this.makeRequest(function(data) {
							that.cache = data;
							cb();
						});
					}
				},				
				// Helpers 
				//Returns regular season start date (cached)
				getSeasonStartDate: function() {
					var out = 0, date;
					if(this.cache) {
						if(this.seasonStart) {
							out = this.seasonStart;
						} else {
							var root = this.cache;
							date = utils.getDeep(root, "org_history[0].queryResults[0].row[0].first_date_seas");
							out = this.seasonStart = date && bam.datetime.parseISODate(date);
						}
					}
					return out;
				},
				// Returns Date based on schedule event name <Date>
				//	@eventName - name of the event to filter by (i.e.Spring Training Start) 
				
				getScheduleEventDate: function(eventName) {
					if(this.cache) {
						var root = this.cache,
							evt = utils.getDeep(root, "root.schedule_event_info[0].queryResults[0].row"),
							date = evt && evt.getNodesByAttribute("event", eventName),
							out = date && date.length && bam.datetime.parseISODate(date[0].game_date);
						return out;
					}
				},
				// Returns Spring Training Start date <Date> 
				getSpringTrainingStart: function() {
					var out = 0, date;
	
					if(this.springTrainingStart) {
						out = this.springTrainingStart;
					} else {
						date = this.getScheduleEventDate("Spring Training Start");
						out = this.springTrainingStart = date;
					}
					return out;
				},
				// Returns Spring Training End date <Date> 
				getSpringTrainingEnd: function() {
					var out = 0, date;
	
					if(this.springTrainingEnd) {
						out = this.springTrainingEnd;
					} else {							
						date = this.getScheduleEventDate("Spring Training End");
						out = this.springTrainingEnd = date;
					}				
					return out;
				},
				// Returns All-Star Start date <Date> 
				getAllStarEnd: function() {
					var out = 0, date;
	
					if(this.allStarEnd) {
						out = this.allStarEnd;
					} else {							
						date = this.getScheduleEventDate("All-Star End");
						out = this.allStarEnd = date;
					}
					return out;
				},
				
				//	Returns a season year that should be used for all lookups
				//	@now - system date for today
				
				getSeason: function(now) {
					if(this.cache) {
						var season_start = this.getSeasonStartDate(),							
							spring_training_start = this.getSpringTrainingStart(),
							earliest = Math.min(this.seasonStart, this.springTrainingStart);
							
						return now.valueOf() < earliest?season_start.getFullYear()-1:season_start.getFullYear();
					}
				},
				
				//	Returns true if Spring training is in affect
				//	@now - system date for today
				
				isSpringTraining: function(now) {
					if(now) {
						var spring_training_start = this.getSpringTrainingStart(),
							spring_training_end = this.getSpringTrainingEnd(),
							season_start = this.getSeasonStartDate();
						//If now is within a spring training range and reg. season start is greater then spring season end.
						return now.valueOf() >= spring_training_start.valueOf() && now.valueOf() <= spring_training_end.valueOf() && season_start.valueOf() > spring_training_end.valueOf();
					}
				},
				
				//	Returns true if wildcard filter tab should be showing
				//	@now - system date for today
				
				isWildcardShowing: function(now) {
					if(now) {
						var all_star_end = this.getAllStarEnd();
						return all_star_end && now.valueOf() >= all_star_end.valueOf();
					}
				}
			}),
			//Lookup service wrapper for regular season data
			regSeasonLookup: bam.ajaxHelper.createInstance({
				traditional: true,
				cache: {},
				dataType: XML,
				load: function(cb) {
					var that = this;
					this.url = formatString(REGULAR_SEASON_URI, SYS_YEAR);
					if($.isFunction(cb)) {				
						this.makeRequest(function(data) {
							that.cache = data;
							cb();
						});
					}
				},
				getStandings: function(league) {
					return mvc.model.extractLeagueData.call(this, "standings_all", league);
				},
				getDivisionStandings: function(league, div) {
					league = this.getStandings(league);
					return mvc.model.filterByDivision(div, league);
				},
				onFail: function() {
					mvc.events.onDataError();
				}
			}),
			//Lookup service wrapper for Spring Training season data
			springSeasonLookup: bam.ajaxHelper.createInstance({
				traditional: true,
				cache: {},
				dataType: XML,
				load: function(cb) {
					var that = this;
					this.url = formatString(SPRING_SEASON_URI, SYS_YEAR);
					if($.isFunction(cb)) {				
						this.makeRequest(function(data) {
							that.cache = data;
							cb();
						});
					}
				},
				getStandings: function(league) {
					return mvc.model.extractLeagueData.call(this, "standings_spring", league);
				},
				onFail: function() {
					mvc.events.onDataError();
				}
			}),
			//Lookup service wrapper for Wild Card data
			wildcardLookup: bam.ajaxHelper.createInstance({
				traditional: true,
				cache: {},
				dataType: XML,
				load: function(cb) {
					var that = this;
					this.url = formatString(WILD_CARD_URI, SYS_YEAR);
					if($.isFunction(cb)) {						
						this.makeRequest(function(data) {
							that.cache = data;
							cb();
						});						
					}
				},
				getStandings: function(league) {				
					return mvc.model.extractLeagueData.call(this, "standings_wild_card", league);
					//return mvc.model.extractWCData.call(this, "standings_wild_card");
				}
			})
		},
		mvc = {
			model:{
				//Extracts Wild Card data from a lookup service
				extractWCData: function(dataset) {
					var out = [];
					if(this.cache && (dataset in this.cache)) {
						out = this.cache[dataset].queryResults.row;
					}
					return out;
				},
				//Extracts League data from lookup service
				extractLeagueData: function(dataset, league) {
					var out = [];
					if(this.cache && (dataset in this.cache) && !!league) {
						league = this.cache[dataset].getNodesByAttribute("league_id", String(league));
						if(!!league && !!league.length) {
							out = utils.getDeep(league[0], "queryResults[0].row");
						}
					}
					return out;
				},
				//Applies division filter to a league data
				filterByDivision: function(division, data) {
					var out = [];
					if(data && data.length) {
						out = data.getNodesByAttribute("division_id", String(division));
					}
					return out;
				},
				//Populates datagrid with data
				fillDatagrid: function(dgObj, data, userLimit) {
					if(data && data.length) {
						dgObj.clearData();
						$.each(data.slice(0, (~~+userLimit || RESULTS_MAX)), function(i, row) {
							dgObj.insertRow(row);
						});					
					}
				}
			},
			view:{
				//Pre-pends a prefix to the team name (runs in the scope of the datagrid.row)
				setTeamPrefix: function(o) {
					var out = o.value,
						wild_card = this.cell("wild_card").val(),
						division_champ = this.cell("division_champ").val(),
						playoffs_sw = this.cell("playoffs_sw").val(),
						prefix = (division_champ === 'Y')?"y-":
							(division_champ === 'N' && (playoffs_sw === 'Y' || playoffs_sw === 'C'))?"x-":
							(wild_card === 'Y')?"w-":"";
							  
						if(!!prefix) { SHOW_PREFIX_LEGEND = true; }
						
						return prefix + out;
				},	
				//Render a single standings grid
				renderGrid: function(dgObj, data, to) {
					mvc.model.fillDatagrid(dgObj, data);
					dgObj.render(to);
				},
				//Refreshes grid content
				refreshGrid: function(dgObj, data) {
					mvc.model.fillDatagrid(dgObj, data);
					dgObj.refreshView();
				},
				//Hide division links above grid (east, west, etc..)
				hideDivisionFilters: function() {
					$get("div.divisionLinks").hide();
				},
				//Show Wild Card filter
				showWCFilters: function() {
					$get("div.divisionLinks li.wc").css({display:"inline"});
				},
				//Show prefixes legend
				showLegend: function() {
					$get("#standingsLegend").show();
				},
				//Restes href for a Full Standings link (spring is diffrent then regular)
				setFullStandingsLink: function(href) {
					if(href) {
						$get("#standingsFullLink").attr("href", href);
					}
				},
				//Clears active class fom filter links
				resetActiveLinks: function(parent) {
					if(parent) { $get(parent + " div.divisionLinks li").removeClass(ACTIVE); }
				},
				//Sets default division link as active (by rel value)
				setDefaultDivisionActive: function() {
					$.each(arguments, function(i, div) {
						if(div)	{							
							$get("div.divisionLinks li[rel=" + div + "]").addClass(ACTIVE); 
						}
					});
				}
			},
			controller:{
				//Main function that initializes the component
				init: function(sysdateStr, narrowband){
					var sysdate = bam.datetime.parseYMD(sysdateStr), 
						sysyear = sysdate.getFullYear(), defDivAL, defDivNL, 
						isSpringTraining = false,
						isWildCardShowing = false;
						CLUB_PROPS = bam.mlbhome.getClubProperties();
					// @TODO: 
					//1 - Load season schedule
					dao.seasonScheduleLookup.load(sysyear, function() {						
						//2 - Determine/set season year
						SYS_YEAR = dao.seasonScheduleLookup.getSeason(sysdate);
						//3 - Determine Season Mode (Regular | Spring)
						isSpringTraining = dao.seasonScheduleLookup.isSpringTraining(sysdate);						
						if(isSpringTraining) {
							dao.springSeasonLookup.load(function() {
								RESULTS_MAX = 5;
								mvc.view.hideDivisionFilters();
								mvc.view.renderGrid(AL_GRID, dao.springSeasonLookup.getStandings(LEAGUES.AL), "standingsTableAL");
								mvc.view.renderGrid(NL_GRID, dao.springSeasonLookup.getStandings(LEAGUES.NL), "standingsTableNL");
								mvc.view.setFullStandingsLink(EXHIBITION_LINK);
							});						
						} else {
							defDivAL = mvc.controller.getDefaultDivision(LEAGUES.AL);
							defDivNL = mvc.controller.getDefaultDivision(LEAGUES.NL);
							mvc.view.setDefaultDivisionActive(defDivAL, defDivNL);
							if(defDivAL && defDivNL) {
								dao.regSeasonLookup.load(function() {																  
									mvc.view.renderGrid(AL_GRID, dao.regSeasonLookup.getDivisionStandings(LEAGUES.AL, defDivAL), "standingsTableAL");
									mvc.view.renderGrid(NL_GRID, dao.regSeasonLookup.getDivisionStandings(LEAGUES.NL, defDivNL), "standingsTableNL");
									//4 - Determine Wildcard Mode
									isWildCardShowing = dao.seasonScheduleLookup.isWildcardShowing(sysdate);
									if(isWildCardShowing) {
										dao.wildcardLookup.load(function(){
											mvc.view.showWCFilters();
										});
									}
									//5 - Render Leagues
									if(SHOW_PREFIX_LEGEND){ mvc.view.showLegend(); }
								});
							}
						}						
					});				
				},
				//Retrieves default division based on cookie data
				getDefaultDivision: function(league) {
					var rnd = utils.getRandomNumber(0, 2),
						out = DIVISION_NAMES[rnd];		
					
						if(DEFAULT_TEAM in DIVISION_TEAMS[DIVISIONS[league].East]) {
							out = DIVISION_NAMES.East;	
						} else if(DEFAULT_TEAM in DIVISION_TEAMS[DIVISIONS[league].Central]) {
							out = DIVISION_NAMES.Central;
						} else if(DEFAULT_TEAM in DIVISION_TEAMS[DIVISIONS[league].West]) {
							out = DIVISION_NAMES.West;
						}
					
					return DIVISIONS[league][out];
				},
				//Sets division filter based on rel data stored in a link
				setDivision: function(league, division) {
					var divData, dgObj = (league === LEAGUES.AL)?AL_GRID:NL_GRID;
					if(division === "wc") {
						divData = dao.wildcardLookup.getStandings(league);
						mvc.view.refreshGrid(dgObj, divData);
					} else {
						divData = dao.regSeasonLookup.getDivisionStandings(league, ~~+division);
						mvc.view.refreshGrid(dgObj, divData);						
					}
				}
			},
			events: {
				//Fires if data could not be retrieved
				onDataError: function() {
					$get("#standingsTableAL, #standingsTableNL").html(NO_DATA_MSG);					
				}
			}
		};
		
		//Window load events
		$doc.selector = "#standingsAL div.divisionLinks li";
		$doc.live("click", function(e) {
			if(!e.button) {
				mvc.view.resetActiveLinks("#standingsAL");
				var $this = $(this).addClass(ACTIVE), div = $this.attr(REL);					
				mvc.controller.setDivision(LEAGUES.AL, div);
			}
		});
		$doc.selector = "#standingsNL div.divisionLinks li";
		$doc.live("click", function(e) {
			if(!e.button) {
				mvc.view.resetActiveLinks("#standingsNL");
				var $this = $(this).addClass(ACTIVE), div = $this.attr(REL);
				mvc.controller.setDivision(LEAGUES.NL, div);
			}
		});
		//Expose mvc.controller interface to public
		return mvc.controller;						  
	})(),
	/* Mini-Standings End */
		
	
	
	/**
	* 
	*
	* _track
	* 
	*
	*/	
	_track = function(){		
	
		_log("_track");

		//headlines
		$doc.selector = "#headlinesContainer .hl_ftr_right, #headlinesContainer .h_module_content a:not(.pulseicon)";	
		$doc.live("click", function(e) {	
			var $target = $(e.target),
				$container = $target.closest("#headlinesContainer"),
				$activetab = $target.closest(".h_module_content"),
				idx = $container.find(".h_module_content").index($activetab),
				component = "MLB Headline Stack",
				tabtext = $container.find(".navbuttons li:eq("+idx+")").find("h3").text(),
				clicktext = "Headline Click",
				activity = "";		
			if ($target.hasClass("hl_ftr_left")) clicktext = "More Click";		
			activity = component + ": " + tabtext + " " + clicktext;		
			if ($target.hasClass("hl_ftr_right")) activity = component + ": RSS Click";	
			bam.tracking.track({
				async:{
					isDynamic: false,
					compName: component,
					compActivity: activity,
					actionGen: true
				}
			}, $(this)[0]);			
		});	
		
		//standings
		$doc.selector = "#standingsContent .divisionLinks";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:true,
					compName:'MLB Standings/Leaders',
					compActivity:'MLB Standings/Leaders: Standings: Division Click',
					actionGen:true
				}
			}, $(this)[0]);
		});	
		$doc.selector = "#standingsFullLink";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:false,
					compName:'MLB Standings/Leaders',
					compActivity:'MLB Standings/Leaders: Standings: Full Standings Click',
					actionGen:true
				}
			}, $(this)[0]);
		});

		
		//leaders
		$doc.selector = "#league_leaders_batting .nav";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:true,
					compName:'MLB Standings/Leaders',
					compActivity:'MLB Standings/Leaders: Leaders: Batting Stat Click',
					actionGen:true
				}
			}, $(this)[0]);
		});
		$doc.selector = "#league_leaders_pitching .nav";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:true,
					compName:'MLB Standings/Leaders',
					compActivity:'MLB Standings/Leaders: Leaders: Pitching Stat Click',
					actionGen:true
				}
			}, $(this)[0]);
		});		
		$doc.selector = "#league_leaders_more_links";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:false,
					compName:'MLB Standings/Leaders',
					compActivity:'MLB Standings/Leaders: Leaders: More Stats Click',
					actionGen:true
				}
			}, $(this)[0]);
		});
		
		//twitter
		$doc.selector = "#twitter_container .twitter_blurb";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:false,
					compName:'MLB Twitter',
					compActivity:'MLB Twitter: Content Click',
					actionGen:true
				}
			}, $(this)[0]);
		});			
		$doc.selector = "#twitter_container .more a";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:false,
					compName:'MLB Twitter',
					compActivity:'MLB Twitter: Join Click',
					actionGen:true
				}
			}, $(this)[0]);
		});			
		
		//blogs
		$doc.selector = "#recent-blogs li";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:false,
					compName:'MLB Blogs',
					compActivity:'MLB Blogs: Blog Click',
					actionGen:true
				}
			}, $(this)[0]);
		});			
		$doc.selector = "#blogs_container .more a";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:false,
					compName:'MLB Blogs',
					compActivity:'MLB Blogs: More Blogs Click',
					actionGen:true
				}
			}, $(this)[0]);
		});		
		$doc.selector = ".blogs_module .more a";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:false,
					compName:'MLB Blogs',
					compActivity:'MLB Blogs: More Blogs Click',
					actionGen:true
				}
			}, $(this)[0]);
		});				
				
		//fantasy
		$doc.selector = "#fantasy_container div";  
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:false,
					compName:'MLB Fantasy Panel',
					compActivity:'MLB Fantasy Panel: Content Click',
					actionGen:true
				}
			}, $(this)[0]);
		});
		$doc.selector = "#fantasy_container .more:last";  
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:false,
					compName:'MLB Fantasy Panel',
					compActivity:'MLB Fantasy Panel: More Click',
					actionGen:true
				}
			}, $(this)[0]);
		});
	
		//spotlight		
		$("#h_spotlight .carousel_next").click(function(e) {
			bam.tracking.track({
				async:{
					isDynamic:true,
					compName:'MLB Spotlight',
					compActivity:'MLB Spotlight: Next Click',
					actionGen:true
				}
			}, $(this)[0]);
		});
		$("#h_spotlight .carousel_prev").click(function(e) {
			bam.tracking.track({
				async:{
					isDynamic:true,
					compName:'MLB Spotlight',
					compActivity:'MLB Spotlight: Previous Click',
					actionGen:true
				}
			}, $(this)[0]);
		});		
		$doc.selector = "#h_spotlight .carousel_container";  
		$doc.live("click", function(e) {
			var $target=$(e.target);
			var items = $target.closest("ul").find(".carousel_item");
			var idx = items.index($target.closest(".carousel_item"));	
			idx=idx-2;	
			if($target.closest("li").hasClass("isClone")) {
				idx=(idx<1)?idx+9:idx-9;
			}			
			bam.tracking.track({
				async:{
					isDynamic:false,
					compName:'MLB Spotlight',
					compActivity:'MLB Spotlight: Panel ' + idx + ' Click',
					actionGen:true
				}
			}, $(this)[0]);					
		});		
		$("#h_spotlight .carousel_pag li").click(function() {		
			var items = $(this).closest("ul").find("li");
			var idx = items.index($(this));		
			++idx;		
			bam.tracking.track({
				async:{
					isDynamic:true,
					compName:'MLB Spotlight',
					compActivity:'MLB Spotlight: Circle ' + idx + ' Click',
					actionGen:true
				}
			}, $(this)[0]);
			return false;
		});	
		
		// initiatives
		$("#h_initiatives .carousel_next").click(function(e) {
			bam.tracking.track({
				async:{
					isDynamic:true,
					compName:'MLB Initiatives',
					compActivity:'MLB Initiatives: Next Click',
					actionGen:true
				}
			}, $(this)[0]);			
		});
		$("#h_initiatives .carousel_prev").click(function(e) {
			bam.tracking.track({
				async:{
					isDynamic:true,
					compName:'MLB Initiatives',
					compActivity:'MLB Initiatives: Previous Click',
					actionGen:true
				}
			}, $(this)[0]);
		});	
		$("#h_initiatives .carousel_container li").click(function(e) {
			bam.tracking.track({
				async:{
					isDynamic:false,
					compName:'MLB Initiatives',
					compActivity:'MLB Initiatives: Panel Click',
					actionGen:true
				}
			}, $(this)[0]);
		});	

		//photos
		$doc.selector = "#h_photos #ph_prev";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:true,
					compName:'MLB Photos',
					compActivity:'MLB Photos: Previous Click',
					actionGen:true
				}
			}, $(this)[0]);
		});		
		$doc.selector = "#h_photos #ph_next";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:true,
					compName:'MLB Photos',
					compActivity:'MLB Photos: Next Click',
					actionGen:true
				}
			}, $(this)[0]);
		});		
		$doc.selector = "#h_photos #ph_slide";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:false,
					compName:'MLB Photos',
					compActivity:'MLB Photos: Gallery Click',
					actionGen:true
				}
			}, $(this)[0]);
		});
		$doc.selector = "#h_photos #ph_more";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:false,
					compName:'MLB Photos',
					compActivity:'MLB Photos: More Click',
					actionGen:true
				}
			}, $(this)[0]);
		});
		
		// poll
		$doc.selector = "#pollContainer #submitButton";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:false,
					compName:'MLB Poll',
					compActivity:'MLB Poll: Vote Click',
					actionGen:true
				}
			}, $(this)[0]);
		});		
		
		// Hot Stove Headline Click
		$doc.selector = "#hot_stove_container ul li a";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:false,
					compName:'MLB Hot Stove Notes',
					compActivity:'MLB Hot Stove Notes: Headline Click',
					actionGen:true
				}
			}, $(this)[0]);
		});
		
		// "More Player Notes" Click
		$doc.selector = "#hot_stove_container .more";
		$doc.live("click", function(e) {
			bam.tracking.track({
				async:{
					isDynamic:false,
					compName:'MLB Hot Stove Notes',
					compActivity:'MLB Hot Stove Notes: More Notes Click',
					actionGen:true
				}
			}, $(this)[0]);
		});
		
		
	},


	

	/**
	* 
	*
	* _self
	* 
	* root object
	* all modules are initialized here
	*
	*
	*/
	
	_self = {
		debug:  !!bam.env.host["isBeta"] || !!bam.env.host["isQA"] || !!bam.env.host["isDev"],
		
		narrowband: "/mlb/homepage/narrowband.jsp",
		
		// setter/getter
		set: function(key,value){
			if(key && typeof(value)!=="undefined") {
				_properties[key]=value;
			}
			_log("setting property: " + key + ". Displaying _properties below:");
			_log(_properties);
		},
		get: function(key) {
			_log("getting " + key);
			return (key && key in _properties)?_properties[key]:null;
		},
		
		
		init: function(){
			_track();							
			_videoModule.init();
			_navbuttons();
			 sysdate = typeof(sysdate)!=="undefined"?sysdate:bam.datetime.DateTime().toYMD();
			_headlines.init();
			_createCarousel({
				container: $("#h_spotlight"), 
				maxItems: 3
			});
			_miniStandings.init(sysdate, false);			
			var initiatives = $("#h_initiatives .carousel_container li").length;
			_createCarousel({
				container: $("#h_initiatives"), 
				maxItems: initiatives
			});				
			_photogalleries.init();
			//BTSModule.init();
			// 30 minute refresh
			setTimeout(function(){document.location.reload(true);},1800000);		
			delete _self.initPostseason;
			
					
		},

		getClubProperties: function() {
			var out = this.get('clubprops');
			if(!out) {
				$.ajax({ 
					async: false, 
					url: "/scripts/club_properties.jsp", 
					dataType: 'json', 
					data: { responseType: "json" }, 
					success: function(json){ 
						out = json;
						bam.mlbhome.set("clubprops",json); 
					}, 
					error: function(xhr) {_log("Error accessing club properties: " + xhr.status + " " + xhr.statusText);} 
				});
			}
			return out;
		}
	};
	

	$.bindable(_self, "onPageLoad");
	_self.bind("onPageLoad", _self.init);

	
	return _self;			

})(jQuery);



//Lineups popModule
bam.showLineup = function(g){
	bam.load(bam.homePath+"bam.popModule.js", function(){
		//convert gameID format to gameday link	
		if ( ~g.indexOf("/") && ~g.indexOf("-") ) {
			g = g.replace(/\//g,"_");
			g = g.replace(/-/g,"_");
		}
		var newHTML  = "<iframe frameborder='0' width='725' height='438' scrolling='no' src='/mlb/lineups/index.jsp?game_id="+g+"'></iframe>";
		bam.popModule.init({
			css        : "/mlb/lineups/lineups.popModule.css", // this is the default css, but custom css can be set here like so
			overlayCss : "/shared/css/bam/bam.overlay.css"    // (same as above)
		});
		bam.popModule.show({
			htmlContent    : newHTML, 
			width          : 725, // default is 500 (set in CSS file)
			height         : 438,
			overlayOpacity : 0.8,
			preShow        : function(){},
			postShow       : function(){}
		});
	});
};



/*
//BTS Module
bam.mlbhome.one("onPageLoad", function() {
	if(typeof(BTSModule) !== "undefined") {
		BTSModule.init({tooltipPosition:"topRight"});
	}
});
*/


$(function() {
	//bam.mlbhome.init();
	bam.mlbhome.trigger("onPageLoad");	
});
