(function() {
  var Gallery;
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  Gallery = (function() {
    function Gallery(element) {
      var children, image, _i, _len, _ref;
      this.element = $(element);
      children = this.element.getChildren('dl');
      this.element.grab(children[0].clone(), 'bottom');
      this.element.grab(children[1].clone(), 'bottom');
      this.element.grab(children.getLast().clone(), 'top');
      this.element.grab(children[children.length - 2].clone(), 'top');
      this.images = this.element.getChildren('dl a');
      this.effect = new Fx.Scroll(this.element, {
        wheelstops: false
      });
      this.updateOffset();
      _ref = this.images;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        image = _ref[_i];
        image.addEvent('click', this.click.bind(this, image));
      }
      this.moveTo(this.images[Math.floor(Math.random() * this.images.length)], false);
      window.addEvent('resize', this.resize.bind(this));
    }
    Gallery.prototype.updateOffset = function() {
      return this.offset = this.element.getSize().x / 2 - (this.images[0].getSize().x / 2);
    };
    Gallery.prototype.moveTo = function(currentImage, animate) {
      this.currentImage = currentImage;
      if (animate == null) {
        animate = true;
      }
      if (animate) {
        if (this.currentImage === this.images[1]) {
          return this.effect.start(this.element.scrollLeft + this.currentImage.getPosition().x - this.offset, 0).chain(__bind(function() {
            return this.moveTo(this.images[this.images.length - 3], false);
          }, this));
        }
        if (this.currentImage === this.images[0]) {
          return this.effect.start(this.element.scrollLeft + this.currentImage.getPosition().x - this.offset, 0).chain(__bind(function() {
            return this.moveTo(this.images[this.images.length - 4], false);
          }, this));
        }
        if (this.currentImage === this.images[this.images.length - 2]) {
          return this.effect.start(this.element.scrollLeft + this.currentImage.getPosition().x - this.offset, 0).chain(__bind(function() {
            return this.moveTo(this.images[2], false);
          }, this));
        }
        if (this.currentImage === this.images.getLast()) {
          return this.effect.start(this.element.scrollLeft + this.currentImage.getPosition().x - this.offset, 0).chain(__bind(function() {
            return this.moveTo(this.images[3], false);
          }, this));
        }
        return this.effect.start(this.element.scrollLeft + this.currentImage.getPosition().x - this.offset, 0);
      } else {
        if (this.currentImage === this.images[0]) {
          return this.moveTo(this.images[this.images.length - 4], false);
        }
        if (this.currentImage === this.images[1]) {
          return this.moveTo(this.images[this.images.length - 3], false);
        }
        if (this.currentImage === this.images.getLast()) {
          return this.moveTo(this.images[3], false);
        }
        if (this.currentImage === this.images[this.images.length - 2]) {
          return this.moveTo(this.images[2], false);
        }
        return this.effect.set(this.element.scrollLeft + this.currentImage.getPosition().x - this.offset, 0);
      }
    };
    Gallery.prototype.resize = function() {
      this.updateOffset();
      return this.moveTo(this.currentImage, false);
    };
    Gallery.prototype.click = function(image, event) {
      this.moveTo(image);
      return event.stop();
    };
    return Gallery;
  })();
  window.addEvent('domready', function() {
    var gallery, _i, _len, _ref, _results;
    _ref = $$('.gallery');
    _results = [];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      gallery = _ref[_i];
      _results.push(new Gallery(gallery));
    }
    return _results;
  });
}).call(this);

