/*
 * jQuery SnapSlide V1.3
 * A simple jQuery slideshow plugin
 * Author: Nathan Blenke
 * http://nathan.blenke.com/jquery/snapslide/
 * License: http://creativecommons.org/licenses/by-nc-sa/3.0/
 *
 * EXAMPLE OF USE:
 *  $("#slideshow").snapslide({ 
 *    speed:300, // speed of transition in ms
 *    delay:4000 // delay time between slides in ms
 *  });
 * 
 *  <div id="slideshow">
 *    <img src="1.png">
 *    <img src="2.png">
 *    <img src="3.png">
 *  </div>
 */
 
(function($) {
 
  $.fn.snapslide = function(settings) {
    var config = {
      speed: 300,
      delay: 4000
    };
    if (settings) $.extend(config, settings);
    
    var t = $(this);
    var count;
    var interval;
    var old = 0;
    var current = 0;
    count = t.find("img").size();
    interval = setInterval(rotate,config.delay);
    
    function rotate() {
      current = (old + 1) % count;
      t.find("img:eq(" + old + ")").hide();
      t.find("img:eq(" + current + ")").fadeIn(config.speed);
      old = current;
    };
    
    this.each(function() {
      
      t.find("img").hide().parents().find("img:eq(0)").fadeIn(config.speed);
      
      t.find("img").click(function(){
        current = (old + 1) % count;
        if ( $(this).is(":last-child") ) {
          $(this).hide().parents().find("img:first").fadeIn(config.speed);
        } else {
          $(this).hide().next().fadeIn(config.speed);
        }
        old = current;
      });
      
      t.find("img").hover(function() {
        clearInterval(interval);
      }, function() {
        interval = setInterval(rotate,config.delay);
      });
    
    });
  
    return this;
   
  };
 
})(jQuery);
