$(document).ready(function() {
    
    scrollable = $('.subscription-countries');
    itemsCount = $('.subscription-countries .subscription-item').length;
    jspPane = $('.jspPane');
    itemHeight = 28;
    
    $('.subscription-item').click(function(){
        tmpScrollIndex = scrollIndex;
        scrollIndex = $(this).attr('count');
        direction = 'down';
        if (tmpScrollIndex > scrollIndex)
        {
            direction = 'up';
        }
        seekTo(scrollIndex, direction);
    });
    
    $('#btn_next_country').click(function(){
        level++;
        scrollIndex++;
        scrollIndex = (scrollIndex > itemsCount) ? 1 : scrollIndex;
        seekTo(scrollIndex, 'down');
        return false;
    });
    
    $('#btn_prev_country').click(function(){
        level--;
        scrollIndex--;
        scrollIndex = (scrollIndex < 1) ? itemsCount : scrollIndex;
        seekTo(scrollIndex, 'up');
        return false;
    });
    
    $('.clear-timer').click(function(){
        if (t)
        {
            clearTimeout(t);
        }
    });
    
    autoPlayScroll();
    
});

var jspPane;
var scrollable;
var itemsCount;
var itemHeight;
var t;
var scrollIndex = 0;
var level = 1;

function seekTo(position, direction)
{
    proceed = false;
    topValue = Number((position - 1) * itemHeight);
    originalLevel = level;
    
    if (level > 4)
    {
        level = 4;
    }
    else if (level < 1)
    {
        level = 1;
    }
    
    if (
        (position > 3 && direction == 'down') ||
        (direction == 'down' && topValue == 0) ||
        (direction == 'up')
    )
    {
        topValue -= (itemHeight * (level - 1));
        animateEnd = false;
        
        if (originalLevel < 1 && direction == 'up')
        {
            level = 4;
            topValue = (itemsCount - 4) * itemHeight;
            animateEnd = true;
        }
        
        if (originalLevel > 4 && direction == 'down')
        {
            level = 1;
            topValue = 0;
            animateEnd = true;
        }
        
        if (animateEnd)
        {
            $('.jspPane').animate(
                {
                    top: topValue * -1
                },
                {
                    queue: false,
                    duration: 500
                }    
            );
        }
        
        if (level == 1 && direction == 'up' && topValue - itemHeight >= 0)
        {
            level = 2;
            topAbsoluteValue = (topValue - itemHeight) * -1;
            
            $('.jspPane').animate(
                {
                    top: topAbsoluteValue
                },
                {
                    queue: false,
                    duration: 500
                }    
            );
        }
        else if (level == 4 && direction == 'down' && scrollIndex < itemsCount)
        {
            level = 3;
            topAbsoluteValue = (topValue + itemHeight) * -1;
            
            $('.jspPane').animate(
                {
                    top: topAbsoluteValue
                },
                {
                    queue: false,
                    duration: 500
                }    
            );
        }
        
        api.scrollToY(topValue);
    }
    
    //$('#debug').html(' ' + position + '|' + topValue + '|' + level);
    
    $('.subscription-item').removeClass('active');
    $('.subscription-item:nth-child(' + position + ')').addClass('active');
    
    doShowSubscriptionInfo();
}


function autoPlayScroll()
{
    scrollIndex++
    scrollIndex = (scrollIndex > itemsCount) ? 1 : scrollIndex;    
    seekTo(scrollIndex, 'down');
    level++;
    t = setTimeout("autoPlayScroll()", 5000);
}
