var Booking = Booking || {};

Booking.slider = 
{
    setup: function () {
        Booking.slider.items = $('#strip-wrapper div.strip-item');
        Booking.slider.index = 0;
        
        $('#strip-wrapper').serialScroll({
            items: 'div.strip-item',
            duration: 800,
            axis: 'x',
            cycle: false,            
            force: false,
            constant: false,
            lazy: true
        });
        
        $("a.prev").click(function(evt) {
            Booking.slider.index -= 1;
            if (Booking.slider.index < 0)
                Booking.slider.index = 0;
            else
                $("#strip-wrapper").trigger('goto', [Booking.slider.index]);
        });
        
        $("a.next").click(function(evt) {
            var count = $('#strip-wrapper div.strip-item').length - 1;
            Booking.slider.index += 1;
            if (Booking.slider.index > count)
                Booking.slider.index = count;
            else
                $("#strip-wrapper").trigger('goto', [Booking.slider.index]);
        });
        
        $(".scrollto-control .month").click(function(evt) {
            var item = $($(this).attr("id").replace(/strip-control-/, "#strip-item-"));
            // scrollTo is extremely sensible to passing non-existent values: make sure
            // to continue only if the item exists.
            if (!item.length) return;
            Booking.slider.index = parseInt(item.attr("name").replace(/strip-item-/, ""));
            $("#strip-wrapper").trigger('goto', [Booking.slider.index]);
        });

        Booking.slider.filter(null);
    },

    filter: function(s) {
        function setupEvents(item) {
            item.hover(
                function (evt) {
                    $(".bookit", this).hide();
                    $(".buttons", this).show();
                    $(".title", this).fadeIn(500);
                    $(".bookit", this).fadeIn(500);
                },
                function (evt) {
                    $(".title", this).fadeOut(500);
                    $(".strip-item .buttons").hide();            
                }
            );
        }
        
        var strip = $("#strip-events");
        strip.find("div.strip-item").appendTo($("#strip-events-store"));
        
        if (s) {
            var regexp = new RegExp(s, "gi");
            Booking.slider.items.each(function(i) {
                if (regexp.test(this.innerHTML)) {
                    strip.append(this);
                    setupEvents($(this));
                }
            });
            $(".scrollto-control").slideUp();
        }
        else {
            Booking.slider.items.each(function(i) {
                strip.append(this);
                setupEvents($(this));
            });
            $(".scrollto-control").slideDown();

        }
        Booking.slider.index = 0;
        $("#strip-wrapper").trigger('goto', [0]);
    }
};