$(document).ready(function() {
    
    scrollable = $('#slider .items');
    itemsCount = $('#slider .slider-item').length;
    itemHeight = 38;
    
    $('#slider .slider-item').click(function(){
        scrollIndex = $(this).attr('count');
        seekTo(scrollIndex);
    });
    
    $('#button-down').click(function(){
        scrollIndex++;
        scrollIndex = (scrollIndex > itemsCount) ? 1 : scrollIndex;
        seekTo(scrollIndex);
    });
    
    $('#button-up').click(function(){
        scrollIndex--;
        scrollIndex = (scrollIndex < 1) ? itemsCount : scrollIndex;
        seekTo(scrollIndex);
    });
    
    $('.clear-timer').click(function(){
        if (t)
        {
            clearTimeout(t);
        }
    });
    
    autoPlayScroll();
    
});

var subscriptions = new Array();
var scrollable;
var itemsCount;
var itemHeight;
var t;
var scrollIndex = 0;

function seekTo(position)
{
    proceed = false;
    topValue = Number((position - 2) * itemHeight);
    topValue = 0 - topValue;
    
    if (position == 1)
    {
        topValue = 0;
        proceed = true;
    }
    else if (position == itemsCount)
    {
        topValue = 0 - Number((itemsCount - 3) * itemHeight);
        proceed = true;
    }
    
    if ((position > 1 && position < itemsCount) || proceed == true)
    {
        scrollable.animate(
            {
                top: topValue
            },
            {
                queue: false,
                duration: 500
            }
        );
    }
    
    $('#slider .slider-item').removeClass('active');
    $('#slider .slider-item:nth-child(' + position + ')').addClass('active');
    doShowSubscriptionInfo();
}

function autoPlayScroll()
{
    scrollIndex++
    scrollIndex = (scrollIndex > itemsCount) ? 1 : scrollIndex;
    seekTo(scrollIndex);
    t = setTimeout("autoPlayScroll()", 5000);
}
