(function ($) {
	
	jQuery.fn.mailDialog = function (options) {
	
		var defaults, eL, mailDialog, opts;

		defaults = {
		
			"subject" : false,
			
			"url" : false
			
		};
		
		opts = jQuery.extend(defaults, options);
		
		eL = jQuery(this);
		
		mailDialog = '';

		eL.click(function () {

			if ('' === mailDialog) {

				opts.subject_line = (false !== opts.subject) ? ' value="' + opts.subject + '" readonly="readonly"' : '';
				
				opts.url_input = (false !== opts.url) ? '<input name="url" value="' + opts.url + '" type="hidden" />' : '';
				
				mailDialog = jQuery('<form id="email-form" action="#" method="post"><label for="name">Name&nbsp;</label><input class="text ui-widget-content ui-corner-all" id="name" name="full_name" type="text" /><label>Email Address&nbsp;</label><input class="text ui-widget-content ui-corner-all" id="email_address" name="email_address" type="text" /><label for="subject">Subject&nbsp;</label><input class="text ui-widget-content ui-corner-all" id="subject" name="subject" type="text"' + opts.subject_line + ' /><label for="subject">Message&nbsp;</label><textarea class="textarea ui-widget-content ui-corner-all" cols="118" name="message" rows="10"></textarea></p><p>&nbsp;</p><div id="recaptcha"></div><p>&nbsp;' + opts.url_input + '</p><p><input class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="submit" value="Send" /></p></form>').submit(function () {

					if (0 === jQuery('#status-message').length) {

						jQuery('#email-form input:submit').before('<div id="status-message"><p>We\'re working on sending your email right now!</p></div>')
						jQuery('#status-message').css({
							'border' : '1px solid #ccc',
							'margin' : '9px 0 9px 0',
							'padding' : '9px'
						});

					};

					jQuery.post(

						'/',

						jQuery(this).serialize(),

						function (data) {

							if ('false' === data[0] || false === data[0]) {

								Recaptcha.reload();

								jQuery("#status-message").html('<p>Sorry, I had trouble processing your submission. Please try again.</p>');

							} else if (true === data[0]) {

								jQuery('#email-form input:submit').remove();

								jQuery('#email-form input:text').attr('readonly', 'readonly').css({

									'background-color' : '#ddd'

								});

								jQuery('#email-form textarea').attr('readonly', 'readonly').css({

									'background-color' : '#ddd'

								});

								jQuery('#recaptcha').slideUp('slow', function () {

									Recaptcha.destroy();

								});

								jQuery("#status-message").html('<p>Thanks for your message! We\'ll be back in touch soon.</p>');

							}

						},

						'json'

					);

					return false;

				});

				jQuery(mailDialog).css({

					'text-align': 'left',
					'font-size': '12px'

				});

				jQuery(mailDialog).children("p").css({

					'margin' : '0 0 7px 0'

				});

				jQuery(mailDialog).dialog({

					autoOpen : false,
					title : "Thank you for getting in touch with us via our online form.",
					modal : true,
					width: 850

				});

				Recaptcha.create("6LffebsSAAAAAMVjN-4dWWM7JX1WA2oCdFzqnVgD",
				"recaptcha", {
				   theme: "clean",
				   callback: Recaptcha.focus_response_field
				});

				mailDialog.dialog('open');
				
				return false;

			} else {

				mailDialog.dialog('open');
				
				return false;

			}

			return false;

		});
		
	};
	
}(jQuery));
