/*=================================
	Home page hero banner JS
==================================*/

$(document).ready(function() {

	//get the links ready
	$("#heroNav span").each( function() {
		$(this).mouseover(function() {
			var id = $(this).attr("id");
			activateHeroLink(id,1);				   
		});
	});
	
	//summer savings box
	$('#summerSavingsProds').jcarousel({
	   scroll:1,
	   animation:"slow",
	   start:1
	});	

	//holiday box
	$('#holidayPromosProds').jcarousel({
	   scroll:1,
	   animation:"slow",
	   start:1
	});	
	
	//auto rotate the links
	$.t = setTimeout("heroAutoRotate()",3500);
	
	//expansion boxes
	$("#homePromoBoxes .homePromoBox").each( function() {
		var id = $(this).attr("id");
		$("span",this).click(function() {
			if( $("#"+id + " .promoBoxTitle").hasClass("opened")) {
				$("#"+id + " .promoBoxTitle").removeClass("opened");
				$(this).removeClass("contract");					
				$(".neverMiss",this).hide();
				$("#"+id + " .promoBoxContent").hide();
				$("#"+id + " .promoBoxBottom").hide();
			} else {
				//perform any other options specific to this box
				$("#"+id + " .promoBoxTitle").addClass("opened");
				$(this).addClass("contract");
				$("#"+id + " .promoBoxTitle div").show();
				$(".neverMiss",this).show();
				$("#"+id + " .promoBoxContent").show();
				$("#"+id + " .promoBoxBottom").show();
				doPromoBoxAction(id);
			}
		});
	});
});

//active the link and move the image
var activateHeroLink = function(id,stopTimer) {
	removeHeroActiveLinks();
	$("#"+id).addClass("active");
	showHeroImage(id);	
	if(stopTimer == 1) {
		clearTimeout($.t);	
	}
}

//switch to the correct hero image base on click
var showHeroImage = function(id) {
	$("#heroRotate div.rotator").each( function() {
		$(this).hide();								   
	});
	$("#"+ id + "Img").show();
}

//removes any active state links (orange bg)
var removeHeroActiveLinks = function() {
	$("#heroNav span").each( function() {
		$(this).removeClass("active");						   
	});
}

//auto rotate through the headers until there is a click
var heroAutoRotate = function() {
	//detect the active link and move forward
	var id = $("#heroNav .active").next('span').attr("id");
	if(!id) {
		var id = 'moreSavings';	
	}
	activateHeroLink(id,0);
	$.t = setTimeout("heroAutoRotate()",3500);
}

//handles the promo box-specific actions
//@param the id of the box set
var doPromoBoxAction = function(id) {
	switch(id) {
		case "summerSavings":
			//summer savings carousel
			break;
	 	case "recentProducts":
			//recently added
			$('#recentProds').jcarousel({
			   scroll:1,
			   visible:false,
			   animation:"slow",
			   start:1
			});		
			break;
		case "holidayPromos":
			//holiday
		default:
			return false;
	}
}
								