//= require <mootools-core-1.3-full-nocompat>
//= require <uiframework/UICEvent>
//= require <uiframework/UIComponent>
var FormItem = function()
{
	this.Extends = UIComponent;

	this._label = null;
	this._input = null;
	this._labelWidth = 75;
	this._horizontalGap = 5;
	this._valid = true;

	this.initialize = function(text /* String */, options /* Object */)
	{
		this.parent("div", options);
		if (text) this.label().set("text", text);
	};

	this.label = function()
	{
		if (!this._label )
		{
			this._label = new UIComponent("p", {"class":"formItemLabel"});
			this.addChild(this._label);
		}
		return this._label;
	};

	this.input = function()
	{
		if (!this._input)
		{
			this._input = new UIComponent("input", {"class":"formItemInput"});
			this._input.addEventListener("keyup", this.keyPressHandler, false, 0, this);
			this.addChild(this._input);
		}
		return this._input;
	};
	
	this.keyPressHandler = function()
	{
		if (this.hasEventListener("input"))
			this.dispatchEvent(new UICEvent("input", false, true, this._input.get("value")));
	};

	this.measure = function()
	{
		this._measuredWidth = 175;
		this._measuredHeight = 17;
	};

	this.createChildren = function()
	{
		if (!this._label) this.label();
		if (!this._input) this.input();
	};

	this.updateDisplayList = function(width, height)
	{
		this.parent(width, height);
		var _w = width;
		var _x = 0;

		this.label().width(this._labelWidth);
		
		_w -= (this._labelWidth + this._horizontalGap);
		_x = this._labelWidth + this._horizontalGap;

		this.input().setActualSize(_w, this._input.measuredHeight() - 5);
		this._input.move(_x, 0);
	};

	this.labelWidth = function(value /* Number */)
	{
		if (arguments.length && this._labelWidth != value)
		{
			this._labelWidth = value;

			this.invalidateDisplayList();
		}
		return this._labelWidth;
	};

	this.horizontalGap = function(value /* Number */)
	{
		if (arguments.length && this._horizontalGap != value)
		{
			this._horizontalGap = value;

			this.invalidateDisplayList();
		}
		return this._horizontalGap;
	};

	this.valid = function(value /* Boolean */)
	{
		if (arguments.length && this._valid != value)
		{
			this._valid = value;
			var clazz = this._valid ? "formItemInput" : "formItemInputInvalid";
			this.input().set("class", clazz);
		}
	}
}
FormItem = new Class(new FormItem());
