﻿var lbTimer = null;

$(function() {
    initLiveboard();
});

function initLiveboard() {
    $('div.lbButton:first').addClass('active');

    if ($('div.lbItem').length > 1) {
        $('div.lbButton').click(function() {
            // Alleen als er geen items midden in een animatie zitten
            if (!$('div.lbItem').is(':animated')) {
                // ID bepalen van de geklikte item
                var id = $(this).data('itemid');

                if (id !== $('div.lbItem:first').data('itemid')) {
                    $('div.lbButton').removeClass('active');
                    $(this).addClass('active');
                    liveboardScrollTo(id);
                }
            }
        });

        lbTimer = setTimeout(nextLiveboardItem, 5000);
    }
}

// Scrollt naar het volgende item in het liveboard
function liveboardScrollTo(id) {
    if (lbTimer !== null) {
        // Timer opnieuw starten
        clearTimeout(lbTimer);
        lbTimer = setTimeout(nextLiveboardItem, 5000);
    }
    
    var showItem = $('div.lbItem[data-itemid="' + id + '"]');
    showItem.prependTo('div#lbItems');
    showItem.css('margin-top', -321);

    showItem.animate({
        'margin-top': 0
    }, 'slow');
}

// Selecteert het volgende item in de liveboard
function nextLiveboardItem() {
    var current = $('div.lbButton.active');
    var next = $('div.lbButton.active').next();
    if (next.length === 0) {
        next = $('div.lbButton:first');
    }

    next.click();
}

