$.ui.component.subclass('ui.form', {
	_init: function() {
		var self = this, 
			options = this.options, 
			form = this.form = self.element, 
			formHeader = this.formHeader = form.find(' > .' + options.headerClass), 
			formBody = this.formBody = form.find('> .' + options.bodyClass), 
			formBodyFieldSet = this.formBodyFieldSet = formBody.find(' > form').find('.' + options.bodyFieldSetClass), 
			formBodyFieldSetContent = this.formBodyFieldSetContent = formBodyFieldSet.find('.' + options.bodyFieldSetContentClass), 
			formBodyButtonSet = this.formBodyButtonSet = formBody.find('> .' + options.bodyButtonSetClass), 
			formFooter = this.formFooter = form.find('> .' + options.footerClass);
		this.create();
	}, 
	hide: function(event) {
		if (this.options.dialog) this.form.dialog('close');
		this.form.hide();
	}, 
	show: function(event) {
		if (this.options.dialog) this.form.dialog('open');
		this.form.show();
	}, 
	close: function() {
		this.hide();
	}, 
	reset_: function() {
		return this.form.find('form').clearForm();
	}, 
	reset: function() {
		if (confirm('Clear all entered information?')) return this.reset_();
		else return false;
	}, 
	create: function(event) {
		this.createHeader();
		this.createBody();
		if (this.options.dialog) form.dialog();
		this.createFooter();
	}, 
	destroy: function(event) {
		if (this.options.dialog) this.form.dialog('destroy');
		this.destroyHeader();
		this.destroyBody();
		this.destroyFooter();
	}, 
	getBodyObject: function(selector) {
		return this.formBody.find(selector);
	}, 
	// Header
	createHeader: function() {
		var self = this;
		this.formHeader.find('.close').each(function() {
			$(this).unbind('click');
			$(this).bind('click', function() {self.close();});
		});
	}, 
	destroyHeader: function() {
		this.formHeader.find('.close').each(function() {$(this).unbind('click');});
	}, 
	// Body
	createBody: function() {
		this.createFieldSet();
		this.createButtonSet();
	}, 
	destroyBody: function() {
		this.destroyButtonSet();
		this.destroyFieldSet();
	}, 
	// Footer
	createFooter: function() {
		
	}, 
	destroyFooter: function() {}, 
	// FieldSet
	createFieldSet: function() {
		var self = this;
		this.formBodyFieldSetContent.find('.help').tooltip();
		this.formBodyFieldSetContent.find('.error').each(function() {
			$(this).unbind();
			$(this).bind('click', function() {
				$(this).fadeOut(500);	
			});
		});
		this.formBodyFieldSetContent.find('.messager').each(function() {
			$(this).corners('8px transparent no-native');
			$(this).find('.message-set').corners('6px transparent no-native');
			$(this).unbind();
			$(this).bind('click', function() {
				$(this).fadeOut(500);	
			});
		});
		this.form.find('form').unbind('keypress');
		this.form.find('form').bind('keypress', function (event) {
			if (event.keyCode == 13) {
				if (event.target && event.target.tagName && (String(event.target.tagName).toUpperCase() != 'TEXTAREA')) {
					self.submit();
					return false;
				} else return true;
			}
		});
	}, 
	destroyFieldSet: function() {
		this.formBodyFieldSetContent.find('.error').each(function() {$(this).unbind();});
		this.formBodyFieldSetContent.find('.messager').each(function() {$(this).unbind();});
		this.formBodyFieldSetContent.find('.messager').remove();
	}, 
	recreateFieldSet: function(content) {
		this.destroyFieldSet();
		this.formBodyFieldSetContent.html(content);
		this.createFieldSet();
	}, 
	// Button Set
	createButtonSet: function() {
		var self = this;
		this.formBodyButtonSet.find('.submit').each(function() {
			$(this).bind('click', function() {self.submit(); return false;});
		});
		this.formBodyButtonSet.find('.reset').each(function() {
			$(this).bind('click', function() {self.reset(); return false;});
		});
		this.formBodyButtonSet.find('.close').each(function() {
			$(this).bind('click', function() {self.close(); return false;});
		});
	}, 
	destroyButtonSet: function() {
		this.formBodyButtonSet.find('.submit').each(function() {$(this).unbind('click');});
		this.formBodyButtonSet.find('.reset').each(function() {$(this).unbind('click');});
		this.formBodyButtonSet.find('.close').each(function() {$(this).unbind('click');});
	}, 
	
	getURL: function() {
		return this.form.find('form').attr('action');
	}, 
	submitHandler: function(responce) {
		if (responce) {
			if (responce.redirect_url) {
				return window.location = responce.redirect_url;
			}
			if (responce.html) {
				if (responce.html.fieldset_content) this.recreateFieldSet(responce.html.fieldset_content);
			}
		}
	}, 
	post: function(func, query) {
		var url = this.getURL();
		if (query) {
			url += query;
		}
		$.request({
			url: url, 
			type: 'post', 
			form: this.form.find('form'), 
			container: this.form, 
			scope: this, 
			func: func
		});
	}, 
	submit: function(query) {
		this.post(this.submitHandler, query);
	}
});
$.ui.form.defaults.extend(
	{
		headerClass: 'form-header', 
		bodyClass: 'form-body', 
		bodyFieldSetClass: 'form-body-fieldset', 
		bodyFieldSetContentClass: 'form-body-fieldset-content', 
		bodyButtonSetClass: 'form-body-buttonset', 
		footerClass: 'form-footer', 
		dialog: false
	}
);