(function ($) {


afterLoad("initializing slideshow and carousels", function () {
    $("div#slides").cycle({
        fx: "fade",
        timeout: 6000,
        delay: 1000,
        before: function (currentSlide, nextSlide, options, forwardFlag) {
            $(this).children("div.slide-caption-container").css({
                opacity: 0
            });
            setTimeout(function () {
                $(nextSlide).children("div.slide-caption-container").animate({
                    opacity: 1
                }, 1000);
            }, 500);
        },
        after: function (currentSlide, nextSlide, options, forwardFlag) {
            var $this = $(this);
            $this.siblings().removeClass("current-slide");
            $this.addClass("current-slide");
        },
        pager: "#pager"
    });

    $("img.caption").each(function () {
        var $this = $(this);
        
        if ($this.outerHeight() === 0) {
            $this.load(centerVertical);
        } else {
            centerVertical();
        }
        
        function centerVertical() {
            var captionTop = $this.outerHeight() / 2;
            var linkTop = captionTop + 50;
            $this.css({
                top: '50%',
                marginTop: '-' + captionTop + 'px'
            });
            $this.siblings('a.caption-link').css({
                top: '50%',
                marginTop: linkTop + 'px'
            });
        }
    });
    
    if ($("#recipes-we-love ul.carousel").children().length > 3) {
        $("#recipes-we-love ul.carousel").bxSlider({
            displaySlideQty: 3, moveSlideQty: 3, pager: false,
            prevSelector: '#recipes-we-love div.carousel-left', 
            nextSelector: '#recipes-we-love div.carousel-right',
            prevImage: '/media/images/carousel-arrow-left.png',
            nextImage: '/media/images/carousel-arrow-right.png',
            nextText: '', prevText: ''
        });
    }
    
    if ($("#featured-products ul.carousel").children().length > 3) {
        $("#featured-products ul.carousel").bxSlider({
            displaySlideQty: 3, moveSlideQty: 3, pager: false,
            prevSelector: '#featured-products div.carousel-left', 
            nextSelector: '#featured-products div.carousel-right',
            prevImage: '/media/images/carousel-arrow-left.png',
            nextImage: '/media/images/carousel-arrow-right.png',
            nextText: '', prevText: ''
        });
    }
});

})(jQuery);
