/*
 * jQuery Simple Slideshow Plugin 1.0.0
 *
 * Copyright (c) 2009 Andria Klimov
 * Licensed under the MIT license (MIT-LICENSE.txt)
 * http://www.opensource.org/licenses/mit-license.php
 */
(function($) {

  $.fn.slideshow = function(options) {

    // initialize plugin
    return this.each(function() {

      var $set = $(this);

      // init configuration
      var config = {
        play     : true,
        wait     : false,
        fade     : 500,
        interval : 5000};

      // update configuration
      if (options)
        $.extend(config, options);

      // extend configuration
      $.extend(config, {
        block    : false,
        timeout  : false});

      // slide count
      function count() {
        return $set.children('li').length;
      }

      // play slideshow
      function playTimeout() {
        if (!config.timeout) {
          config.timeout = setTimeout(switchSlide, config.interval, false);
        }
      }

      // stop slideshow
      function stopTimeout() {
        if (config.timeout) {
          clearTimeout(config.timeout);
          config.timeout = false;
        }
      }

      // switch slide
      function switchSlide(direction) {
        if (!config.block && count()) {
          var $current = $set.children('li.active');
          var $replace = direction ?
           ($current.prev().length ? $current.prev() : $set.children('li:last')) :
           ($current.next().length ? $current.next() : $set.children('li:first'));
          if ($current[0] != $replace[0]) {
            config.block = true;
            if (config.play)
              stopTimeout();
            $current.removeClass('active').addClass('passive');
            $replace.addClass('active').css({opacity: 0.0}).animate({opacity: 1.0}, config.fade, function() {
              $current.removeClass('passive');
              if (config.play)
                playTimeout();
              config.block = false;
            });
          }
        }
      }

      // setup external interface
      this.slideshow = {
        remove: function () {
          stopTimeout();
          delete this.slideshow;
        },
        play: function (wait) {
          config.play = true;
          if (wait)
            playTimeout();
          else
            switchSlide();
        },
        stop: function () {
          config.play = false;
          stopTimeout();
        },
        next: function () {
          switchSlide();
        },
        prev: function () {
          switchSlide(true);
        }
      };

      // correct widths and heights
      var max = { width: 0, height: 0 };
      $set.children('li').each(function() {
        if (max.width < $(this).width())
          max.width = $(this).width();
        if (max.height < $(this).height())
          max.height = $(this).height();
      });

      $set.width(max.width).height(max.height);
      $set.children('li').width(max.width).height(max.height);

      if (config.play)
        this.slideshow.play(config.wait);

    });
  };
})(jQuery);

