﻿window['dvl'] = window['dvl'] || {};
window['dvl']['billboard'] = window['dvl']['billboard'] || {};

// Billboard module
(function (billboard, $, undefined) {
    // private properties
    var outerWidth = 0;
    var itemCount = 0;
    var intervalID = -1;

    // public function to start the carousel
    billboard.start = function (targetSelector, delay, scrollDuration) {
        // store the element we'll be sliding around
        var slideElement = $(targetSelector + '>ul');

        // set member vars
        outerWidth = $(targetSelector).width();
        itemCount = slideElement.children().length;

        // carry out carousel transition every [delay] milliseconds
        intervalID = setInterval(function () {
            // get the current position
            var currentPos = slideElement.css('left');
            currentPos = currentPos.replace('px', '');
            var totalWidth = -(itemCount - 1) * outerWidth;
            // if the current position is less than overall width
            if (currentPos === 'auto' || currentPos > totalWidth) {
                // do normal scroll to right
                slideElement.animate({
                    left: '-=' + (outerWidth + 4)
                }, scrollDuration);
            }
            else {
                // scroll back to start
                slideElement.animate({
                    left: 0
                }, scrollDuration);
            }

        }, delay);
    }; // end of carousel.start
} (window['dvl']['billboard'], jQuery));
