(function (win){
	var comment_poster = function (index, node){
		var $form = $(node),
		form_action = $form.attr('action'),
		form_related_guid = $form.find('input[name="related_guid"]').val(),
		$form_author = $form.find('input[name="author"]'),
		$form_text = $form.find('textarea[name="text"]'),
		$form_submit = $form.find('input[type="submit"]'),
		$form_author_error = $form.find('label[for="author"] .error'),
		$form_text_error = $form.find('label[for="text"] .error'),
		$form_captcha_error = $form.find('.cap .error'),
		$form_submit_error = $form.find('.submit-box .error'),
		clear_form = function (){
			$form_author.val('');
			$form_text.val('');
		},
		clear_errors = function (){
			$form_author_error.html('');
			$form_text_error.html('');
			$form_captcha_error.html('');
			$form_submit_error.html('');
		};

		$form.submit(function (){
			var author = $.trim($form_author.val()),
			text = $.trim($form_text.val()),
			// We need to re-'find' these nodes each time because we'll reload the
			// captcha on failure. Otherwise we'll have a stale reference.
			recaptcha_challenge = $form.find('input[name="recaptcha_challenge_field"]').val(),
			recaptcha_response = $form.find('input[name="recaptcha_response_field"]').val();

			clear_errors();

			if (author === ''){
				// Author input error.
				$form_author_error.html('What\'s your name?');
			}

			if (text === ''){
				// Text input error.
				$form_text_error.html('What do you want to say?');
			}

			if (author !== '' && text !== ''){
				$form_submit.val('Please wait...');
				$form_submit.attr('disabled', 'true');
				$.post(
					form_action,
					{
						related_guid:form_related_guid,
						author:author,
						text:text,
						recaptcha_challenge_field:recaptcha_challenge,
						recaptcha_response_field:recaptcha_response
					},
					function (data){
						$form_submit.val('Submit.');
						$form_submit.removeAttr('disabled');
						switch (data.status){
							// Failed captcha.
							case 'not-human':
							Recaptcha.reload()
							$form_captcha_error.html('You failed to pass the test for humanitiy.');
							break;

							// Tried to use 'Mason'.
							case 'author-error':
							Recaptcha.reload()
							$form_author_error.html('My name is Mason. Your name can\'t be Mason.');
							break;

							// Generic failure.
							case 'fail':
							Recaptcha.reload()
							$form_submit_error.html('There was an unpredictable error. Try again, now or later, or tell Mason about it.');
							break;

							// Pass!
							case 'success':
							clear_form();
							$form.html('Your comment has been submitted, but it\'s subject to moderation. I\'m a little worried about spam even with the CAPTCHA because I\'ve seen them compromised a few times recently.');
							break;
						}
					}
				);
			} else {
				Recaptcha.reload()
			}

			return false;
		});
	},
	docready = function (){
		$('.comment-posting form').each(comment_poster);
	};

	win.docready = win.docready || docready;
})(window);

