/*
jQuery Slideshow - Jon Suderman May 2011

Useage: $('.slideshow').slideshow();

Markup:
<div class="slideshow">
  <ol>
    <li>
      <a href="#">
        <img src="/images/content/slide1.jpg" />
      </a>
    </li>
    <li>
      <a href="#">
        <img src="/images/content/slide2.jpg" />
      </a>
    </li>
  </ol>

  <nav></nav>
  <a class="nav previous" href="#"></a>
  <a class="nav next" href="#"></a>
</div>
*/

(function($){

  $.fn.slideshow = function(options) {

    var settings = {
      delay: 5000
    };
    if (options) {
      $.extend(settings, options);
    }

    function build(slideshow){
      $(slideshow).find('li:gt(0)').hide();
      $(slideshow).find('li').each(function(){
        $(this).find('img:gt(0)').hide().css({ position:'absolute', zIndex:20 });
      });

      $(slideshow).find('nav').each(function(){
        var $nav = $(this);
        var navLinks = '';
        var numSlides = $(slideshow).find('li').length;

        $(slideshow).find('li').each(function(index){
          $(this).attr('rel', index);
          next = ((index + 1) < numSlides) ? index + 1 : 0;
          previous = ((index - 1) >= 0) ? index - 1 : numSlides - 1;
          navLinks += '<a href="#slideshow-nav-'+index+'" rel="'+index+'" previous="'+previous+'" next="'+next+'"></a>';
        });
        $(navLinks).appendTo($nav);

        $nav.find('a:first').addClass('active');
        $nav.find('a').click(function(event){ 
          event.preventDefault();
          click(slideshow, $(this).attr('rel')); 
        });

        $(slideshow).find('a.previous').click(function(event){
          event.preventDefault();
          click(slideshow, $(slideshow).find('nav a.active').attr('previous')); 
        });

        $(slideshow).find('a.next').click(function(event){
          event.preventDefault();
          click(slideshow, $(slideshow).find('nav a.active').attr('next')); 
        });

      });
    }

    function switchSlides(slideshow){
      var selector = ($(slideshow).data('nextSlide')) ? 'li[rel='+$(slideshow).data('nextSlide')+']' : 'li:first';

      var $current = $(slideshow).find('li:first');
      var $next = $current.nextAll(selector);

      $(slideshow).find('nav a.active').not('a.pause').removeClass('active');
      $(slideshow).find('nav a[rel='+$next.attr('rel')+']').addClass('active');

      $(slideshow).data('animating', true);
      $current.fadeOut('slow', function(){
        animate(slideshow, $next);
        $next.fadeIn('slow', function(){
            $(slideshow).data('animating', false);
            stop(slideshow);
            start(slideshow, settings.delay);
          }).end().appendTo($current.parent());
      });
    }

    function start(slideshow, delay){
      $(slideshow).data('nextSlide', null);
      $(slideshow).data('timer', window.setTimeout(function(){switchSlides(slideshow);}, delay));
    }

    function stop(slideshow) {
      if ($(slideshow).data('timer')) {
        window.clearTimeout($(slideshow).data('timer'));
      }
    }

    function click(slideshow, id) {
      if ($(slideshow).data('animating') === false) {
        stop(slideshow);
        $(slideshow).data('nextSlide', id);
        switchSlides(slideshow);
      }
      return false;
    }


    function animate(slideshow, next){
      $(next).find('img:gt(0)')
        .css({ left:'-'+$(next).width()+'px' })
        .show()
        .animate({ left:0 }, 'slow');
    }

    return this.each(function(i, slideshow) {
      if ($(this).find('ol li').length > 1) {

        $(this).data('timer', null);
        $(this).data('animating', false);
        $(this).data('nextSlide', null);

        build(this);
        animate(this, $(this).find('ol li:first'));
        start(slideshow, settings.delay);
      }
    });
  };

})(jQuery);

