﻿var fbdCarousel = null;
var dataFile = 1;

function fbdcarousel_initCallback(carousel) {
    fbdCarousel = carousel;

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function fbdcarousel_reload() {
    dataFile++;
    if (dataFile > 9)
        dataFile = 1;
    fbdCarousel.reset();
}

function fbdcarousel_itemLoadCallback(carousel, state) {
    // Since we get all URLs in one file, we simply add all items
    // at once and set the size accordingly.
    if (state == 'init')
    {
        jQuery.get('CarouselList' + dataFile + '.txt', function(data) {
            fbdcarousel_itemAddCallback(fbdCarousel, fbdCarousel.first, fbdCarousel.last, data);
            });
    }
    if (state == 'next') 
    {
        if (carousel.last == carousel.noItems)
            setTimeout(fbdcarousel_reload,10);
    }
};

function fbdcarousel_itemAddCallback(carousel, first, last, data) {
    // Simply add all items at once and set the size accordingly.
    var items = data.split('|');

    for (i = 0; i < items.length; i++) {
        carousel.add(i + 1, fbdcarousel_getItemHTML(items[i]));
    }
    
    carousel.noItems = items.length;
    carousel.size(items.length);
};

/**
* Item html creation helper.
*/
function fbdcarousel_getItemHTML(urls) {
    var url = urls.split(',');
    return '<a href="' + url[1] + '"><img src="' + url[0] + '" width="75" height="75" alt="" /></a>';
};
