/**
 * Scripts for handling booking form effects
 */
var bookingForm = {
	/**
	 * The location URL of the handler
	 */
	handler : '/contact/book_us_process',
	/**
	 * The form container object
	 */
	container : null,
	/**
	 * The reference to the form container object
	 */
	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 = bookingForm.formContainer.find('*[name=' + field_name + ']').val();
		
		/* Check what to return */
		return (defValue == curValue) ? '' : curValue;
	},
	/**
	 * Responds to clicking the calendar button for date selection
	 */
	onCalendarClick : function () {
		/* Show the calendar */
		$('input[name=hidden_date]').datepicker('show');
		
		/* Break the normal button routine */
		return false;
	},
	/**
	 * 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 */
		bookingForm.notice.hide({
			speed : 500
		});
		bookingForm.formContainer.fadeIn(500);
	},
	/**
	 * Handles the response from the server after submission
	 * @param Object The returned response data 
	 */
	onResponse : function (data, textStatus) {
		/* Determine what to show */
		var message = '';
		if (data && (data.status == true || data.status == false)) {
			/* We have a legit response */
			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>There were no messages returned from the server.</p>';
			}
		} else {
			/* The response is not recognized */
			message += '<h4>Oops</h4>';
			message += '<p>The response from the server was not recognized.</p>';
		}
		
		/* Append a button to the notice */
		message += '<button type="button" id="notice-button">Close</button>';
		
		/* Show the text */
		bookingForm.notice.set(message);
		
		/* Attach a handler to the close button */
		$('button#notice-button').click(bookingForm.onCloseClick);
	},
	/**
	 * Handler for form submissions
	 */
	onSubmit : function () {
		/* Get the form fields */
		var data = {
			'fld_day' : bookingForm.getFieldValue('fld_day'),
			'fld_month' : bookingForm.getFieldValue('fld_month'),
			'fld_year' : bookingForm.getFieldValue('fld_year'),
			'fld_time' : bookingForm.getFieldValue('fld_time'),
			'fld_location' : bookingForm.getFieldValue('fld_location'),
			'fld_contact' : bookingForm.getFieldValue('fld_contact'),
			'fld_venue' : bookingForm.getFieldValue('fld_venue'),
			'fld_email' : bookingForm.getFieldValue('fld_email'),
			'fld_phone' : bookingForm.getFieldValue('fld_phone'),
			'fld_seats' : bookingForm.getFieldValue('fld_seats'),
			'fld_description' : bookingForm.getFieldValue('fld_description'),
			'returnType' : 'json'
		}
		
		/* Try to send the data to the server */
		$.post(bookingForm.handler, data, bookingForm.onResponse, 'json');
		
		/* Hide the form and show the notice */
		bookingForm.formContainer.fadeOut(500);
		bookingForm.notice.show({
			text : 'Submitting the form...',
			speed : 500
		});
		
		/* Make sure to return false to break true form submission */
		return false;
	},
	/**
	 * Function to set up the form
	 */
	setup : function () {
		/* Save a reference to the container object */
		bookingForm.formContainer = $('div#booking-form-container');
		bookingForm.container = $('div#booking-form');
		
		/* Create an instance of the notice object */
		bookingForm.notice = new ContactNotice({
			headerImageSrc : '/design/sub-book-us.png',
			headerImageAlt : 'Book Us',
			container : bookingForm.container
		});
		
		/* Form button datepicker */
		$('input[name=hidden_date]').datepicker({
			minDate : new Date(),
			numberOfMonths : 2,
			onSelect : function (dateText, inst) {
				/* Apply the dates to the  */
				var dates = dateText.split('/');
				$('input[name=fld_day]').val(dates[1]);
				$('input[name=fld_month]').val(dates[0]);
				$('input[name=fld_year]').val(dates[2]);
			}
		});
		$('button.select-date').click(bookingForm.onCalendarClick);
		
		/* Hook form submision */
		$('form#booking-form').submit(bookingForm.onSubmit);
		
		/* Set up the fields */
		bookingForm.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 ($('#booking-form').length > 0) {
		bookingForm.setup();
	}
});
