/*
Script: bumper.js
	Contains <Bumper>

Author:
	Alan Roemen
	June 8, 2010

Class: Bumper
	A javascript horizontal bumper

Options:
*/
var Bumper = new Class({
	options: {
		bumps: 10,
		wheel: true,
    offset: 0,
    bump_speed: 50,
		bumpers: {
			left: 'bump-left',
			right: 'bump-right'
		},
		onBump: Class.empty
	},

	initialize: function(content, options) {
    this.container = $(content);
    if (!this.container) return;
		this.setOptions(options);
    this.bumping = false;

    // Wrap Elements
    this.max = this.min = 0;
    this.wrapper = new Element('div', {'styles': {'width': '0px'}});
    this.wrapper.adopt(this.container.getChildren());
    this.wrapper.inject(this.container);
    this.wrapper.getChildren().each(function(el, i){
      this.min += el.getCoordinates().width.toInt();
    }.bind(this));
    this.wrapper.setStyle('width', this.min);
    this.min = (this.min + this.options.offset - this.container.getCoordinates().width.toInt()) * -1;
    this.at = this.max = this.wrapper.getStyle('margin-left').toInt();

    // Setup Bumpers
    this.bumper = {left: false, right: false}
    if (this.options.bumpers.left !== false)
      this.bumper.left = $(this.options.bumpers.left);
    if (this.options.bumpers.right !== false)
      this.bumper.right = $(this.options.bumpers.right);

    // Setup Events
    if (this.bumper.left)
      this.bumper.left.addEvents({
				'mousedown': this.bump.bindWithEvent(this, 'left'),
				'click': this.bumpit.bindWithEvent(this, 'left'),
        'dblclick': this.bump_out.bindWithEvent(this, 'left'),
				'mouseout': this.speed_bump.bindWithEvent(this),
				'mouseup': this.speed_bump.bindWithEvent(this)
      });
    if (this.bumper.right)
      this.bumper.right.addEvents({
				'mousedown': this.bump.bindWithEvent(this, 'right'),
        'dblclick': this.bump_out.bindWithEvent(this, 'right'),
				'mouseout': this.speed_bump.bindWithEvent(this),
				'mouseup': this.speed_bump.bindWithEvent(this)
      });
    if (this.options.wheel)
      this.container.addEvent('mousewheel', this.wheel_scroll.bindWithEvent(this));
	},

  speed_bump: function() {
    $clear(this.bumping);
    this.bumping = false;
  },

  bump: function(e, dir) {
    if (e) {
			e = new Event(e);
			if (e.type == 'click') e.stop();
		}
    if (this.bumping !== false) return;
    this.bumping = this.bumpit.periodical(this.options.bump_speed, this, [e, dir]);
  },

  wheel_scroll: function(e) {
    var e2 = new Event(e);
		e2.stop();
    if (e2.wheel > 0)
      this.bumpit(e, 'left');
    else this.bumpit(e, 'right');
  },

  bumpit: function(e, dir) {
    if (e) {
			e = new Event(e);
			if (e.type == 'click') e.stop();
		}
    switch (dir) {
      case 'left': this.at += this.options.bumps; break;
      case 'right': this.at -= this.options.bumps; break;
    }
    this.go();
  },

  bump_out: function(e, dir) {
    if (e) {
			e = new Event(e);
			if (e.type == 'click') e.stop();
		}
    this.speed_bump();
    this.at = dir == 'left' ? this.max : this.min;
    this.go();
  },

  go: function() {
    if (this.at > this.max) this.at = this.max;
    if (this.at < this.min) this.at = this.min;
    this.wrapper.setStyle('margin-left', this.at);
  }
});

Bumper.implement(new Options, new Events);