(function() {
  var Slideshow;

  Slideshow = (function() {

    Slideshow.Selector = '.slideshow';

    Slideshow.initialize = function() {
      var _this = this;
      return $(this.Selector).each(function(e) {
        return new _this($(e));
      });
    };

    Slideshow.defaultSettings = {
      cycleSpeed: 6.5 * 1000,
      transitionType: 'fade',
      transitionDuration: .5 * 1000,
      randomizeFirstIndex: true
    };

    function Slideshow(element) {
      var thumbnail;
      this.element = element;
      this.settings = Slideshow.defaultSettings;
      this.settings.cycleSpeed = parseInt(this.element.data('cycleSpeed')) || this.settings.cycleSpeed;
      this.settings.transitionType = (this.element.data('transitionType')) || this.settings.transitionType;
      this.settings.transitionDuration = parseInt(this.element.data('transitionDuration')) || this.settings.transitionDuration;
      this.settings.randomizeFirstIndex = this.element.data('randomizeFirstIndex') != null ? {
        "true": true,
        "false": false
      }[this.element.data('randomizeFirstIndex')] : this.settings.randomizeFirstIndex;
      this.mainImage = this.element.find('.slideshow-main-image img');
      this.slideStage = this.element.find('.slideshow-stage');
      this.thumbnails = this.element.find('.slideshow-thumbnails').children();
      this.indicators = this.element.find('.slideshow-indicators').children();
      this.slides = this.slideStage.children().hide();
      this.targets = (function() {
        var _i, _len, _ref, _results;
        _ref = this.thumbnails;
        _results = [];
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          thumbnail = _ref[_i];
          _results.push($(thumbnail).data('target'));
        }
        return _results;
      }).call(this);
      this.setupElements();
      this.currentImageIndex = this.settings.randomizeFirstIndex === true ? Math.round(Math.random() * this.targets.length) : -1;
      this.currentSlide = this.slides.first().show();
      this.refreshThumbnails();
      this.refreshIndicators();
      if (this.targets.length > 1) {
        $.fx.off = true;
        this.displayNextSlide();
        $.fx.off = false;
      }
      this.beginCycling();
    }

    Slideshow.prototype.setupElements = function() {
      var indicator, thumbnail, _i, _j, _len, _len2, _ref, _ref2;
      var _this = this;
      _ref = this.thumbnails;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        thumbnail = _ref[_i];
        $(thumbnail).click(function(e) {
          _this.ceaseCycling();
          return _this.displaySlide($(e.currentTarget).index());
        });
      }
      _ref2 = this.indicators;
      for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
        indicator = _ref2[_j];
        $(indicator).click(function(e) {
          _this.resetCycling();
          return _this.displaySlide($(e.currentTarget).index());
        });
      }
      this.element.find('.slideshow-previous').click(function() {
        return _this.displayPreviousSlide();
      });
      this.element.find('.slideshow-next').click(function() {
        return _this.displayNextSlide();
      });
      this.mainImage.parent().width(this.mainImage.parent().width());
      this.mainImage.parent().height(this.mainImage.parent().height());
      this.mainImage.css({
        position: 'absolute'
      });
      this.mainImage.width(this.mainImage.width());
      this.mainImage.height(this.mainImage.height());
      this.slideStage.children().css({
        position: 'absolute'
      });
      this.slideStage.children().width(this.slideStage.width());
      return this.slideStage.children().height(this.slideStage.height());
    };

    Slideshow.prototype.displaySlide = function(index) {
      var nextSlide, target, thisSlide;
      var _this = this;
      if (this.targets.length > 1) {
        index %= this.targets.length;
        if (index === this.currentImageIndex) return;
        this.currentImageIndex = index;
        target = this.targets[index];
        this.displayImage(target);
        this.refreshThumbnails();
      } else if (this.slides.length > 1) {
        nextSlide = this.slides.eq(index % this.slides.length);
        if (nextSlide === this.currentSlide) return;
        thisSlide = this.currentSlide;
        this.currentSlide = nextSlide;
        nextSlide.stop().css({
          left: this.slideStage.width()
        });
        thisSlide.stop().css({
          left: 0
        });
        nextSlide.show();
        $.timeout(800, function() {
          nextSlide.animate({
            left: 0
          }, _this.settings.transitionDuration, function() {
            return thisSlide.hide();
          });
          return thisSlide.animate({
            left: -_this.slideStage.width()
          }, _this.settings.transitionDuration);
        });
      }
      return this.refreshIndicators();
    };

    Slideshow.prototype.displayImage = function(newPath) {
      var newImage, oldImage;
      newImage = this.mainImage.clone().attr({
        src: newPath
      });
      oldImage = this.mainImage;
      this.mainImage = newImage;
      switch (this.settings.transitionType) {
        case 'fade':
          oldImage.after(newImage);
          newImage.hide().position(oldImage.position());
          newImage.fadeIn(this.settings.transitionDuration);
          return oldImage.fadeOut(this.settings.transitionDuration, function() {
            return oldImage.detach();
          });
        case 'slide':
          oldImage.after(newImage);
          newImage.animate({
            left: 0
          }, this.settings.transitionDuration);
          return oldImage.animate({
            left: -oldImage.width()
          }, this.settings.transitionDuration, function() {
            return oldImage.detach();
          });
      }
    };

    Slideshow.prototype.refreshThumbnails = function() {
      var _this = this;
      return this.thumbnails.each(function(index, element) {
        if (!$(element).hasClass('active') && index === _this.currentImageIndex) {
          $(element).addClass('active');
        }
        if ($(element).hasClass('active') && index !== _this.currentImageIndex) {
          return $(element).removeClass('active');
        }
      });
    };

    Slideshow.prototype.refreshIndicators = function() {
      this.indicators.removeClass('current');
      if (this.targets.length > 1) {
        return this.indicators.eq(this.currentImageIndex).addClass('current');
      } else if (this.slides.length > 1) {
        return this.indicators.eq(this.currentSlide.index()).addClass('current');
      }
    };

    Slideshow.prototype.displayNextSlide = function() {
      if (this.targets.length > 1) {
        return this.displaySlide(this.currentImageIndex + 1);
      } else if (this.slides.length > 1) {
        return this.displaySlide(this.currentSlide.index() + 1);
      }
    };

    Slideshow.prototype.displayPreviousSlide = function() {
      if (this.targets.length > 1) {
        return this.displaySlide(this.currentImageIndex - 1);
      } else if (this.slides.length > 1) {
        return this.displaySlide(this.currentSlide.index() - 1);
      }
    };

    Slideshow.prototype.beginCycling = function() {
      var _this = this;
      this.ceaseCycling();
      return this.intervalID = $.interval(this.settings.cycleSpeed, function() {
        return _this.displayNextSlide();
      });
    };

    Slideshow.prototype.ceaseCycling = function() {
      clearInterval(this.intervalID);
      return this.intervalID = null;
    };

    Slideshow.prototype.resetCycling = function() {
      this.ceaseCycling();
      return this.beginCycling();
    };

    return Slideshow;

  })();

  $(function() {
    return Slideshow.initialize();
  });

}).call(this);

