function auth_ajax( a ) {	
	$.ajax({
		type: a.type,
		dataType: a.dataType,
		url: a.url,
		data: a.data,
		success: function(data, textStatus){
			if( data.auth_status == "login_required" ) {
				window.auth_ajax_param = a;
				$('.login-dialog').dialog('open').show();
			}
			else if( data.auth_status == "ok" ) {
				a.success(data, textStatus);				
			}
			else {
				alert("Invalid auth_status is returned... = "+data.auth_status);
			}
		},
		error: function(XMLHttpRequest, textError, exception){
			a.error(XMLHttpRequest, textError, exception);
		},
		complete:	function(){
			a.complete();
		}
	});	
}

function login_request() {	
	
	wait_message = "logging in...";	
	
	var formData = $("#login-form").serialize();
	
	$.ajax({
		type: 'post',
		dataType: 'html',
		url: '/ajaxlogin/',
		data: formData,
		success: function(data, textStatus){
			if(data == "done") {					
				$('.login-dialog').dialog('close');
				// Exchange the login logout link
				$('#navtop-login').hide();
				$('#navtop-logout').show();				
				if (window.auth_ajax_param) {						
					a = window.auth_ajax_param;
					window.auth_ajax_param == null;
					auth_ajax(a);
				}
			}
			else {
//				$('#login-dialog-content').empty();
				$('#login-dialog-content').html(data);
			}
		},
		error: function(XMLHttpRequest, textError, exception){
			alert('BookSmith is erring. Please try again.');
			$('#error-msg').html('BookSmith is erring. Please try again.');
		},
		complete:	function(){
			$('.wait-dialog').dialog('close');
		}
	});		
	return false;
}

function auth_GET( GET_request ) {
	
	wait_message = "Authenticating... Please wait.";
	$('.wait-dialog').dialog('open').show();
	
	auth_ajax({
		type: 'post',
		dataType: 'json',
		url: '/check_auth',
		data: null,
		success: function(data, textStatus){
			//alert('GET_request : '+GET_request);			
			window.location.href = GET_request;
		},
		error: function(XMLHttpRequest, textError, exception){
			alert(textError);			
		},
		complete:	function(){
			$('.wait-dialog').dialog('close');
		}
	});
}

function login() {	
	
	wait_message = "Authenticating... Please wait.";
	$('.wait-dialog').dialog('open').show();
	
	auth_ajax({
		type: 'post',
		dataType: 'json',
		url: '/check_auth',
		data: null,
		success: function(data, textStatus){
			//alert("Successfully logged in.");
		},
		error: function(XMLHttpRequest, textError, exception){
			alert(textError);			
		},
		complete:	function(){
			$('.wait-dialog').dialog('close');
		}
	});
}

function setupLoginDialog(){
	
	window.loginPanelLoaded = false;
	
	var content = '<div id="login-dialog-content">';
	content += 'Getting login page';
	content += '<div id="spinner-div"><img src="/site_media/images/spinner.gif"></div>';
	content += '</div>';
	
	$(content).addClass('login-dialog').dialog({
		modal: true,
		autoOpen: false,
		width: 500,
		resizable: false,
		dialogClass: 'login-dialog-container',
		title: 'Login',
		overlay: { 'background-color': 'gray', 'opacity': 0.5 },
		open: function(Event,ui){			
			if ( window.loginPanelLoaded == false) {
				$('#login-dialog-content').load(
					'/get_logindialog',
					function(){
						window.loginPanelLoaded = true;						
					}
				)
			}
			// Clear any earlier set error messages
			$('#login-error-msg').html('');
		},
		close: function(Event, ui){}	
	}).siblings('.ui-dialog-titlebar').css('font-size','60%');
}


