/*!
 * Slideshow with jQuery
 *  
 * Built on top of the jQuery library
 *   http://jquery.com
 * 
 */

 // namespace the plugin
  (function($) {
      /**
       * Slideshow methods.
       */
      var methods = {
          /**
           * Initialize
           */
          init: function(options) {
              var settings = {
                  scrollSpeedMillis:         1000,
                  autoScrollTimeoutMillis:   10000,
                  enableAutoScroll:          true 
              };
              $.extend(settings, options);
              return this.each(function() { 
                  var $this = $(this);
                  var data = {
                      scrollSpeedMillis:             settings.scrollSpeedMillis,
                      autoScrollTimeoutMillis:       settings.autoScrollTimeoutMillis,
                      enableAutoScroll:              settings.enableAutoScroll,
                      currentSlide:                  0,
                      interval:                      undefined,
                      animating:                     false
                  };
                  $this.data('slideshow', data);
                  
                  // set initial positioning info
                  $this.children().css({
                      position: 'absolute',
                      top:        '0px',
                      left:       '0px',
                      width:      '100%',
                      height:     '100%'
                  });
                  $this.css({
                      position:   'relative',
                      overflow:   'hidden'
                  });
                  $this.slideshow('showSlide', 0);
                  
                  // start the auto scrolling
                  if (settings.enableAutoScroll) {
                      data.interval = setInterval(function() { 
                          $this.slideshow('next'); 
                          }, settings.autoScrollTimeoutMillis);
                  }
              });
          },
          /**
           * stops auto scroll
           */
          stopAutoScroll: function() {
              return this.each(function(){
                    var $this = $(this);
                    var data = $this.data('slideshow');
                  clearInterval(data.interval);
              });
          },
          /**
           * shows a slide with animation
           */
          scrollSlide: function(slide, direction) {
              return this.each(function(){
                  var $this = $(this);
                  var data = $this.data('slideshow');

                  // get children
                  var children = $this.children();
                  
                  // determine direction
                  if (!direction) {
                      direction = "right";
                  }

                   // bail if they're stupid
                   if (data.currentSlide==slide) {
                       return;
                   } else if (slide>=$this.children().length) {
                       return;
                   } else if (data.animating) {
                       return;
                   }
                   data.animating = true;

                  // slide them
                  var $current = $(children[data.currentSlide]);
                  var $next = $(children[slide]);
                  if (direction=="left") {
                     $next.css({
                         top: '0px',
                         left: $this.width()+'px'
                     });
                     $current.css({
                          top:  '0px',
                          left: '0px'
                     });
                  } else {
                      $next.css({
                          top: '0px',
                          left: '-'+$this.width()+'px'
                      });
                      $current.css({
                           top:  '0px',
                           left: '0px'
                      });
                  }
                  $next.show();
                  var operator = (direction=="left") ? "-" : "+";
                    $next.animate({
                        left: operator+'='+$next.width()
                    }, 1000, function() { data.currentSlide=slide; $this.trigger("slideshow:newslide", slide) });
                    $current.animate({
                        left: operator+'='+$next.width()
                    }, 1000, function() { data.animating=false; $current.hide(); });
              });
          },
          /**
           * shows a slide without animation
           */
          showSlide: function(slide) {
              return this.each(function(){
                 var $this = $(this);
                 var data = $this.data('slideshow');
                 $this.children().each(function(idx, el) {
                     if (idx==slide) {
                         $(el).show();
                         $(el).css({
                             top:     '0px',
                             left:    '0px'
                         });
                     } else {
                         $(el).hide();
                         $(el).css({
                             top:     '0px',
                             left:    $this.width()+'px'
                         });
                     }
                 });
                 data.currentSlide=slide;
              });
           },
           /**
            * Lays out the next slide
            */
           next: function(slide) {
               return this.each(function(){
                  var $this = $(this);
                  var data = $this.data('slideshow');
                  var children = $this.children();
                  var nextSlide = data.currentSlide+1;
                  if (nextSlide>children.length-1) {
                      nextSlide=0;
                  }
                  $this.slideshow('scrollSlide', nextSlide, "left");
              });
           },
           /**
            * Lays out the previous slide
            */
          previous: function(slide) {
              return this.each(function(){
                 var $this = $(this);
                 var data = $this.data('slideshow');
                 var children = $this.children();
                 var previousSlide = data.currentSlide-1;
                 if (previousSlide<0) {
                     previousSlide=children.length-1;
                 }
                 $this.slideshow('scrollSlide', previousSlide, "right");
             });
          }
      };
      /**
       * Access to all slideshow methods are through
       * this function.
       */
      $.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');
          }
      };
      
  }(jQuery));
