/**
 * jShowcase 0.1
 *
 * Written by Bruno Kühnen Meneguello <o_kuhnen@yahoo.com.br>
 * Last updated: 2009.02.20
 *
 * jShowcase is a jQuery plugin to display lists of elements. His particular
 * advantage is tabbed groupping of lists.
 * Similar to jCarousel and jCarouselLite
 *
 * @todo Implementar a opção fisheye. Esta opção permitirá que quando o número
 * de itens for menor que a capacidade do painel, os itens exibidos fiquem
 * agrupados no centro, respeitando a margem. E quando houver a movimentação,
 * o elemento acelerará para a borda enquanto some
 *
 * Changes:
 *
 * 0.2
 * - O os botões de controle foram movidos para dentro da fução setList, agora
 *   independentes para cada lista
 */
jQuery.fn.jshowcase = function($options) {
    var $defaults = {
        headerEnabledClass:'jshowcase-header-enabled',
        itensShown:0,
        itemWidth:100,
        itemHeight:100,
        itemMargin:0,
        listMargin:0,
        prevCommand:'<button>&lt;&lt;</button>',
        prevCommandClass:'jshowcase-prev-command',
        nextCommand:'<button>&gt;&gt;</button>',
        nextCommandClass:'jshowcase-next-command'
    }

    var $locals = jQuery.extend({}, $defaults, $options);

    function setupGroup($group) {
        $group.children('dd').each(function() {
            jQuery(this).children('ul').each(function() {
                setupList(jQuery(this));
            });
        });

        $group.children('dd').hide();
        jQuery($group.children('dt')[0]).addClass($locals.headerEnabledClass);
        jQuery($group.children('dd')[0]).show();

        $group.children('dt').click(function() {
            jQuery(this).siblings('dt').andSelf().removeClass($locals.headerEnabledClass);
            jQuery(this).siblings('dd').hide();
            jQuery(this).addClass($locals.headerEnabledClass).next('dd').show();
        });
    }

    function setupList($list) {
        var $children = $list.children('li');
        var $size = $children.size();

        $list.css({
            width:'100%',
            height:$locals.itemHeight,
            position:'relative',
            overflow:'hidden'
        });

        var $itensShown = $locals.itensShown;
        var $margin     = $locals.itemMargin;
        var $listWidth  = $list.width() - ($locals.listMargin * 2);

        if($itensShown == 0 || $itensShown > $size) {
            if($size * $locals.itemWidth <= $listWidth) {
                // Mostra todos
                $itensShown = $size;
            } else {
                // Quantos cabem inteiros na janela
                $itensShown = Math.floor($listWidth / ($margin + $locals.itemWidth));
            }
        }

        if($itensShown >= 1) {
            var $itens_width = $locals.itemWidth * $itensShown;
            $margin = (($listWidth - $itens_width) / ($itensShown + 1));
        }

        $children.each(function($index) {
            jQuery(this).css({
                position:'absolute',
                left:$index * ($locals.itemWidth + $margin) + $margin + $locals.listMargin,
                width:$locals.itemWidth,
                height:$locals.itemHeight
            });
            if($index >= $itensShown) {
                jQuery(this).hide();
            }
        });

        var $index = 0;
        var $animating = false;

        switch(typeof($locals.prevCommand)) {
            case 'string':
                var $prevCommand = jQuery($locals.prevCommand);
                if($prevCommand.selector == "") {
                    $list.before($prevCommand);
                }
                break;
            case 'object':
                if($locals.prevCommand instanceof jQuery) {
                    $prevCommand = $locals.prevCommand;
                }
                break;
            case 'function':
                $prevCommand = ($locals.prevCommand)();
                break;
        }
        $prevCommand.addClass($locals.prevCommandClass);
        $prevCommand.click(function() {
            if(!$animating && $index > 0) {
                $animating = true;
                $($children[$index - 1 + $itensShown]).fadeOut();
                $children.each(function($i) {
                    var $left = ($i - ($index - 1)) * ($locals.itemWidth + $margin) + $margin + $locals.listMargin;
                    if($i != $index - 1) {
                        $(this).animate({
                            left:$left
                        },1000);
                    } else {
                        $(this).css('left',$left);
                    }
                });
                $($children[$index - 1]).animate({wait:0},500).fadeIn({
                    duration:'slow',
                    complete:function() {
                        $index -= 1;
                        $animating = false;
                    }
                });
            }
        });

        switch(typeof($locals.nextCommand)) {
            case 'string':
                var $nextCommand = jQuery($locals.nextCommand);
                if($nextCommand.selector == "") {
                    $list.after($nextCommand);
                }
                break;
            case 'object':
                if($locals.nextCommand instanceof jQuery) {
                    $nextCommand = $locals.nextCommand;
                }
                break;
            case 'function':
                $nextCommand = ($locals.nextCommand)();
                break;
        }
        $nextCommand.addClass($locals.nextCommandClass);
        $nextCommand.click(function() {
            if(!$animating && $index + $itensShown < $size) {
                $animating = true;
                $($children[$index]).fadeOut();
                $children.each(function($i) {
                    var $left = ($i - ($index + 1)) * ($locals.itemWidth + $margin) + $margin + $locals.listMargin;
                    if($i != $index + $itensShown) {
                        $(this).animate({
                            left:$left
                        },1000);
                    } else {
                        $(this).css('left',$left);
                    }
                });
                $($children[$index + $itensShown]).animate({wait:0},500).fadeIn({
                    duration:'slow',
                    complete:function() {
                        $index += 1;
                        $animating = false;
                    }
                });
            }
        });
    }

    this.each(function() {
        if(this.tagName == 'DL') {
            setupGroup(jQuery(this));
        } else if(this.tagName == 'UL') {
            setupList(jQuery(this));
        }
    });
};