/* *************************************************
* newsTicker
*
* syntax:
* newsTicker(containerId, newsItemId, animationTimeInMilliseconds, easingMethod, delayBetweenInMilliseconds);
*
************************************************** */

function scrollNewsTicker() {
    var $thisNewsTickerContainer = $('#' + newsTickerSettings.id);
    var currentPosition = $thisNewsTickerContainer.scrollTop();
    var easeIn = 'easeIn'+newsTickerSettings.easingMethod;
    var easeOut = 'easeOut'+newsTickerSettings.easingMethod;
    
    if (newsTickerLoopCount === 'undefined') {
        newsTickerLoopCount = 0;
    }
    
    $thisNewsTickerContainer.animate({
        scrollTop: currentPosition+(newsTickerSettings.amount/2)
    }, newsTickerSettings.duration, easeIn).animate({
        scrollTop: currentPosition+newsTickerSettings.amount
    }, newsTickerSettings.duration, easeOut, function() {
        newsTickerLoopCount++;
        
        if (newsTickerLoopCount > ((newsTickerSettings.originalBlurbCount*5)-2)) {
            setTimeout("resetNewsTicker()", newsTickerSettings.duration);
        } else {    
            if (newsTickerSettings.addElement) {
                
                // add the one just moved off screen to the end
                $('#' + newsTickerSettings.id + ' '+ newsTickerSettings.blurbContainer).slice(newsTickerLoopCount-1, newsTickerLoopCount).clone().appendTo('#' + newsTickerSettings.id);
            }
            
            // call the next display
            nextScrollNewsTicker = setTimeout("scrollNewsTicker()", newsTickerSettings.delayBetween);
        }
    });
}

function resetNewsTicker() {
    $('#' + newsTickerSettings.id).scrollTop(0);
    newsTickerLoopCount = 0;
    newsTickerSettings.addElement = false;
    nextScrollNewsTicker = setTimeout("scrollNewsTicker()", newsTickerSettings.delayBetween);
}

function newsTicker(id, blurbContainer, dur, easingMethod, delayBetween) {            
    var $newsTickerBlurbs = $('#' + id + ' ' + blurbContainer);
    $newsTickerContainer = $('#' + id);
    
    $('#' + id).scrollTop(0);
    
    newsTickerSettings = {
        "id": id,
        "blurbContainer": blurbContainer,
        "amount": $newsTickerBlurbs.height(),
        "duration": dur,
        "easingMethod": easingMethod,
        "delayBetween": delayBetween,
        "originalBlurbCount": $newsTickerBlurbs.length,
        "addElement": true
    };
    nextScrollNewsTicker = '';
    
    $newsTickerContainer.hover(
        function() {
            clearTimeout(nextScrollNewsTicker);
        },
        function() {
            clearTimeout(nextScrollNewsTicker);
            nextScrollNewsTicker = setTimeout("scrollNewsTicker()", newsTickerSettings.delayBetween);
        }
    );
    
    newsTickerLoopCount = 0;            
    nextScrollNewsTicker = setTimeout("scrollNewsTicker()", newsTickerSettings.delayBetween);
}
