/*
 * 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],
	
	label: '',
	
	type: 'text',
	
	initialize: function(elId) {
		this.el = $(elId);
		if (!this.el || this.el.nodeName != 'INPUT' || 
				(this.el.getProperty('type') != 'text'
				&& this.el.getProperty('type') != 'password')) {
			return;
		}
		this.type = this.el.getProperty('type');
		this.label = this.el.getProperty('value');
		
		if (this.type == 'password') {
			//this.el.setProperty('type', 'text');
			//this.type == 'text'
		}
		
		this.el.addEvent('focus', function() {
			this.el.setProperty('type', this.type);
			if (this.el.getProperty('value') == this.label) {
				this.el.setProperty('value', '');
			}
		}.bind(this));

		this.el.addEvent('blur', function() {
			if (this.isEmpty(this.el.getProperty('value'))) {
				this.el.setProperty('value', this.label);
				//this.el.setProperty('type', 'text');
			}
		}.bind(this));
		
	},
	
	isEmpty: function(s) {
		if (!s) {
			return true;
		}
		var ts = s.replace (/^\s+/, '').replace (/\s+$/, '');
		return (ts.length == 0);
	}

});
