/*
 * Mootools Class for the Quick Response Form
 * 
 */
var QuickResponseForm = new Class({
	
	Implements: [Options],  
	
	options: {
		defaults: {}
	},
	
	
	initialize: function(container, options) {
		
		this.setOptions(options);	
		
		if ($type(container) == 'element')
			this.container = container;
		else
			this.container = $(container);
			
		if (!this.container) 
			return false;
			
		this.initForm();
	},	
	
	initForm: function() {
		
		this.container.setStyle('cursor', '');
		
		// fix the form onSubmit if it's there
		this.formElem = this.container.getElement('form');
		if (this.formElem) {
			this.formElem.addEvent('submit', this.submitForm.bindWithEvent(this));
			
			// init the quick_name field
			$(this.formElem.quick_name).addEvents({
				
				'focus': function(elem) {
					if (this.options.defaults.quick_name && elem.value == this.options.defaults.quick_name) {
						elem.value = '';
					}
					elem.setStyle('color', '#fff');
				}.bind(this, this.formElem.quick_name),
				
				'blur': function(elem) {
					if (this.options.defaults.quick_name && elem.value == '') {
						elem.value = this.options.defaults.quick_name;
						elem.setStyle('color', '');					
					}
					elem.removeClass('error');
					
				}.bind(this, this.formElem.quick_name)
				
			});
			if (this.formElem.quick_name.value != this.options.defaults.quick_name && !this.formElem.quick_name.hasClass('error')) {
				this.formElem.quick_name.setStyle('color', '#fff');
			}
			
			
			// init the quick_email field
			$(this.formElem.quick_email).addEvents({
				
				'focus': function(elem) {
					if (this.options.defaults.quick_email && elem.value == this.options.defaults.quick_email) {
						elem.value = '';
					}
					elem.setStyle('color', '#fff');
				}.bind(this, this.formElem.quick_email),
				
				'blur': function(elem) {
					if (this.options.defaults.quick_email && elem.value == '') {
						elem.value = this.options.defaults.quick_email;
						elem.setStyle('color', '');
					}					
					elem.removeClass('error');
				}.bind(this, this.formElem.quick_email)
				
			});
			if (this.formElem.quick_email.value != this.options.defaults.quick_email && !this.formElem.quick_email.hasClass('error')) {
				this.formElem.quick_email.setStyle('color', '#fff');
			}
			
			
			// init the quick_phone field
			$(this.formElem.quick_phone).addEvents({
				
				'focus': function(elem) {
					if (this.options.defaults.quick_phone && elem.value == this.options.defaults.quick_phone) {
						elem.value = '';						
					}
					elem.setStyle('color', '#fff');
				}.bind(this, this.formElem.quick_phone),
				
				'blur': function(elem) {
					if (this.options.defaults.quick_phone && elem.value == '') {
						elem.value = this.options.defaults.quick_phone;	
						elem.setStyle('color', '');					
					}					
					elem.removeClass('error');
				}.bind(this, this.formElem.quick_phone)
				
			});
			if (this.formElem.quick_phone.value != this.options.defaults.quick_phone && !this.formElem.quick_phone.hasClass('error')) {
				this.formElem.quick_phone.setStyle('color', '#fff');
			}
						
			
			// init the quick_comments field
			$(this.formElem.quick_comments).addEvents({
				
				'focus': function(elem) {
					if (this.options.defaults.quick_comments && elem.value == this.options.defaults.quick_comments) {
						elem.value = '';
					}
					elem.setStyle('overflow', 'auto');
					elem.setStyle('color', '#fff');
					
				}.bind(this, this.formElem.quick_comments),
				
				'blur': function(elem) {
					if (this.options.defaults.quick_comments && elem.value == '') {
						elem.value = this.options.defaults.quick_comments;
						elem.setStyle('color', '');
					}					
					elem.removeClass('error');
				}.bind(this, this.formElem.quick_comments)
				
			});
			if (this.formElem.quick_comments.value != this.options.defaults.quick_comments && !this.formElem.quick_comments.hasClass('error')) {
				this.formElem.quick_comments.setStyle('color', '#fff');
			}
			
			
		}	
		
	}, 
	
	submitForm: function(event) {
		
		// cancel the default event
		event.preventDefault();							
		
		// send the form
		var data = this.formElem.toQueryString();
		var myHTMLRequest = new Request.HTML({
			url: this.formElem.action,
			onComplete: this.initForm.bind(this),
			update: this.container
		}).post(this.formElem);	
		
		//
		this.container.setStyle('cursor', 'wait');
		$('quick-response-submit').setStyle('cursor', 'wait');
	
	
	}	
});

	