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_Form').submit(function(event){
		//Stop event, add loading pic...
		event.preventDefault();
		idstr = jQuery(this).attr('id');
		id = idstr.split('-')[1];
		if( $('#LoginWithAjax-' + id).length > 0 ){
			$('<div class="LoginWithAjax_Loading" id="LoginWithAjax_Loading-' + id + '"></div>').prependTo('#LoginWithAjax-' + id);
		}else{
			$('<div class="LoginWithAjax_Loading" id="LoginWithAjax_Loading-' + id + '"></div>').prependTo('#login-with-ajax-' + id);
		}
		//Sort out url
		url = $('#LoginWithAjax_Form-' + id).attr('action');
		url += (url.match(/\?/) != null) ? '&callback=?' : '?callback=?' ;
		url += "&log="+$("#lwa_user_login-" + id).attr('value');
		url += "&pwd="+$("#lwa_user_pass-" + id).attr('value');
		url += "&login-with-ajax=login";
		$.getJSON( url , function(data, status){
			$('#LoginWithAjax_Loading-' + id).remove();
			if( data.result === true || data.result === false ){
				if(data.result == '1'){
					//Login Successful
					if( $('#LoginWithAjax_Status-' + id).length > 0 ){
//						$('#LoginWithAjax_Status-' + id).attr('class','confirm').html("Login Successful, redirecting...");
						$('#LoginWithAjax_Status-' + id).addClass('confirm').html("Login Successful, redirecting...");
					}else{
//						$('<span id="LoginWithAjax_Status-"' + id + ' class="confirm">Login Successful, redirecting...</span>').prependTo('#login-with-ajax-' + id);
						$('<span id="LoginWithAjax_Status-"' + id + ' class="LoginWithAjax_Status confirm">Login Successful, redirecting...</span>').prependTo('#login-with-ajax-' + id);
					}
					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_Status-' + id).length > 0 ){
//						$('#LoginWithAjax_Status-' + id).attr('class','invalid').html(data.error);
						$('#LoginWithAjax_Status-' + id).addClass('invalid').html(data.error);
					}else{
//						$('<span id="LoginWithAjax_Status-"' + id + ' class="invalid">'+data.error+'</span>').prependTo('#login-with-ajax-' + id);
						$('<span id="LoginWithAjax_Status-"' + id + ' class="LoginWithAjax_Status invalid">'+data.error+'</span>').prependTo('#login-with-ajax-' + id);
					}
					//We assume a link in the status message is for a forgotten password
					$('.LoginWithAjax_Status').click(function(event){
						event.preventDefault();
						idstr = jQuery(this).attr('id');
						id = idstr.split('-')[1];
						$('#LoginWithAjax_Remember-' + id).show('slow');
					});
				}
			}else{	
				//If there already is an error element, replace text contents, otherwise create a new one and insert it
				if( $('#LoginWithAjax_Status-' + id).length > 0 ){
//					$('#LoginWithAjax_Status-' + id).attr('class','invalid').html('An error has occured. Please try again.'+status);
					$('#LoginWithAjax_Status-' + id).addClass('invalid').html('An error has occured. Please try again.'+status);
				}else{
//					$('<span id="LoginWithAjax_Status-"' + id + ' class="invalid">An error has occured. Please try again.</span>').prependTo('#login-with-ajax-' + id);
					$('<span id="LoginWithAjax_Status-"' + id + ' class="LoginWithAjax_Status invalid">An error has occured. Please try again.</span>').prependTo('#login-with-ajax-' + id);
				}
			}
		});
	});	
	
 	$('.LoginWithAjax_Remember').submit(function(event){
		//Stop event, add loading pic...
		event.preventDefault();
		idstr = jQuery(this).attr('id');
		id = idstr.split('-')[1];
		$('<div id="LoginWithAjax_Loading-' + id + '"></div>').prependTo('#LoginWithAjax-' + id);
		//Sort out url
		url = $('#LoginWithAjax_Remember-' + id).attr('action');
		url += (url.match(/\?/) != null) ? '&callback=?' : '?callback=?' ;
		url += "&user_login="+$("#lwa_user_remember-" + id).attr('value');
		url += "&login-with-ajax=remember";
		$.getJSON( url , function(data, status){
			$('#LoginWithAjax_Loading-' + id).remove();
			if( data.result === true || data.result === false ){
				if(data.result == '1'){
					//Successful
					if( $('#LoginWithAjax_Status-' + id).length > 0 ){
						$('#LoginWithAjax_Status-' + id).addClass('confirm').html("We have sent you an email");
					}else{
						$('<span id="LoginWithAjax_Status-' + id + '" class="confirm">We have sent you an email</span>').prependTo('#login-with-ajax-' + id);
					}
				}else{
					//Failed
					//If there already is an error element, replace text contents, otherwise create a new one and insert it
					if( $('#LoginWithAjax_Status-' + id).length > 0 ){
						$('#LoginWithAjax_Status-' + id).addClass('invalid').html(data.error);
					}else{
						$('<span id="LoginWithAjax_Status-' + id + '" class="LoginWithAjax_Status invalid">'+data.error+'</span>').prependTo('#login-with-ajax-' + id);
					}
				}
			}else{	
				//If there already is an error element, replace text contents, otherwise create a new one and insert it
				if( $('#LoginWithAjax_Status-' + id).length > 0 ){
					$('#LoginWithAjax_Status-' + id).addClass('invalid').html('An error has occured. Please try again.'+status);
				}else{
					$('<span id="LoginWithAjax_Status-' + id + '" class="LoginWithAjax_Status invalid">An error has occured. Please try again.</span>').prependTo('#login-with-ajax-' + id);
				}
			}
		});
	});		
	$('#LoginWithAjax_Remember').hide();
	$('.LoginWithAjax_Links_Remember').click(function(event){
		event.preventDefault();
		idstr = jQuery(this).attr('id');
		id = idstr.split('-')[1];
		$('#LoginWithAjax_Remember-' + id).show('slow');
	});
	$('.LoginWithAjax_Links_Remember_Cancel').click(function(event){
		event.preventDefault();
		idstr = jQuery(this).attr('id');
		id = idstr.split('-')[1];
		$('#LoginWithAjax_Remember-' + id).hide('slow');
	});

	$('.wp-logout').click(function (event)
	{
		event.preventDefault();

		idstr = $(this).attr('id');
		id = idstr.split('-')[2];

		if ($('#LoginWithAjax-' + id).length > 0)
		{
			$('<div class="LoginWithAjax_Loading" id="LoginWithAjax_Loading-' + id + '"></div>').prependTo('#LoginWithAjax-' + id);
		}
		else
		{
			$('<div class="LoginWithAjax_Loading" id="LoginWithAjax_Loading-' + id + '"></div>').prependTo('#login-with-ajax-' + id);
		}

		var enc_logout_url = $(this).attr('href');
		var logout_url = decodeURIComponent(enc_logout_url);
		var redir_start = logout_url.indexOf('redirect_to');
		var redir_end = logout_url.indexOf('&', redir_start);
		var enc_redirect_url = logout_url.substring(redir_start + 12, redir_end);
		var redirect_url = decodeURIComponent(enc_redirect_url);

		$.ajax(
		{
			url:
				enc_logout_url,

			type:
				'GET',

			complete:
				function (XMLHttpRequest, textStatus)
				{
					$('#LoginWithAjax_Loading-' + id).remove();
					if($('#LoginWithAjax_Status-' + id).length > 0)
					{
						$('#LoginWithAjax_Status-' + id).addClass('confirm').html("Logout Successful, redirecting...");
					}
					else
					{
						$('<span id="LoginWithAjax_Status-' + id + '" class="LoginWithAjax_Status confirm">Logout Successful, redirecting...</span>').prependTo('#login-with-ajax-' + id);
					}
					location.href = redirect_url;
				}
		});
	});
});