jQuery(document).ready( function($) {
	//Oh well... I guess we have to use jQuery ... if you are a javascript developer, consider MooTools if you have a choice, it's great!
	//I'm biased, but form functionality already comes prepacked with MooTools :) 	
 	$('#LoginWithAjax_Login').submit(function(event){
		//Stop event, add loading pic...
		event.preventDefault();
		$('#LoginWithAjax_LoginMsg').empty().attr('class', 'LoginWithAjax_Msg');
		if($('#lwa_user_login').val() == '')
		{
			$('#LoginWithAjax_LoginMsg').addClass('invalid').html(decodeHtml(lwa_settings.err_invalid_login));
			return false;
		}
		$('#lwa_loading_login').show();
		
		//Sort out url
		url = $('#LoginWithAjax_Login').attr('action');
		url += (url.match(/\?/) != null) ? '&callback=?' : '?callback=?' ;
		url += "&log="+encodeURIComponent($("#lwa_user_login").attr('value'));
		url += "&pwd="+encodeURIComponent($("#lwa_user_pass").attr('value'));
		if($("#lwa_rememberme").is(':checked') ){
			url += "&rememberme="+encodeURIComponent($("#lwa_rememberme").attr('value'));
		}
		url += "&login-with-ajax=login";
		
		$.getJSON( url , function(data, status){
			$('#lwa_loading_login').hide();
			console.log(data);
			if( data.result === true || data.result === false ){
				if(data.result === true){
					//Login Successful
					//if( $('#LoginWithAjax_LoginMsg').length > 0 ){
						$('#LoginWithAjax_LoginMsg').addClass('confirm').html(data.message);
					//}else{
					//	$('<span id="LoginWithAjax_LoginMsg" class="confirm">'+data.message+'</span>').prependTo('#login-with-ajax');
					//}
					if(data.redirect == null){
						window.location.reload();
					}else{
						window.location = data.redirect;
					}
				}else{
					//Login Failed
					//If there already is an error element, replace text contents, otherwise create a new one and insert it
					//if( $('#LoginWithAjax_LoginMsg').length > 0 ){
						if($('#lwa_user_login').val() == '' || $('#lwa_user_pass').val() == '' )
						{
							$('#LoginWithAjax_LoginMsg').addClass('invalid').html(data.error);
						}else{
							$('#LoginWithAjax_LoginMsg').addClass('invalid').html(decodeHtml(lwa_settings.err_invalid_login));
							console.log(decodeHtml(lwa_settings.err_invalid_login));
						}
					//}else{
					//	$('<span id="LoginWithAjax_LoginMsg" class="invalid">'+data.error+'</span>').prependTo('#login-with-ajax');
					//}
					//We assume a link in the status message is for a forgotten password
					//$('#LoginWithAjax_LoginMsg').click(function(event){
					//	event.preventDefault();
					//	$('#LoginWithAjax_Remember').show('slow');
					//});
				}
			}else{	
				//If there already is an error element, replace text contents, otherwise create a new one and insert it
				//if( $('#LoginWithAjax_LoginMsg').length > 0 ){
					$('#LoginWithAjax_LoginMsg').addClass('invalid').html(decodeHtml(lwa_settings.err_default + status));
				//}else{
				//	$('<span id="LoginWithAjax_LoginMsg" class="invalid">An error has occured. Please try again.</span>').prependTo('#login-with-ajax');
				//}
			}
		});
	});	
	
 	$('#LoginWithAjax_Remember').submit(function(event){
		//Stop event, add loading pic...
		event.preventDefault();
		$('#LoginWithAjax_RememberMsg').empty().attr('class', 'LoginWithAjax_Msg');	
		$("#lwa_loading_remember").show();
		//Sort out url
		url = $('#LoginWithAjax_Remember').attr('action');
		url += (url.match(/\?/) != null) ? '&callback=?' : '?callback=?' ;
		url += "&user_login="+$("#lwa_user_remember").attr('value');
		url += "&login-with-ajax=remember";
		$.getJSON( url , function(data, status){
			$('#lwa_loading_remember').hide();
			if( data.result === true || data.result === false ){
				if(data.result == '1'){
					//Successful
					//if( $('#LoginWithAjax_RememberMsg').length > 0 ){
						$('#LoginWithAjax_RememberMsg').addClass('confirm').html(data.message);
					//}else{
					//	$('<span id="LoginWithAjax_RememberMsg" class="confirm">'+data.message+'</span>').prependTo('#login-with-ajax');
					//}
				}else{
					//Failed
					//If there already is an error element, replace text contents, otherwise create a new one and insert it
					//if( $('#LoginWithAjax_RememberMsg').length > 0 ){
						$('#LoginWithAjax_RememberMsg').addClass('invalid').html(data.error);
					//}else{
					//	$('<span id="LoginWithAjax_RememberMsg" class="invalid">'+data.error+'</span>').prependTo('#login-with-ajax');
					//}
				}
			}else{	
				//If there already is an error element, replace text contents, otherwise create a new one and insert it
				//if( $('#LoginWithAjax_RememberMsg').length > 0 ){
					$('#LoginWithAjax_RememberMsg').addClass('invalid').html(decodeHtml(lwa_settings.err_default) + status);
				//}else{
				//	$('<span id="LoginWithAjax_RememberMsg" class="invalid">An error has occured. Please try again.</span>').prependTo('#login-with-ajax');
				//}
			}
		});
	});	
	
	$("#lwa_wp-register").click(function(){
			
		$('#LoginWithAjax_RegisterMsg').empty().attr('class', 'LoginWithAjax_Msg');	
		$("#lwa_loading_register").show();
		
		$.post(lwa_settings.base_url + '/wp-content/plugins/login-with-ajax/login-with-ajax-register.php', {user_login: $("#lwa_register_login").val(), user_email: $("#lwa_register_email").val() }, function(data, status){
			console.log(data);
			$("#lwa_loading_register").hide();
			//console.log(status);
			if(data.result != 1)
			{
				$("#LoginWithAjax_RegisterMsg").addClass('invalid').html(data.msg);
			}else{
				$("#LoginWithAjax_RegisterMsg").addClass('confirm').html(data.msg);
			}
		}, 'json' );
		
	} );
	
	function decodeHtml(encoded)
	{
		return encoded.replace(/&lt;/g, '<').replace(/&gt;/g, '>');
	}
	
});