if(!Nibynic) {
	var Nibynic = {}
}

Nibynic.HintedInput = Class.create({
	defaultOptions: {
		hint: '',
		hintClass: 'hint'
	},
	
	initialize: function(id, options) {
		this.id = id;

		this.element = $(this.id);
		if(!this.element) {
			throw('Element "'+this.id+'" not found.');
		}
		
		this.options = Object.extend(Object.extend({ },this.defaultOptions), options || { });
		
		this.label = new Element('label', {
			'for': this.element.id,
			className: this.options.hintClass,
			style: 'position: absolute; padding: 2px'
		}).update(this.options.hint);
		this.element.insert({ after: this.label });
		
		this.focused = false;
		
		new Form.Element.Observer(this.element, 0.33, function() {
		  	this.hideHint();
		}.bind(this));
		this.label.observe('click', function() {
		  	this.hideHint();
		}.bind(this));
		
		new PeriodicalExecuter(function() {
		  	this.showHint();
		}.bind(this), 0.33);
		this.element.observe('blur', function() {
			this.focused = false;
		  	this.showHint();
		}.bind(this));
		
		this.element.observe('focus', function() {
			this.focused = true;
		}.bind(this));

		this.showHint();
	},
	
	showHint: function() {
		if (!this.focused && (this.element.value == '' || this.element.value == this.options.hint)) {
			this.label.clonePosition(this.element);
			this.label.show();
		}

	},
	
	hideHint: function() {
		if (this.element.value != this.options.hint) {
			this.label.hide();
		}
	}
})
