/*
 * replaces the value of a text-input with the innerHTML of it's label, when the value is empty
 */
var LabelReplacement = new Class({

	Implements: [Events, Options],
	
	initialize: function(elId) {
		this.el = $(elId);
		if (!this.el || this.el.nodeName != 'INPUT' || this.el.getProperty('type') != 'text') {
			return;
		}
		var labels = $$('label[for="' + elId + '"]');
		if (!labels.length) {
			return;
		}
		this.label = labels[0];

		if (this.isEmpty(this.el.getProperty('value'))) {
				this.el.setProperty('value', this.label.innerHTML);
		}
		
		this.el.addEvent('focus', function() {
			if (this.el.getProperty('value') == this.label.innerHTML) {
				this.el.setProperty('value', '');
			}
		}.bind(this));

		this.el.addEvent('blur', function() {
			if (this.isEmpty(this.el.getProperty('value'))) {
				this.el.setProperty('value', this.label.innerHTML);
			}
		}.bind(this));
		
	},
	
	isEmpty: function(s) {
		if (!s) {
			return true;
		}
		var ts = s.replace (/^\s+/, '').replace (/\s+$/, '');
		return (ts.length == 0);
	}

});