//jQuery plugin for a slideshow of testimonials

(function( $ ){

  $.fn.testimonialSlideshow = function(options) {

    // Create some defaults, extending them with any options that were provided
    var settings = $.extend( {
        'quoteSelector'         : '.quoteContainer',
        'timer'                 : 8500
      }, options);

    return this.each(function() {    
        var $slideshow = $(this),
        _width = $slideshow.outerWidth(),
        $quotes = $slideshow.find(settings.quoteSelector),
        cssObj = {position:'absolute',top:0, left:_width * 2,width:_width,opacity:0,zIndex:1};

        // set up CSS and add events
        $slideshow.css({overflow:'hidden',position:'relative'});
        $quotes.each(function(){
            var $this = $(this);
            $this.css(cssObj);

            // events
            $this.bind('moveIn',function(){
                var $this = $(this);
                $this.animate({left:0,opacity:1},'slow');
                $slideshow.css({height:$(this).outerHeight()});
    
                setTimeout(function(){
                   $this.trigger('moveOut'); 
                },settings.timer);
            });
            $this.bind('moveOut',function(){
                var $this = $(this);
                $this.animate({left:-_width},function(){
                    $this.css(cssObj);
                    var $next = ($this.next('.quoteContainer').length > 0) ? $this.next('.quoteContainer') : $quotes.eq(0);
                    $next.trigger('moveIn');
                });
            });
        });

        // start the first one
        $quotes.eq(0).trigger('moveIn');
          
    });

  };
})( jQuery );


$(function(){
    $('.testimonialSlideshow').testimonialSlideshow();
});

