/*
 * Slide show jQuery plugin
 *
 * @description  
 * @author       Razvan Nastase <contact@bonsoftware.com>
 * @version      1.0
 * @created      2011/05/18 09:00:00
 * @modified     2011/05/18 09:00:00
 * @copyright    Copyright (c) 2009, Bon Media Software - All Rights Reserved
 *
 * No part of this document may be reproduced and you may not modify or use this
 * document for any other purposes (including other projects) without written
 * consent from the author. Please see http://www.bonsoftware.com/license for
 * more information.
 */

(function($) {
    var defaultOptions = {
        autoplay: false,
        autoplayTimeout: 7000
    };

    var methods = {
        init: function(options) {
            options = options ? options : {};

            return this.each(function(){
                var $this = $(this);
                var data = $this.data('slideshow');

                options = $.extend({}, defaultOptions, options);

                if (!data) {
                    $this.data('slideshow', {
                        target: $this,
                        options: options
                    });
                }

                build($this);
            });
        }
    };

    $.fn.slideshow = function(method) {
        if (methods[method]) {
            return methods[method].apply(this,
                                         Array.prototype.slice.call(arguments, 1));
        }
        else if (typeof method === 'object' || ! method) {
            return methods.init.apply(this, arguments);
        }
        else {
          $.error('Method ' +  method + ' does not exist on jQuery.slideshow');
          return null;
        }
    };


    function build($this) {
        $this.addClass("slideshow");

        $this.each(function() {
            var $el = $(this);

            $el.find(".prev").unbind('click.slideshow');
            $el.find(".prev").bind('click.slideshow', function() {
                clearInterval($el.data('autoplayTimeout'));

                scrollLeft($el);
                return false;
            });

            $el.find(".next").unbind('click.slideshow');
            $el.find(".next").bind('click.slideshow', function() {
                clearInterval($el.data('autoplayTimeout'));

                scrollRight($el);
                return false;
            });

            if ($el.data("slideshow").options.autoplay) {
                $el.data('autoplayTimeout',
                         setInterval(function() {
                                        scrollRight($el);
                                    }, $el.data("slideshow").options.autoplayTimeout));
            }
        });
    }

    function scrollLeft($el) {
        if ($el.find(".list .item").length > 1) {
            $el.find(".list .item:last").hide();
            $el.find(".list .item:last").insertBefore($el.find(".list .item:first"));
            $el.find(".list .item:first").animate({width: 'show'});
        }
    }

    function scrollRight($el) {
        if ($el.find(".list .item").length > 1) {
            $el.find(".list .item:first").animate({width: 'hide'}, function() {
                $el.find(".list .item:first").insertAfter($el.find(".list .item:last"));
                $el.find(".list .item:last").show();
            });
        }
    }
})(jQuery);
