/*
Compact Forms (for Prototype)
http://www.alistapart.com/articles/makingcompactformsmoreaccessible/
*/
OverLabel = Class.create();
OverLabel.prototype = {
  label: null,
  target: null,

  initialize: function(label) {
    this.label = $(label);
    this.target = $(this.label.htmlFor || this.label.getAttribute('for'));
    this.label.addClassName('overlabel-apply');
    this.hide_or_show(); // initialize the state
    this.target.observe('focus', this.hide.bind(this));
    this.target.observe('blur', this.hide_or_show.bind(this));

    this.label.observe('click', function() {
      this.target.focus();
    }.bind(this));
  },

  hide_or_show: function() {
    if (this.target.value) this.hide();
    else this.show();
  },

  hide: function() {
    this.label.style.textIndent = '-2000px';
  },
  show: function() {
    this.label.style.textIndent = '0px';
  }
}

OverLabel.initialize_all = function() {
  $$('label.overlabel').each(function(label) {
    if (label.htmlFor || label.getAttribute('for')) new OverLabel(label);
  });
}

Event.observe(window, 'load', function() {
  setTimeout(OverLabel.initialize_all, 50)
});