// Events Carousel 
$(".sig_carousel_events").jCarouselLite({
	btnNext: "#sig_carousel_button_events_right",
	btnPrev: "#sig_carousel_button_events_left",
	circular: false,
	visible: 4,
	scroll: 4,
	afterEnd: disableLeftButtonEvents
});

// Offers Carousel 
$(".sig_carousel_offers").jCarouselLite({
	btnNext: "#sig_carousel_button_offers_right",
	btnPrev: "#sig_carousel_button_offers_left",
	circular: false,
	visible: 3,
	scroll: 3,
	afterEnd: disableLeftButtonOffers

});
// Seasonal Carousel
$(".sig_carousel_seasonal").jCarouselLite({
	btnNext: "#sig_carousel_button_seasonal_right",
	btnPrev: "#sig_carousel_button_seasonal_left",
	circular: false,
	visible: 3,
	scroll: 3,
	afterEnd: disableLeftButtonSeasonal
});

// Cards Carousel
$(".sig_carousel_cards").jCarouselLite({
	btnNext: "#sig_carousel_button_cards_right",
	btnPrev: "#sig_carousel_button_cards_left",
	circular: false,
	visible: 4,
	scroll: 4,
	afterEnd: disableLeftButtonCards
});


$(function(){

	// Hover state for offer border color
	$(".sig_carousel_offers a, .sig_carousel_seasonal a").hover(
      function () {
        $(this).children("img").addClass("hover");
      }, 
      function () {
        $(this).children("img").removeClass("hover");
      }
    );
		
	// Disable Buttons when not enough offers are showing
	if (($(".sig_carousel_events li").length) < 5){
		$("#sig_carousel_button_events_left, #sig_carousel_button_events_right").addClass("disabled");
	};
	if (($(".sig_carousel_offers li").length) < 4){
		$("#sig_carousel_button_offers_left, #sig_carousel_button_offers_right").addClass("disabled");
	};
	if (($(".sig_carousel_seasonal li").length) < 4){
		$("#sig_carousel_button_seasonal_left, #sig_carousel_button_seasonal_right").addClass("disabled");
	};
	if (($(".sig_carousel_cards li").length) < 4){
		$("#sig_carousel_button_cards_left, #sig_carousel_button_cards_right").addClass("disabled");
	};
	
		
	// Cards carousel - turn on background image if javascript is enabled
	$(".sig_carousel_cards_container").addClass("bgimage");
	
	disableLeftButtonEvents();
	disableLeftButtonOffers();
	disableLeftButtonSeasonal();
	disableLeftButtonCards();
});

// Disable Left Button
function disableLeftButtonEvents(){
	if (($(".sig_carousel_events ul").css("left")) == "0px"){
			$("#sig_carousel_button_events_left").addClass("disabled");
		}
		else{
			$("#sig_carousel_button_events_left").removeClass("disabled");
		};
	disableRightButtonCheck("events", 4);
};
function disableLeftButtonOffers(){
	if (($(".sig_carousel_offers ul").css("left")) == "0px"){
			$("#sig_carousel_button_offers_left").addClass("disabled");
		}
		else{
			$("#sig_carousel_button_offers_left").removeClass("disabled");
		};
	disableRightButtonCheck("offers", 3);
};
function disableLeftButtonSeasonal(){
	if (($(".sig_carousel_seasonal ul").css("left")) == "0px"){
			$("#sig_carousel_button_seasonal_left").addClass("disabled");
		}
		else{
			$("#sig_carousel_button_seasonal_left").removeClass("disabled");
		};
	disableRightButtonCheck("seasonal", 3);
};
function disableLeftButtonCards(){
	if (($(".sig_carousel_cards ul").css("left")) == "0px"){
			$("#sig_carousel_button_cards_left").addClass("disabled");
		}
		else{
			$("#sig_carousel_button_cards_left").removeClass("disabled");
		};
	//commented out disableRightButtonCheck call back on Cards Carousel Oct. 19 2009 CLM	
	//disableRightButtonCheck("cards", 4);
};

// Additional check to disable the right button - used because the carousel plugin gets confused with nested lists
function disableRightButtonCheck(carouselName, numberVisible) {
	var carousel = $(".sig_carousel_" + carouselName + " ul");
	
	if(carousel.length > 0) {
		// Find the carousel item that would be the next visible when pressing the right button
		var nextItemPosition = Math.floor(Math.abs(carousel.css("left").replace("px", "")) / (530 / numberVisible)) + numberVisible;
		var nextItem = $(".sig_carousel_" + carouselName + " ul > li:eq(" + nextItemPosition + ")");

		// If there is no next carousel item or it's a dummy item with no children then we should be disabling
		if(nextItem.length < 0 || nextItem.children().length == 0) {
			$("#sig_carousel_button_" + carouselName + "_right").addClass("disabled");
		} else {
			$("#sig_carousel_button_" + carouselName + "_right").removeClass("disabled");
		}
	}
}
