/**
 * Carousel plugin for jQuery
 *
 * width: width of carousel container
 * height: height of carousel container
 * duration: animation time in milliseconds
 * selector: child node selector
 * loop: carousel goes back to the first item if set to true
 *
 * @author Kamesh Balasubramanian
 *
 */

(function($){
    // Carousel class
    var Carousel = function(element, options)
    {
        var elem = $(element),
            obj = this,
            current = 0,
            settings = $.extend({
                width:          300,
                height:         200,
                duration:       300,
                selector:       'img',
                loop:           false
            }, options || {}),
            children = elem.find(settings.selector);
        
        elem.css({
            position:       'relative',
            overflow:       'hidden',
            //overflow:       'visible',
            width:          settings.width + 'px',
            height:         settings.height + 'px'
        });
        
        children.css({
            position:       'absolute',
            zIndex:         99,
            top:            0,
            left:           0,
            width:          settings.width + 'px',
            height:         settings.height + 'px'
        });

        var getChild = function(index)
        {
            return $(children[index]);
        };
        
        var showChild = function(index, direction)
        {
            var nextChild = getChild(index),
                currentChild = getChild(current);
            
            currentChild.css({
               left:        0,
               zIndex:      100
            }).animate({
                left:       -(direction * settings.width) + 'px'
            }, settings.duration);
            
            nextChild.css({
                left:       (direction * settings.width) + 'px',
                zIndex:     101
            }).animate({
                left:       0
            }, settings.duration, function()
            {
                children.not(nextChild).css({
                    zIndex:         99,
                    left:           0
                });
                nextChild.css({
                    zIndex:         100
                });
            });
        };
        
        this.next = function()
        {
            var next = current + 1;
            if (next >= children.length) {
                if (!settings.loop) return;
                next = 0;
            }
            showChild(next, 1);
            current = next;
        };
        
        this.prev = function()
        {
            var prev = current - 1;
            if (prev < 0) {
                if (!settings.loop) return;
                prev = children.length - 1;
            }
            showChild(prev, -1);
            current = prev;
        };
        
        getChild(current).css('zIndex', 100);
    };

    // Register jQuery plugin
    $.fn.carousel = function(options)
    {
        return this.each(function()
        {
            var element = $(this);
            if (element.data('carousel')) return;
            var carousel = new Carousel(element, options);
            element.data('carousel', carousel);
        });
    };
})(jQuery);

