$.fn.Slideshow = function(options) {
  options = $.extend({
    navigation: null,   // Attaches 'showSlide(@href)' to each anchor in the provided object
    next:       null,   // Attaches 'nextSlide()' to this object
    previous:   null,   // Attaches 'prevSlide()' to this object
    selector:   'div.slideshow-slides > img', // The selector for your slides
    speed:      500,    // Speed of the transition
    start:      null,   // Attaches 'startShow()' to this object
    stop:       null,   // Attaches 'stopShow()' to this object
    timeout:    5000,   // Length of time between slides
    zindex:     500     // zIndex for active slides. Slides shown next will be 'zindex - 1'
  }, options);

  var slides = $(options.selector, this);
  var slide  = 0;
  var timer;

  var nextSlide = function() {
    if (slide + 1 >= slides.length) {
      showSlide(0);
    } else {
      showSlide(slide + 1);
    }

    return false;
  };
  var prevSlide = function() {
    if (slide - 1 < 0) {
        showSlide(slides.length);
    } else {
        showSlide(slide - 1);
    }

    return false;
  };
  var showSlide = function(idx) {
    if (idx >= 0 && idx < slides.length && slide != idx) {
      var active = $(slides).filter(':visible');
      var toshow = $(slides[idx]);

      toshow.css('zIndex', options.zindex - 1).show();
      active.fadeOut(options.speed, function() {
        toshow.css('zIndex', options.zindex);
      });

      slide = idx;
      updateNav();
    }
  };
  var startShow = function() {
    if (!timer) {
      timer = setInterval(nextSlide, options.timeout);
      $(options.start).addClass('active');
      $(options.stop).removeClass('active');
    }

    return false;
  };
  var stopShow = function() {
    if (timer) {
      clearInterval(timer);
      timer = null;
      $(options.stop).addClass('active');
      $(options.start).removeClass('active');
    }

    return false;
  };
  var updateNav = function() {
    if (options.navigation) {
      $('a.active', $(options.navigation)).removeClass('active');
      $('a[href="#slide-' + slide + '"]', $(options.navigation)).addClass('active');
    }
  };

  if (options.start) {
    $(options.start).bind('click', startShow).hover(function() {
        if (!timer) $(this).addClass('active');
    }, function() {
        if (!timer) $(this).removeClass('active');
    });
  }
  if (options.stop) {
    $(options.stop).bind('click', stopShow).hover(function() {
        if (timer) $(this).addClass('active');
    }, function() {
        if (timer) $(this).removeClass('active');
    });
  }
  if (options.navigation) {
    $('a', $(options.navigation)).each(function() {
      $(this).bind('click', function() {
        var idx = parseInt($(this).attr('href').replace(/#slide-/, ''));
        var restart = false;

        // We need to stop then start the show to reset the timeout
        if (timer) {
          stopShow();
          restart = true;
        }
        showSlide(idx);
        if (restart) {
          startShow();
        }

        return false;
      });
    });
  }

  slides.not(':first-child').hide();
  slides.filter(':first-child').css('zIndex', options.zindex);
  updateNav();
  startShow();

  return this;
};

