// Create club homepage namespace
var $doc = $(document),
	ClubHome = (typeof ClubHome=="object") ? ClubHome : {},
	nextbutton = "#promotions_next",
	prevbutton = "#promotions_prev",
	theClub = bam.url.Location( window.location ).getParam( 'c_id' ) || "MLB";

theClub = theClub.toUpperCase();
ClubHome.debugMode=false;
ClubHome.log = function(msg){ if (typeof console!="undefined" && ClubHome.debugMode){ console.log("ClubHome: " + msg); }}

// 30 minute refresh
setTimeout(function(){document.location.reload(true);},1800000);




/*
* TRACKING
*/

// next button
$doc.selector = nextbutton;  
$doc.live("click", function(){
	bam.tracking.track({
		async:{
			isDynamic:true,
			compName:theClub + " Homepage Promotions",
			compActivity:"Next",
			actionGen:true				
		}
	});
});

// prev button
$doc.selector = prevbutton;  
$doc.live("click", function(){
	bam.tracking.track({
		async:{
			isDynamic:true,
			compName:theClub + " Homepage Promotions",
			compActivity:"Previous",
		actionGen:true
		}
	});
});


// pagination
$doc.selector = "#promotions_pagination a";  
$doc.live("click", function(){
	var page = $(this).attr("class").substring(1);
	bam.tracking.track({
		async:{
			isDynamic:true,
			compName:theClub + " Homepage Promotions",
			compActivity:"Navigation " + page + " Click",
			actionGen:true
		}		
	});	
});


// content
$doc.selector = "#promotions li a";  
$doc.live("click", function(){
	var panel="", listItems = $("#promotions .promotionsLI"), parentListItem=$(this).parents("li.promotionsLI");		
	if(listItems.index(parentListItem)==-1){
		//map cloned items
		listItems = $("#promotions .isClone");
		parentListItem=$(this).parents("li.isClone");
		panel=listItems.index(parentListItem);
		panel=(panel<3)?panel+10:panel-2;  
	}else{
		panel=listItems.index(parentListItem)+1;
	}	
	bam.tracking.track({
		async:{
			isDynamic:false,
			compName:theClub + " Homepage Promotions",
			compActivity:"Panel " + panel + " Click",
			actionGen:true,
			mwContentId:parentListItem.find(".promotions_contentID").text()
		}
	});
	return true;
});



$(document).ready(function(){		
	// turned off for offseason
	// ClubHome.initHomestand();	
	

	//Initialize promotions carousel
	$("#promotions_wrapper")
		.find("#promotions_pagination a:first")
		.addClass("pagOn")
		.end()	
		.find(nextbutton)
		.hover(
			function(){ $(this).addClass("nextOn"); },
			function(){ $(this).removeClass("nextOn"); }
		)
		.end()	
		.find(prevbutton)
		.hover(
			function(){ $(this).addClass("prevOn"); },
			function(){ $(this).removeClass("prevOn"); }
		)
		.end()
		.find("#promotions").jCarouselLite({
   	 		btnNext: nextbutton,
    		btnPrev: prevbutton,
			btnGo: ["#promotions_pagination .p1","","","#promotions_pagination .p2"],
			scroll:3,
			visible:3,
			circular:true,
			speed: 600,
			beforeStart: function(){
				$("#promotions_pagination a").removeClass("pagOn");
			},		
			afterEnd : function(item, isAutoScroll){										
				var allItems = item.parent().children(),
					allNavItems = $("#promotions_pagination a"),
					itemNum = allItems.index(item)/3;  // # of items per "page"
				itemNum--;			
				if(itemNum >= allNavItems.length)itemNum=0;
				else if(itemNum<0)itemNum=allNavItems.length-1;					
				$("#promotions_pagination a:eq("+itemNum+")").addClass("pagOn");							
			}			
		})
		.end()
		.css("visibility","visible");	
	
});

