WR = window.WR || {};
WR.bogushtime = WR.bogushtime || {};

WR.bogushtime.index = {
    init: function() {
        this.initMainSlider();
        this.initGuideWidget();
        this.initClientList();
    },

    initMainSlider: function() {
        // inited as list widget inside 'v3/pages/index/list_widget/widget.html' template
    },

    initGuideWidget: function() {
        $('#guide-widget ul li input').click(function() {
            $(this).parent()[this.checked ? 'addClass' : 'removeClass']('bt-active');
        });
        $('#guide-widget ul li input').removeAttr("checked");
        $('#guide-widget').hover(
            function() {
                $(this).find('.bt-goals .bt-content').stop(true).animate({ top: 0 });
            },
            function() {
                $(this).find('.bt-goals .bt-content').stop(true).animate({ top: $(this).height() });
            }
        );
        // Inint submit to guide action
        $('#submit-goals').click(function(e) {
            e.preventDefault();
            var url = '/trainings/open/?guide=yes';
            $('input:checkbox:checked[name=goal]').each(function() {
                url += '&goal=' + this.value;
            });
            location.href = url;

        })
    },

    initClientList: function() {
        var clients = $('#main-clients'),
            list = clients.find('ul'),
            items = list.find('li');

        // Adjust logos in one line
        var totalWidth = 0;
        items.each(function() {
            totalWidth += $(this).outerWidth(true);
        });
        list.width(totalWidth);

        // Calculate pages offset
        var displayWidth = list.parent().width(),
            pages = [0];
        items.each(function() {
            var item = $(this),
                left = item.position().left,
                width = item.width();
            if (left % displayWidth + width > displayWidth)
                pages.push(left);
        });

        // Page flip function
        var currentPage = 0;
        function slideTo(pageNum) {
            if (pageNum === '--')
                pageNum = currentPage - 1;
            if (pageNum === '++')
                pageNum = currentPage + 1;
            if (pageNum >= 0 && pageNum < pages.length) {
                currentPage = pageNum;
                list.stop(true).animate({
                    marginLeft: -pages[pageNum]
                });
                updateNavButtons();
            }
        }

        // Enable buttons
        var leftBtn = clients.find('.bt-big-left-btn'),
            rightBtn = clients.find('.bt-big-right-btn');
        leftBtn.click(function(e) { e.preventDefault(); slideTo('--'); });
        rightBtn.click(function(e) { e.preventDefault(); slideTo('++'); });

        // Show / hide buttons
        function updateNavButtons() {
            leftBtn[currentPage == 0 ? 'hide' : 'show']();
            rightBtn[currentPage == pages.length - 1 ? 'hide' : 'show']();
        }
        updateNavButtons();
    }
};

$(function() {
    WR.bogushtime.index.init();
});

