$(document).ready(function () {
	/* Contact Form */
	$('#contact').validate({
		submitHandler:function(form) {
			$('#contact input[type=submit]').after(loading_img);
			$('#contact input[type=submit]').attr('disabled','disabled');
			$.ajax({
				type: 'post',
				url: $(form).attr('action'),
				data: $(form).serialize(),
				success: function (data) {
					if (data.message == 'success') {
						$('#contact input[type=text]').val('');
						$('#contact textarea').val('');
						captcha();
						$('.success').remove();
						$(form).before($('<p class="success"/>').text(data.result));
						$('.success').hide().fadeIn("slow");
						//
					} else if (data.problem == 'captcha') {
						captcha();
						$('.captcha').append($('<label for="flag_input" generated="true" class="error">You must select the correct flag below.</label>'));
					} else {
						alert(data.message);
					}
					$('#contact input[type=submit]').removeAttr('disabled');
					$(loading_img).remove();
				},
				dataType: "json"
			});
			return false;
		},
		messages: {
			name: {
				required: 'Please use your real name.' },
			email: {
				required: 'We need an e-mail address to contact you.',
				email: 'That e-mail does not appear to be valid.',
				remote: 'That e-mail address has been registered already.' },
			subject: {
				required: 'Summarize your message for us' },
			message: {
				required: 'Tell us what is on your mind' },
			flag_input: {
				required: 'You must select the correct flag below.' }
		},
		rules: {
			email: {
				required: true,
				email: true
			},
			flag_input: {
				required: true
			}
		}
	});
	/* Log-in Form */
	$('#login').validate({
		submitHandler:function(form) {
			form.submit();
		},
		messages: {
			username: {
				required: 'Required',
				email: 'Valid e-mail required.' },
			password: {
				required: 'Required' }
		},
		rules: {
			username: {
				required: true,
				email: true },
			password: {
				required: true }
		}
	});
	/* Lost Password Form */
	$('#reset_password').validate({
		submitHandler:function(form) {
			$('#reset_password input[type=submit]').attr('disabled','disabled');
			$('#reset_password input[type=submit]').after(loading_img);
			$.ajax({
				type: 'post',
				url: $(form).attr('action'),
				data: $(form).serialize(),
				success: function (data) {
					if (data.message == 'success') {
						$('.success').remove();
						$(form).before($('<p class="success"/>').text(data.result));
						$('.success').hide().fadeIn("slow");
						$('input[type=text]').val('');
						$('#reset_password input[type=submit]').removeAttr('disabled');
					} else {
						alert(data.message);
					}
					$(loading_img).remove();
				},
				dataType: "json"
			});
			return false;
		},
		messages: {
			email: {
				required: 'Valid e-mail required.',
				email: 'Valid e-mail required.',
				remote: 'That e-mail address has not been registered.' }
		},
		rules: {
			email: {
				required: true,
				email: true,
				remote: {
					url: "/user/email_registered",
					type: "post",
					data: {
						email: function() {
							return $("#email").val();
						}
					}
				}
			}
		}
	});
	/* Lost Password Form */
	$('#reset').validate({
		submitHandler:function(form) {
			$('#reset input[type=submit]').after(loading_img);
			$('#reset input[type=submit]').attr('disabled','disabled');
			$.ajax({
				type: 'post',
				url: $(form).attr('action'),
				data: $(form).serialize(),
				success: function (data) {
					if (data.message == 'success') {
						$('.success').remove();
						$(form).before($('<p class="success"/>').html(data.result));
						$('.success').hide().fadeIn("slow");
						$('input[type=text]').val('');
						$('input[type=password]').val('');
						$('#reset input[type=submit]').removeAttr('disabled');
					} else if (data.message == 'error') {
						window.location = '/login/reset_form?hash=2aa946b39f02913dc284d2fa05036645';
					}
					$(loading_img).remove();
				},
				dataType: "json"
			});
			return false;
		},
		messages: {
			email: {
				required: 'We need a valid e-mail address for you.',
				email: 'We need a valid e-mail address for you.',
				remote: 'That e-mail address has not been registered.' },
			password: {
				required: 'Required',
				minlength: 'Your password must be at least 8 characters.' },
			password_confirm: {
				required: 'Required',
				equalTo: 'The passwords do not match.' }
		},
		rules: {
			email: {
				required: true,
				email: true,
				remote: {
					url: "/user/email_registered",
					type: "post",
					data: {
						email: function() {
							return $("#email").val();
						}
					}
				}
			},
			password: {
				required: true,
				minlength: 7
			},
			password_confirm: {
				equalTo: "#password"
			}
		}
	});
	/* Registration Form */
	$('#register').validate({
		submitHandler:function(form) {
			$('#register input[type=submit]').after(loading_img);
			$('#register input[type=submit]').attr('disabled','disabled');
			$.ajax({
				type: 'post',
				url: $(form).attr('action'),
				data: $(form).serialize(),
				success: function (data) {
					if (data.message == 'success') {
						window.location = '/profile/edit';
					} else if (data.problem == 'captcha') {
						captcha();
						$('.captcha').append($('<label for="flag_input" generated="true" class="error">You must select the correct flag below.</label>'));
						$('#register input[type=submit]').removeAttr('disabled');
					} else {
						alert(data.message);
					}
					$(loading_img).remove();
				},
				dataType: "json"
			});
			return false;
		},
		messages: {
			name: {
				required: 'So, what is your name?' },
			email: {
				required: 'We need a valid e-mail address for you.',
				email: 'We need a valid e-mail address for you.',
				remote: 'That e-mail address has been registered already.' },
			password: {
				required: 'Required',
				minlength: 'Your password must be at least 8 characters.' },
			confirm_password: {
				equalTo: 'You probably misspelled your password' },
			flag_input: {
				required: 'You must select the correct flag below.' }
		},
		rules: {
			name: {
				required: true
			},
			email: {
				required: true,
				email: true,
				remote: {
					url: "/user/unique_email",
					type: "post",
					data: {
						email: function() {
							return $("#email").val();
						}
					}
				}
			},
			password: {
				required: true,
				minlength: 8
			},
			confirm_password: {
				equalTo: "#password"
			},
			flag_input: {
				required: true
			}
		}
	});
	/* CAPTCHA */
	var captcha = function () {
		$('label[for=captcha]').remove();
		$('.captcha').empty();
		$('.captcha').text('').before('<label for="captcha">To make sure you\'re human, please select the flag of <a href="" target="_blank" class="flag_link"></a>:</label>');
		$('.captcha').prepend('<input type="hidden" name="flag_input" id="flag_input" />');
				  if ($('.captcha').length) {
		$.ajax({
			type: 'post',
			url: '/captcha',
			success: function (data) {
				if (data.success) {
					//window.location='/admin';
					$('.flag_link').text(data.flag.country).attr('href','http://en.wikipedia.org/wiki/Flag_of_'+data.flag.country);
					for (flag in data.flags) {
						$('.captcha').append( $('<span class="flag_option"/>').bind('click',function() {
							$('.flag_option').removeClass('selected');
							$(this).addClass('selected');
							$('#flag_input').attr('value',$('.selected img').attr('alt') );
						}).append( $('<img />').attr('src','http://www.onlinelanguagehelp.com/media/images/flags/'+data.flags[flag].translation.toLowerCase()+'.png').attr('alt',data.flags[flag].translation.toLowerCase()) ) );
					};
				} else {
					alert(data.message);
				}
			},
			dataType: "json"
		});
				  }
	}
	captcha();
});
