// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
InfoTicker = Class.create({
  initialize: function(elt) {
    this.element = $(elt);
    if(this.makeEllipsis()) {
      Event.observe(this.element, 'mouseover', this.start.bind(this));
      Event.observe(this.element, 'mouseout', this.stop.bind(this));
    }
  },
  makeEllipsis: function() {
    var aw = this.element.getDimensions().width;
    var lw = $(this.element.parentNode).getDimensions().width;
    if(aw > lw) {
      var s = document.createElement('span');
      s.style.position = 'absolute';
      s.style.top = 0;
      s.style.right = 0;
      s.style.backgroundColor = '#FFFFFF';
      s.appendChild(document.createTextNode('\u00A0...'));
      this.element.InfoTicker_aw = aw;
      this.element.parentNode.appendChild(s);
      this.element.innerHTML += InfoTicker.Spacer + this.element.innerHTML + InfoTicker.Spacer;
      if(Prototype.Browser.IE) this.element.style.display = 'block';
      this.ellipsis = $(s);
      return true;
    }
    return false;
  },
  start: function() {
    var aw = this.element.InfoTicker_aw;
    var lw = $(this.element.parentNode).getDimensions().width;
    if(aw > lw) {
      Effect.Queue.invoke('cancel');
      new Effect.Move(this.element, {
        x: -aw / 2, y: 0,
        beforeStart: this.onBeforeStart.bind(this),
        afterFinish: this.start.bind(this),
        duration: (aw / 2) / 100.0 * 2.0,
        transition: Effect.Transitions.linear
      });
    }
  },
  stop: function() {
    Effect.Queue.invoke('cancel');
    this.element.undoPositioned();
    this.element.style.top = this.element.style.left = 0;
    this.ellipsis.show();
  },
  onBeforeStart: function() {
    this.element.makePositioned();
    this.element.style.top = this.element.style.left = 0;
    this.ellipsis.hide();
  }
});
Object.extend(InfoTicker, {
  Spacer: '\u00A0\u00A0\u00A0',
  install: function() {
    $A(arguments).each(function(selector) { $$(selector).each(function(a) { new InfoTicker(a); }); });
  }
});
