$(function()
{
    if (typeof(SHOWCASE_TIME) != "number")
        SHOWCASE_TIME = 5000;
        
    if (typeof(SHOWCASE_PAGESIZE) != 'number')
        SHOWCASE_PAGESIZE = 7;
    
    var showcase = $('#showcase');  if (showcase.length <= 0) return;
    var current = 0;
    var timeout = null;
    var nav = showcase.find('.nav').css('position', 'relative');
    var sz = showcase.find('.nav li').length;
    
    function getPage(index)
    {
        return parseInt(index / SHOWCASE_PAGESIZE);
    }
    
    function stopAuto()
    {
        clearTimeout(timeout);
        timeout = setTimeout(next, SHOWCASE_TIME * 3);
    }
    
    nav.find('li').each(function(index)
    {
        var li = $(this);
        
        if (li.hasClass('active'))
            current = index;
        
        li.find('a').click(function()
        {
            stopAuto();
            activate(index);
            return false;
        })
    });
    
    showcase.find('.panel').hover(function()
    {
        $(this).find('.info').show();
    }, function()
    {
        $(this).find('.info').hide();
    });
    
    showcase.find('.panel').click(function()
    {
        window.location.href = $(this).find('a:first').attr('href');
    }).css('cursor', 'pointer');
    
    function activate(index, auto)
    {   
        if (index >= sz) index = 0;
        if (index < 0) index = sz - 1;
        
        current = index;
        
        var page = getPage(index);
        nav.find('li').each(function(i)
        {
            if (getPage(i) != page)
                $(this).hide();
            else
                $(this).fadeIn();
        });
        
        if (page == 0)
            $('.controls .left').fadeTo('fast', .3).addClass('left-disabled');
        else
            $('.controls .left').fadeTo('fast', 1).removeClass('left-disabled');
        
        if (page == getPage(sz-1))
            $('.controls .right').fadeTo('fast', .3).addClass('right-disabled');
        else
            $('.controls .right').fadeTo('fast', 1).removeClass('right-disabled');
        
        showcase.find('.panel').removeClass('active').hide();
        showcase.find('.nav li').removeClass('active');
        
        showcase.find('.panel:eq(' + index + ')').addClass('active').show();
        showcase.find('.nav li:eq(' + index + ')').addClass('active').show();
        
        if (auto)
            showcase.find('div.active').hide().fadeIn();
    }
    
    function next() {
        activate(current + 1, true);
        timeout = setTimeout(next, SHOWCASE_TIME);
    }
    
    $('.controls .right').click(function() {
        var next = (getPage(current) + 1) * SHOWCASE_PAGESIZE;
        if (next >= sz) return;
        
        activate(next);
        stopAuto();
    });

    $('.controls .left').click(function() {
        var next = (getPage(current) - 1) * SHOWCASE_PAGESIZE;
        if (next < 0) return;
        
        activate(next);
        stopAuto();
    });
    
    if (sz <= SHOWCASE_PAGESIZE)
        $('.controls').hide();
    
    activate(current, true);
    
    timeout = setTimeout(next, SHOWCASE_TIME);
});