/**
 * Scripts for handling contact form effects
 */
var contactForm = {
	/**
	 * The location URL of the handler
	 */
	handler : '/contact/form_process',
	/**
	 * The reference to the entire form and notice container
	 */
	container : null,
	/**
	 * The contact form container reference
	 */
	formContainer : null,
	/**
	 * Instance of the notice object
	 */
	notice : null,
	/**
	 * Function to return the value of the specified field
	 * @param String The field name
	 */
	getFieldValue : function (field_name) {
		/* Get some vars:
		 * - default value of the field if there is one
		 * - current value of the field */
		var defValue = (defaultFormValues && defaultFormValues[field_name]) ? defaultFormValues[field_name] : '',
			curValue = contactForm.formContainer.find('*[name=' + field_name + ']').val();
		
		/* Check what to return */
		return (defValue == curValue) ? '' : curValue;
	},
	/**
	 * Handler for when the user clicks the notice close button
	 */
	onCloseClick : function () {
		/* Remove the click handler on the button for good measure */
		$('button#notice-button').unbind('click', this.onCloseClick);
		
		/* Close up the notice and show the form container */
		contactForm.notice.hide({
			speed : 500
		});
		contactForm.formContainer.fadeIn(500);
	},
	/**
	 * When a response from the server is received
	 */
	onResponse : function (data, textStatus) {
		/* Check the response */
		var message = '';
		if (data && (data.status == true || data.status == false)) {
			/* We're good to go */
			if (data.messages && data.messages.length > 0) {
				message += '<ul>';
				message += '<li class="heading">';
				message += (data.status == true) ? '<h4>Thanks!</h4>' : '<h4>Oops</h4>';
				message += '</li>';
				for (var inc = 0; inc < data.messages.length; inc++) {
					message += '<li>' + data.messages[inc] + '</li>';
				}
				message += '</ul>';
			} else {
				message += '<h4>Oops</h4>';
				message += '<p>Looks like there were no messages returned from the server.</p>';
			}
		} else {
			/* We can not work with this result, is not possible! */
			message += '<h4>Oops</h4>';
			message += '<p>The response from the server was not recognized.</p>';
		}
		
		/* Add on the closing buttin */
		message += '<button type="button" id="notice-button">Close</button>';
		
		/* Show the message and hook the button */
		contactForm.notice.set(message);
		$('button#notice-button').click(contactForm.onCloseClick);
	},
	/**
	 * Runs when the form is submitted
	 */
	onSubmit : function () {
		/* Collect the form data */
		var data = {
			'fld_fname' : contactForm.getFieldValue('fld_fname'),
			'fld_lname' : contactForm.getFieldValue('fld_lname'),
			'fld_email' : contactForm.getFieldValue('fld_email'),
			'fld_item_1' : contactForm.getFieldValue('fld_item_1'),
			'fld_item_2' : contactForm.getFieldValue('fld_item_2'),
			'fld_question_1' : contactForm.getFieldValue('fld_question_1'),
			'fld_question_2' : contactForm.getFieldValue('fld_question_2'),
			'fld_message' : contactForm.getFieldValue('fld_message'),
			'returnType' : 'json'
		}
		
		/* Send the data off */
		$.post(contactForm.handler, data, contactForm.onResponse, 'json');
		
		/* Show the message */
		contactForm.formContainer.fadeOut(500);
		contactForm.notice.show({
			'text' : 'Submitting the form...',
			'speed' : 500
		});
		
		/* Break normal submission */
		return false;
	},
	/**
	 * Function to set up the form
	 */
	setup : function () {
		/* Make references */
		contactForm.container = $('#contact-form');
		contactForm.formContainer = $('#contact-form-container');
		
		/* Create a new notice object */
		contactForm.notice = new ContactNotice({
			headerImageSrc : '/design/sub-contact-light.png',
			headerImageAlt : 'Drop Us a Line',
			container : contactForm.container
		});
		
		/* Hook submit */
		$('form#contact-form').submit(contactForm.onSubmit);
		
		/* Hook up the fields */
		contactForm.setupFields();
	},
	/**
	 * Sets up default field values
	 */
	setupFields : function () {
		/* Check to prevent errors, keeping it pretty */
		if (!defaultFormValues) {
			return;
		}
		
		/* Loop through the field values */
		for (var fld in defaultFormValues) {
			/* Get the reference and check it */
			var field = $('*[name=' + fld + ']');
			if (field.length <= 0) {
				continue;
			}
			
			/* Get the default value and apply it */
			var defValue = defaultFormValues[fld];
			field.val(defValue);
			
			/* Now apply focus and blur listeners */
			field.focus(function () {
				/* See if we have the default value */
				var defVal = defaultFormValues[$(this).attr('name')];
				if ($(this).val() == defVal) {
					$(this).val('');
				}
			});
			field.blur(function () {
				/* See if we need to apply the default value */
				var defVal = defaultFormValues[$(this).attr('name')];
				if ($(this).val() == '') {
					$(this).val(defVal);
				}
			});
		}
	}
}

/* See if we need to hook anything */
$(function () {
	if ($('#contact-form').length > 0) {
		contactForm.setup();
	}
});
