var CBLSDPagination;
(function ($) {
    CBLSDPagination = {
        speed: 500,
        easing: 'swing',

        move: function () {
            var self = this;

            this.pageContainer.animate({
                left: 0 - (this.current * $(this.pages[0]).width()) + "px"
            }, {
                speed: this.speed,
                easing: this.easing,
                complete: function () {
                    self.updatePages();
                }
            });
        },

        goTo: function (ind) {
            this.current = Math.min(0, Math.max(this.total - 1, ind));
            this.move();
        },

        next: function () {
            this.current = Math.min(this.total - 1, this.current + 1);
            this.move();
        },

        prev: function () {
            this.current = Math.max(0, this.current - 1);
            this.move();
        },

        updatePages: function () {
            if (this.current === 0) {
                this.prevBtn.addClass('disabled');
            } else {
                this.prevBtn.removeClass('disabled');
            }
            if (this.current == this.total - 1) {
                this.nextBtn.addClass('disabled');
            } else {
                this.nextBtn.removeClass('disabled');
            }
        },

        initialize: function () {
            var self = this;

            this.pageContainer = $('#slidedeck_cbl-sd .navigation .pages');
            this.pages = $('#slidedeck_cbl-sd .navigation .page');
            this.prevBtn = $('#slidedeck_cbl-sd .navigation a.btn_examples_prevnext.prev');
            this.nextBtn = $('#slidedeck_cbl-sd .navigation a.btn_examples_prevnext.next');

            this.total = this.pages.length;
            this.current = 0;

            this.prevBtn.click(function (event) {
                event.preventDefault();
                self.prev();
            });
            this.nextBtn.click(function (event) {
                event.preventDefault();
                self.next();
            });

            this.updatePages();
        }
    };

    $(document).ready(function () {
        if (document.location.pathname.substr(0, 9) == "/examples") {
            var gotoSlide = document.location.search.match(/\?hash=([a-zA-Z0-9\-_]+)/);
            if (gotoSlide !== null) {
                window.scrollTo(0, 0);
                CBLSlideDeck.goTo($('#slidedeck_cbl-sd dd').index($('#' + gotoSlide[1])));
            }

            var ssHeight = $('.slidedeck dd .screenshot').height();
            $('.slidedeck dd .screenshot img').each(function () {
                $(this).css({
                    marginTop: ((ssHeight - $(this).height()) / 2)
                });
            });
        }

        $('#slidedeck_cbl-sd .navigation a.thumb').click(function (event) {
            if ($('#slidedeck_cbl-sd .navigation').hasClass('index')) {
                event.preventDefault();
                var example_id = this.href.split("#")[1];
                CBLSlideDeck.goTo($('#slidedeck_cbl-sd dd').index($('#' + example_id)) + 1);
                $(this).addClass('active').find('span.complogo').addClass('active');
            }
        });

        CBLSDPagination.initialize();
    });
})(jQuery);
