var ImageFlip;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
ImageFlip = (function() {
  function ImageFlip(options) {
    this.options = options;
    this.arrow_height = 90;
    this.arrow_anim_speed = 200;
    this.arrow_dip = 4;
    if (this.options.exclude) {
      this.exclude = this.options.exclude.join(', ');
    }
    this.init_flip();
  }
  ImageFlip.prototype.init_flip = function() {
    this.div = this.options.div.addClass('imageflip');
    this.images = this.div.children();
    this.image_length = this.images.length;
    this.wrap = this.setup_image_area();
    this.left_arrow = $('.left_arrow', this.wrap);
    this.right_arrow = $('.right_arrow', this.wrap);
    this.wrap.css({
      height: this.options.height,
      width: this.options.width
    });
    this.options.div.css({
      height: this.options.height + 30,
      width: this.options.width
    });
    this.add_nubbins();
    return this.setup_events();
  };
  ImageFlip.prototype.add_nubbins = function() {
    var all_nubs, nub_ct, nubbins_html;
    all_nubs = (function() {
      var _ref, _results;
      _results = [];
      for (nub_ct = 1, _ref = this.images.length; 1 <= _ref ? nub_ct <= _ref : nub_ct >= _ref; 1 <= _ref ? nub_ct++ : nub_ct--) {
        _results.push("<div id='flip_" + nub_ct + "'></div>");
      }
      return _results;
    }).call(this);
    nubbins_html = "<div class='nubbins'>" + (all_nubs.join('')) + "</div>";
    this.wrap.after(nubbins_html);
    this.nubbins = this.wrap.next();
    return this.nubbins.children().first().addClass('active');
  };
  ImageFlip.prototype.setup_image_area = function() {
    this.images.wrapAll('<a href="#" class="flip_content" />');
    this.images.wrapAll('<div class="flip_images" />');
    this.images.first().addClass('active').fadeIn();
    $.each(this.images, function(ct, image) {
      return image.id = "flip_" + (ct + 1);
    });
    return this.div.children('.flip_content').append('<div class="right_arrow" /><div class="left_arrow" />');
  };
  ImageFlip.prototype.setup_events = function() {
    this.wrap.mouseenter(__bind(function(event) {
      return this.on_hover(event);
    }, this)).mouseleave(__bind(function(event) {
      return this.exit_hover(event);
    }, this)).mousedown(__bind(function(event) {
      return this.mouse_down(event);
    }, this)).mouseup(__bind(function(event) {
      return this.mouse_up(event);
    }, this)).click(__bind(function(event) {
      return this.clicked(event);
    }, this));
    this.wrap.children('.flip_images').mousemove(__bind(function(event) {
      return this.mouse_moved(event);
    }, this));
    return this.nubbins.click(__bind(function(event) {
      return this.nubbin_click(event);
    }, this));
  };
  ImageFlip.prototype.nubbin_click = function(event) {
    this.swap_images($("#" + event.target.id, this.wrap), $('.active', this.wrap));
    return false;
  };
  ImageFlip.prototype.clicked = function(event) {
    var $active, prev;
    if ($(event.target).is(this.exclude)) {
      return false;
    }
    $active = $('.active', this.wrap);
    if (this.active_side === 'left') {
      prev = $active.prev().length ? $active.prev() : this.images.last();
    } else {
      prev = $active.next().length ? $active.next() : this.images.first();
    }
    this.swap_images(prev, $active);
    return false;
  };
  ImageFlip.prototype.get_id_num = function($dom) {
    return parseInt($dom[0].id.split('_')[1]);
  };
  ImageFlip.prototype.swap_images = function(active, prev) {
    prev.removeClass('active');
    active.addClass('active');
    $("#" + active[0].id, this.nubbins).addClass('active');
    $("#" + prev[0].id, this.nubbins).removeClass('active');
    if (this.get_id_num(active) > this.get_id_num(prev)) {
      return active.fadeIn(function() {
        return prev.hide();
      });
    } else {
      active.show();
      return prev.fadeOut();
    }
  };
  ImageFlip.prototype.on_hover = function(event) {
    return this.hovering = true;
  };
  ImageFlip.prototype.exit_hover = function(event) {
    this.active_side = '';
    this.hovering = false;
    return this.drop_arrows();
  };
  ImageFlip.prototype.drop_arrows = function() {
    this.right_arrow.removeClass('hovered');
    return this.left_arrow.removeClass('hovered');
  };
  ImageFlip.prototype.mouse_up = function(event) {
    if (this.left_arrow.css('bottom') === ("-" + this.arrow_dip + "px")) {
      return this.left_arrow.css('bottom', 0);
    } else if (this.right_arrow.css('bottom') === ("-" + this.arrow_dip + "px")) {
      return this.right_arrow.css('bottom', 0);
    }
  };
  ImageFlip.prototype.mouse_down = function(event) {
    if (this.active_side === 'left') {
      if (this.left_arrow.css('bottom') === '0px') {
        return this.left_arrow.css('bottom', -this.arrow_dip);
      }
    } else {
      if (this.right_arrow.css('bottom') === '0px') {
        return this.right_arrow.css('bottom', -this.arrow_dip);
      }
    }
  };
  ImageFlip.prototype.mouse_moved = function(event) {
    var normal_pos, offset_pos;
    if (this.hovering) {
      if ($(event.target).is(this.exclude)) {
        this.drop_arrows();
        return;
      }
      offset_pos = event.offsetX != null ? event.offsetX : event.layerX;
      normal_pos = offset_pos / this.options.width;
      if (normal_pos < 0.5 && this.active_side !== 'left') {
        return this.left_hover();
      } else if (normal_pos > 0.5 && this.active_side !== 'right') {
        return this.right_hover();
      }
    }
  };
  ImageFlip.prototype.left_hover = function() {
    this.left_arrow.addClass('hovered');
    this.right_arrow.removeClass('hovered');
    return this.active_side = 'left';
  };
  ImageFlip.prototype.right_hover = function() {
    this.right_arrow.addClass('hovered');
    this.left_arrow.removeClass('hovered');
    return this.active_side = 'right';
  };
  return ImageFlip;
})();
