FieldHint = Class.create({
	initialize: function(element) {
		this.element = $(element);
		this.hint = this.element.value;
		this.type = this.element.getAttribute('type');
		this.element.observe('focus', this.clear.bindAsEventListener(this));
		this.element.observe('blur', this.restore.bindAsEventListener(this));
		if (this.type == 'password') {
			this.shadow = new Element('input', {type: 'text'});
			$A(['name', 'value', 'class']).each(this.copyAttribute.bind(this));
			this.element.parentNode.replaceChild(this.shadow, this.element);
			this.shadow.observe('focus', this.clear.bindAsEventListener(this));
			this.shadow.observe('blur', this.restore.bindAsEventListener(this));
		}
	},
	copyAttribute: function(attr) {
		this.shadow.writeAttribute(attr, this.element.readAttribute(attr));
	},
	focusElement: function() {
		this.element.focus();
	},
	clear: function(e) {
		if (this.element.value == this.hint) {
			this.element.clear();
			if (this.type == 'password') {
				this.shadow.parentNode.replaceChild(this.element, this.shadow);
				setTimeout(this.focusElement.bind(this), 10);
			}
		}
	},
	restore: function(e) {
		if (!this.element.present()) {
			this.element.value = this.hint;
			if (this.type == 'password') {
				this.element.parentNode.replaceChild(this.shadow, this.element);
			}
		}
	}
});
