(function() {
  var $, Slideshow, redraw;
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __slice = Array.prototype.slice;
  $ = jQuery;
  Slideshow = (function() {
    function Slideshow(element, options) {
      var autoplay, next, pause, play, prev, _ref;
      this.options = $.extend({}, this.options, options);
      this.element = $(element);
      this.frame = $(this.options.frame, this.element);
      this.container = $(this.options.container, this.element);
      this.items = this.container.children();
      this.currentIndex = 0;
      this.currentElement = this.items.first();
      _ref = this.options, prev = _ref.prev, next = _ref.next, play = _ref.play, pause = _ref.pause, autoplay = _ref.autoplay;
      if (prev) {
        $(prev, this.element).click(__bind(function(event) {
          event.preventDefault();
          this.stopIdleTimer();
          this.pause();
          return this.previous();
        }, this));
      }
      if (next) {
        $(next, this.element).click(__bind(function(event) {
          event.preventDefault();
          this.stopIdleTimer();
          this.pause();
          return this.next();
        }, this));
      }
      if (play) {
        $(play, this.element).click(__bind(function() {
          return this.play();
        }, this));
      }
      if (pause) {
        $(pause, this.element).click(__bind(function() {
          return this.pause();
        }, this));
      }
      if (autoplay) {
        this.play();
      } else {
        this.startIdleTimer();
      }
      this.index = this.options.wrap ? this.indexWrap : this.indexNoWrap;
      if ($.fn.swipe != null) {
        this.element.swipe({
          swipeLeft: __bind(function() {
            this.pause();
            return this.next();
          }, this),
          swipeRight: __bind(function() {
            this.pause();
            return this.previous();
          }, this)
        });
      }
      this.show(this.currentIndex);
      window.setTimeout(__bind(function() {
        this.element.trigger('show:init', [this]);
        this.element.trigger('show:start', [this]);
        return this.element.trigger('show:complete', [this]);
      }, this));
    }
    Slideshow.prototype.options = {
      container: ".slides",
      frame: ".frame",
      prev: "a.previous",
      next: "a.next",
      play: "a.play",
      pause: "a.pause",
      autoplay: false,
      idle: false,
      delay: 2000,
      wrap: true,
      duration: 'short',
      easing: 'linear',
      queue: false
    };
    Slideshow.prototype.indexWrap = function(n) {
      var len, offset;
      if (typeof n !== 'number') {
        return 0;
      }
      len = this.items.length;
      offset = n < 0 ? len : 0;
      return Math.floor(n) % len + offset;
    };
    Slideshow.prototype.indexNoWrap = function(n) {
      var floor, len, max, min;
      min = Math.min, max = Math.max, floor = Math.floor;
      len = this.items.length;
      return max(0, min(len - 1, floor(n)));
    };
    Slideshow.prototype.previous = function() {
      return this.show(this.currentIndex - 1);
    };
    Slideshow.prototype.next = function() {
      return this.show(this.currentIndex + 1);
    };
    Slideshow.prototype.timer = null;
    Slideshow.prototype.play = function() {
      if (this.timer == null) {
        this.timer = window.setInterval((__bind(function() {
          return this.next();
        }, this)), this.options.delay);
        return this.element.trigger("show:play", [this]);
      }
    };
    Slideshow.prototype.pause = function() {
      if (this.timer != null) {
        window.clearInterval(this.timer);
        this.timer = null;
        this.startIdleTimer();
        this.element.stop();
        return this.element.trigger("show:pause", [this]);
      }
    };
    Slideshow.prototype.show = function(n) {
      var element, index;
      index = this.index(n);
      element = this.items[index];
      this.currentIndex = index;
      this.currentElement = $(element);
      this.disableButtons();
      return this.animate();
    };
    Slideshow.prototype.disableButtons = function() {
      var nextbutton, nextindex, prevbutton, previndex;
      nextindex = this.index(this.currentIndex + 1);
      previndex = this.index(this.currentIndex - 1);
      prevbutton = $(this.options.prev, this.element);
      nextbutton = $(this.options.next, this.element);
      if (nextindex !== this.currentIndex) {
        nextbutton.removeClass("disabled");
      } else {
        nextbutton.addClass("disabled");
      }
      if (previndex !== this.currentIndex) {
        return prevbutton.removeClass("disabled");
      } else {
        return prevbutton.addClass("disabled");
      }
    };
    Slideshow.prototype.animate = function() {
      var coordinates, options, position;
      position = this.currentElement.position();
      coordinates = {
        scrollTop: position['top'] + this.frame.scrollTop(),
        scrollLeft: position.left + this.frame.scrollLeft()
      };
      options = {
        duration: this.options.duration,
        easing: this.options.easing,
        queue: this.options.queue,
        complete: __bind(function() {
          return this.element.trigger('show:complete', [this]);
        }, this)
      };
      this.element.trigger('show:start', [this]);
      return this.frame.animate(coordinates, options);
    };
    Slideshow.prototype.addItem = function(element) {
      this.container.append(element);
      this.items = this.container.children();
      return this.disableButtons();
    };
    Slideshow.prototype.loadSlides = function() {
      var load_slide, make_slide, slides, template;
      slides = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
      template = this.items[0];
      make_slide = __bind(function(opt, callback) {
        var image, link, slide;
        slide = $(template).clone();
        image = $("img", slide);
        link = $("a", slide);
        link.attr('href', opt.link);
        setTimeout(function() {
          return image.attr({
            src: opt.image,
            alt: opt.name
          });
        });
        image.bind('load', __bind(function() {
          this.addItem(slide);
          image.unbind('load');
          return typeof callback === "function" ? callback() : void 0;
        }, this));
        return slide;
      }, this);
      load_slide = function(i) {
        if (i < slides.length) {
          return make_slide(slides[i], function() {
            return load_slide(i + 1);
          });
        }
      };
      return load_slide(0);
    };
    Slideshow.prototype.idleTimer = null;
    Slideshow.prototype.startIdleTimer = function() {
      if (this.options.idle) {
        this.stopIdleTimer();
        return this.idleTimer = window.setTimeout((__bind(function() {
          return this.play();
        }, this)), this.options.idle);
      }
    };
    Slideshow.prototype.stopIdleTimer = function() {
      if (this.idleTimer != null) {
        window.clearTimeout(this.idleTimer);
        return this.idleTimer = null;
      }
    };
    return Slideshow;
  })();
  redraw = function(element) {
    var n;
    n = document.createTextNode(' ');
    element.appendChild(n);
    return window.setTimeout(function() {
      return n.parentNode.removeChild(n);
    });
  };
  $.fn.slideshow = function(options) {
    this.data('slideshow', new Slideshow(this, options));
    return $(this);
  };
  $.fn.loadSlides = function() {
    var slides, slideshow, _ref;
    slides = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
    slideshow = (_ref = this.data('slideshow')) != null ? _ref : this.data('slideshow', new Slideshow(this, options));
    if (slideshow != null) {
      slideshow.loadSlides.apply(slideshow, slides);
    }
    return $(this);
  };
}).call(this);

