// Include at the end of the page

if(!window.LB) window.LB = {};
if(!LB.autoLabel) {
  LB.autoLabel = function(){
    var blessInput = function(i,focus,blur){
      var oldF = i.onfocus;
      var oldB = i.onblur;
      i.onfocus = function(e){ return focus.call(i,e) && (oldF ? oldF.call(i,e) : true); };
      i.onblur  = function(e){ return blur.call(i,e) && (oldB ? oldB.call(i,e) : true); };
    };
    var onFocus = function(e){
      this.className = this.className.replace(/\blabeled\b/g,'');
      if(this.value==this.getAttribute('data-label')) this.value='';
      return true;
    };
    var onBlur = function(e){
      if(!this.value) {
        this.className += ' labeled';
        this.value = this.getAttribute('data-label');
      }
      return true;
    };
    var inputs = document.getElementsByTagName('input');
    for(var x=inputs.length; x--;) {
      var input = inputs[x];
      if(input.getAttribute('data-label')) {
        blessInput(input,onFocus,onBlur);
        onBlur.call(input);
      }
    }
  };
  LB.autoLabel();
}