$(document).ready(function(){
	$("#nav_home").click(function(){
		$("#content_box").html("<div class=\"loading\">Loading</div>");
		$("#content_box").load("_content/main.html");
	});
	$("#nav_ban").click(function(){
		$("#content_box").html("<div class=\"loading\">Loading</div>");
		$("#content_box").load("_content/banned.html");
	});
	$("#nav_join").click(function(){
		HasCaptcha = false;
		$("#content_box").html("<div class=\"loading\">Loading</div>");
		$.getScript('_scripts/jquery.form.js');
		$("#content_box").load("_content/apply.php", function(){
			
			$('#submit_button').hide();
			$('#englishtest').bind( 'blur', addCaptcha );
			
			$('#submit_button').bind( 'click', function() {
				$.ajax({
					type: 'POST',
					url: '_content/apply.php',
					data: $('form#appForm').serialize(),
					dataType: 'json',
					beforeSend: function() {
						$('#output').html('');
						// NEED FORM VALIDATION STUFFS HERE OLOL
						var fname = $('#first_name').val();
						var lname = $('#last_name').val();
						var resp_field = $('#recaptcha_response_field').val();
						/*if(typeof resp_field == "undefined") {
							$('#output').hmtl('Captcha is required');
							return false; }*/
					},
					success: function(data, textStatus){
						if(data.status == "Success") {
							$('#recaptcha').html('');
							$(":input").attr( 'disabled', 'disabled' );
							$("textarea").attr( 'disabled', 'disabled' );
							$('#submit_button').unbind('click');
							alert(data.why);
						} else if(data.status == "Captcha Error") {
							//$('#output').html('<h2>'+data.why+'</h2>');
							Recaptcha.reload();
							//$('#recaptcha_response_field').val('')
							//$('#recaptcha_response_field').focus();
							alert(data.why);
						} else {
							alert(data.why)
							$(data.where).one( 'focus', function(event) {
								$(this).one( 'blur', Recaptcha.reload() );
							});
							$(data.where).focus();
						}
					},
					error: function(XMLHttpRequest, textStatus){
						//alert('Err: '+textStatus);
						$('#output').html('<h2>'+textStatus+'</h2>');
					}
				});
				return false;
			});
		});
	});
	
	//$("#content_box").load("_content/main.html");
});

$HasCaptcha = false;

function addCaptcha() {
	if(HasCaptcha == false ) {
		var RecaptchaOptions = {
			theme : 'black',
			callback: Recaptcha.focus_response_field
		};
		Recaptcha.create('6Lfj5rkSAAAAADE8ACyqz5zpRgzk6Nuor2Um73FF', 'recaptcha', RecaptchaOptions );
		$('#submit_button').show();
		HasCaptcha = true;
	}
}

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18565279-1']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
