/**
 * Carousel jQuery plugin
 *
 * @author Shilikov Alexey <ash@wstudio.ru>
 *
 * @copyright 2011, WhiteStudio
 * @version 0.1
 */

(function($) {
    $.fn.carousel = function(options) {
        var options = $.extend({
            scroll: '',
            item:   '',
            left:   '',
            right:  '',
            speed:  'normal',
            width:  0
        }, options);

        var self = this;

        this.animation = false;
        this.options   = options;

        // =====================================================================

        /**
         * Click on scroll-left button
         */
        $(options.left).click(function() {
            var
                scroll = $(self.options.scroll),
                item   = $(self.options.item),
                count  = $(self.options.item).length;

            if (!self.animation) {
                self.animation = true;

                item.eq(count - 1).clone().prependTo(scroll);
                scroll.css({left: -self.options.width});
                $(scroll).animate({left: 0}, self.options.speed,
                    function() {
                        $(item).eq(count - 1).remove();

                        self.animation = false;
                    }
                );
            }
        });

        // =====================================================================

        /**
         * Click on scroll-right button
         */
        $(options.right).click(function() {
            var
                scroll = $(self.options.scroll),
                item   = $(self.options.item);

            if (!self.animation) {
                self.animation = true;

                $(item).eq(0).clone().appendTo(scroll);
                $(scroll).animate({left: -self.options.width}, self.options.speed,
                    function() {
                        $(item).eq(0).remove();
                        $(scroll).css({left: 0});

                        self.animation = false;
                    }
                );
            }
        });
    };
})(jQuery);

