﻿function startAuto(carousel){
    if (typeof (carousel.autoTimer) != 'undefined') window.clearTimeout(carousel.autoTimer);
    carousel.autoTimer = window.setInterval(function() { carousel.next(); }, 2000);
}
function stopAuto(carousel){
    window.clearInterval(carousel.autoTimer);
}
function loadCarousel(id){
    $('#' + id).jcarousel({
        scroll: 1,
        wrap: 'circular',
        initCallback: function(carousel) {
            var autoStopped = false;
            // Disable autoscrolling if the user clicks the prev or next button.
            carousel.buttonNext.bind('click', function() {
                stopAuto(carousel);
                autoStopped = true;
            });

            carousel.buttonPrev.bind('click', function() {
                stopAuto(carousel);
                autoStopped = true;
            });

            // Pause autoscrolling if the user moves the cursor over the clip.
            carousel.clip.hover(function() {
                stopAuto(carousel);
            }, function() {
                if (autoStopped == false)
                    startAuto(carousel);
            });
            startAuto(carousel);
            window.setTimeout(function() { stopAuto(carousel); }, 120000);
        },
        itemVisibleInCallback: {
            onBeforeAnimation: function(carousel, item, i, state, evt) {
                if ($(item).html() != '')
                    return;

                var lis = carousel.list.find('li');

                var nextItem = $(lis[lis.length - carousel.options.size - 1]);
                if (state == 'prev') {
                    nextItem = $(lis[carousel.options.size - 1]);
                } else {
                }
                $(item).html(nextItem.html());
            }
        }
    });
}
