/**
 * @name QuickForm
 * @author Rick Hopkins
 * @classDescription
 * 	Creates a widget for a quick and easy form
 *	Requires the MooTools Library
 * @copyright 
 * 	copyright (c) 2008 Rick Hopkins
 * 	MIT-style license.
 */

var QuickForm = new Class({
	/** set the default options */
	options: {
		id: 'QuickForm_' + $random(0, 10000), 
		title: '', 
		url: false, 
		content: false, 
		data: {}, 
		onComplete: $empty
	}, 
	
	/** initialize the object */
	initialize: function(options){
		/** do some prep */
		if (!options.url && !options.content) return false;
		this.setOptions(options);
		
		if (!$('QuickFormCss')) new Asset.css('/js/widgets/QuickForm/css/QuickForm.css', {'id':'QuickFormCss', 'media': 'all', 'type': 'text/css'});
		
		/** clear all previously made QuickForms */
		$$('div.QuickForm').each(function(s){ s.destroy(); });
		
		/** create the shell */
		this.shell = new Element('div', {
			'id': this.options.id, 
			'class': 'QuickForm', 
			'styles': {
				'position': (Browser.Engine.trident4 ? 'absolute' : 'fixed'), 
				'top': '-410px', 
				'left': '0px', 
				'width': '390px', 
				'height': 'auto', 
				'background': 'transparent url("/js/widgets/QuickForm/images/QuickForm-ex.png") no-repeat top left', 
				'padding': '10px 10px 0px 10px'
			}
		}).adopt(
			new Element('div', {
				'class': 'QuickForm-Bottom', 
				'styles': {
					'position': 'absolute', 
					'bottom': '-15px', 
					'left': '0px', 
					'width': '410px', 
					'height': '15px', 
					'background': 'transparent url("/js/widgets/QuickForm/images/QuickForm-ex.png") no-repeat bottom left'
				}
			})
		).inject(document.body);
		
		/** build the QuickForm */
		this.build();
		
		/** move the widget into position */
		this.locate();
	}, 
	
	/** build the QuickForm table */
	build: function(){
		new Element('table', {'styles': {'width': '100%'}}).adopt(
			new Element('tbody').adopt(
				new Element('tr').adopt(
					new Element('td', {
						'class': 'QuickForm-Title'
					}).set('text', this.options.title)
				).adopt(
					new Element('td', {
						'class': 'QuickForm-Closer'
					}).adopt(
						new Element('div', {
							'class': 'Closer-Btn', 
							'title': 'Close', 
							'styles': {
								'float': 'right', 
								'cursor': 'pointer', 
								'width': '18px', 
								'height': '18px', 
								'background': 'transparent url("/js/widgets/QuickForm/images/cross.png") no-repeat center center'
							}
						}).addEvent('click', function(){
							this.close();
						}.bind(this))
					)
				)
			).adopt(
				new Element('tr').adopt(
					new Element('td', {
						'class': 'QuickForm-Body', 
						'colspan': '2'
					}).set('text', 'Loading...')
				)
			).adopt(
				new Element('tr').adopt(
					new Element('td', {
						'class': 'QuickForm-Footer', 
						'colspan': '2'
					})
				)
			)
		).inject(this.shell, 'bottom');
	}, 
	
	/** locate the widget on the page, load content */
	locate: function(){
		this.shell.setStyle('left', ((window.getCoordinates().width / 2) - 205));
		new Fx.Tween(this.shell, {onComplete: function(){
			/** get the content */
			if (this.options.content){
				this.shell.getElement('td.QuickForm-Body').empty().adopt(this.options.content);
				this.options.onComplete();
			} else {
				new Request.HTML({url: this.options.url, onComplete: function(r1, r2, r3, r4){
					this.shell.getElement('td.QuickForm-Body').set('html', r3);
					this.options.onComplete();
				}.bind(this)}).post(this.options.data);
			}
		}.bind(this)}).start('top', '20px');
	}, 
	
	/** close the quick form */
	close: function(){
		new Fx.Tween(this.shell, {onComplete: function(){
			this.shell.destroy();
		}.bind(this)}).start('top', '-410px');
	}
});

/** Implement Events & Options */
QuickForm.implement(new Options);