(function($) {
  $.fn.slideshow = function(options) {
    var $that = this,
        $pages = this.find('.child'),
        $window = $(window);
    var params = $.extend({}, this.slideshow.defaults, options || {}),
        currentIndex = params.initialIndex,
        locked = false;    

    var animate = function(n) {
      if (locked || currentIndex === n) return;
      locked = true;
      params.onTransitionStart();
      $that.stop().animate({
        left: - n * $window.width()
      }, params.duration, params.easing, function() { currentIndex = n; locked = false; params.onTransitionEnd(); });
    };

    var api = {
      prev: function() {  
        if (currentIndex === 0) {
          if (params.loop) {
            $that.css('left', - ($pages.length - 1) * $window.width());
          }
          currentIndex = $pages.length - 1;
        }
        animate(currentIndex - 1);
      },
      next: function() {
        if (currentIndex === $pages.length - 1) {
          if (params.loop) {
            $that.css('left', 0);
          }
          currentIndex = 0;
        }
        animate(currentIndex + 1);
      },
      hasPrev: function() {
        return params.loop || currentIndex > 0;
      },
      hasNext: function() {
        return params.loop || currentIndex < $pages.length - 1;
      },
      moveTo: function(n) {
        animate(n);
      },      
      first: function() {
        animate(0);
      },
      last: function() {
        animate($pages.length - 1);
      },   
      index: function() {
        return currentIndex;
      },
      numOfSlides: function() {
        return $pages.length;
      }
    };
    $that.css('left', - currentIndex * $window.width());
    $that.data('data-slideshow-api', api);
    this.slideshow.api = function() {
      return $that.data('data-slideshow-api');
    };
    return this;
  };

  $.fn.slideshow.defaults = {
    lock: false,
    loop: false,
    initialIndex: 0,
    easing: 'easeInOutQuad',
    duration: 800,
    onTransitionStart: function() {},
    onTransitionEnd: function() {}
  };
})(jQuery);

