(function($, window, document, undefined) {

  var $slideshow = $("#client-slideshow"),
    $panels = $slideshow.find("a img"),
    data = $slideshow.data("slideshow", {
      current: 0,
      next: 0,
      total: $panels.length,
      slides: []
    }).data("slideshow"),
    delay = 3000,
    timer;

  function transition() {

    if (data.next === data.total) {
      data.next = 0;
    }

    data.slides[data.current].fadeOut(1500);
    data.slides[data.next].fadeIn(1500);
    
    data.current = data.next;
  }
  
  function resume() {
    timer = setTimeout(autoplay, delay * 2);
  }
  
  function pause() {
    clearTimeout(timer);
  }
  
  function autoplay() {
    data.next++;

    transition();

    timer = setTimeout(autoplay, delay);
  }

  // init data object
  $panels.each(function(index) {
    data.slides.push($(this));
  });
  
  $slideshow.bind({
    mouseover: pause,
    mouseout: resume
  });
  
  timer = setTimeout(autoplay, delay);

})(jQuery, this, this.document);
