$(document).ready(function() {

    // ------------------- FUNCTIONS ----------------------
    // show/hide info and links on hero mouseover
    function CarouselHover() {
        $(".carousel_horizontal li").hover(function() {
            $(this).addClass("hover");
            $(".promotion_info").slideDown();
            $(".promotion_links").fadeIn();
        }, function() {
            $(this).removeClass("hover");
            $(".promotion_info").slideUp();
            $(".promotion_links").fadeOut("slow");
        });
    }

    // --------------- PAGE LOAD EVENTS -------------------

    // hide hero info and links panels
    $(".promotion_info").hide();
    $(".promotion_links").hide();

    // set up hero hover
    CarouselHover();

    // does the carousel have any id's - might be a vanilla hero (on the homepage)
    if ($(".carousel_horizontal li").length) {
        if ($(".carousel_horizontal li").attr("id").length <= 0) {

            // no, so lets add some
            var total = $(".carousel_horizontal li").length;
            
            // for each li;
            $(".carousel_horizontal li").each(function(i) {

                // grab references
                var li = $(this);
                var prev = li.find("a.carousel_prev");
                var next = li.find("a.carousel_next");

                // add id to promo 
                li.attr("id", "promotion" + i);

                // add hrefs

                if (i == 0) {
                    prev.remove();
                }

                if (i > 0) {
                    prev.attr("href", "#promotion" + (i - 1));
                }

                if (i < total - 1) {
                    next.attr("href", "#promotion" + (i + 1));
                }

                if (i == total - 1) {
                    next.remove();
                }

            });

        }
    }

    // --------------- USER INITIATED EVENTS --------------

    // for those with JS, make the whole item clickable
    $(".carousel_horizontal li .promotion_info").click(function() {
        var ph_Url = $(this).find(".promotion_btn").attr("href");
        document.location.href = ph_Url;
    });

    // hero offer scrolling
    // set default axis
    $.localScroll.defaults.axis = 'x';

    // Scroll initially if there's a hash (#something) in the url 
    $.localScroll.hash({
        target: '.carousel_horizontal', // Could be a selector or a jQuery object too.
        queue: true,
        duration: 1500
    });


    $(".promotion_links").localScroll({
        target: '.carousel_horizontal', // could be a selector or a jQuery object too.
        queue: true,
        duration: 1000,
        hash: true,
        onBefore: function(e, anchor, $target) {
            $(".carousel_horizontal li").unbind();
        },
        onAfter: function(anchor, settings) {
            CarouselHover();
        }
    });

});