var prefillField = new Class.create();

prefillField.prototype = {
  field:     null,
  value:     null,
  className: null,

  initialize: function(field, value, className) {
    if ($(field)) {
      this.field = $(field);
      this.value = value;
      this.className = className || 'field_default_value';
      this.unselect();

      this.field.observe('blur', function(event) {
        this.unselect();
      }.bind(this));
      this.field.observe('focus', function(event) {
        this.select();
      }.bind(this));
      this.field.observe('widget:submit', function(event) {
        this.submit();
      }.bind(this));
    }
  },

  unselect: function(){
    if (this.field.value == "") {
      this.field.value = this.value;
      this.field.addClassName(this.className);
    }
  },

  select: function(){
    if (this.field.value == this.value) {
      this.field.value = "";
      this.field.removeClassName(this.className);
    }
  },

  submit: function() {
    this.field.stopObserving('blur');
    this.select();
  }
};
