/* Attches events and labels to an existing text input element.
 * @dependency  addEvent
 * @dependency  hitch
 * @dependency  addClass, removeClass, hasClass
 */
WidgetInput = function(idOfInput, label)
{
	this.label = label;
	this.input = document.getElementById(idOfInput);
	this.isEmpty = true;
	this.listeners = {};
	this.validListeners = ['onBlur'];
	this.init();
}
WidgetInput.prototype.version = 2;	// accepts listeners

WidgetInput.prototype.init = function()
{
	MWL.addEvent(this.input, "focus", MWL.hitch(this, this.inputFocus));
	MWL.addEvent(this.input, "blur", MWL.hitch(this, this.inputBlur));
	if ("" == this.input.value || this.label == this.input.value)
	{
		this.input.value = this.label;
		MWL.addClass(this.input, "empty");
		this.isEmpty = true;
	}
	for (var i = 0; i < this.validListeners.length; i++)
   {
   	this.listeners[this.validListeners[i]] = [];
   }
}

WidgetInput.prototype.inputFocus = function(e, el, data)
{
	if (MWL.hasClass(el, "empty"))
	{
		MWL.removeClass(el, "empty");
		if (el.value == this.label)
		{
			el.value = "";
		}
		this.isEmpty = false;
	}
}

WidgetInput.prototype.inputBlur = function(e, el, data)
{
	if (el.value == "")
	{
		el.value = this.label;
		MWL.addClass(this.input, "empty");
		this.isEmpty = true;
	}
	this.fireListener('onBlur');
}

WidgetInput.prototype.fireListener = function(listenerName)
{
	for (var i = 0; i < this.listeners[listenerName].length; i++)
   {
   	var L = this.listeners[listenerName][i];
		L({isEmpty: this.isEmpty});
   }
}

WidgetInput.prototype.addListener = function(listenerName, fn)
{
	for (var i = 0; i < this.validListeners.length; i++)
   {
   	if (listenerName == this.validListeners[i])
   	{
   		this.listeners[listenerName].push(fn);
   	}
   }
}


