
var userLogin = {
	
	initialised: false,
	
	dialog: null,
	
	loggedIn: false,
	
	tryLogin: function() {
		
		var email = $("#login-email").val();
		var password = $("#login-password").val();
		var _this = this;
		var getAddress = "";
		
		if ( order && order.initialised ) {
			getAddress = "&getAddress=1";
		}
		
		$.ajax({
			type: "POST",
			url: '/ajax/login/',
			data: "email=" + email + "&password=" + password + getAddress,
			
			// These two need to be functions so the response is deferred and not executed immediately
			success: _this.loginReturn,
			
			error: _this.loginError
		});
	},
	
	loginReturn: function(data, textStatus, XMLHttpRequest) {
		
		var dataArr = jQuery.parseJSON(data);
		debug.log("Response from server: ", textStatus);
		debug.log("Data: ", data);
		
		if (data.returnData.status && data.returnData.status == "success")
		{
			debug.log("logged in!");
			$("#login-message").attr("innerHTML", "");
			//userLogin.dialog.dialog('close');
			userLogin.loggedIn = true;
			
			window.location.href = "/order/";
			
			/*if ( order && order.initialised && data.returnData.address )
			{
				order.populateForm( data.returnData.address );
			}*/
			
			// Hide the password area if they are logged in
			/*if (order && order.initialised )
			{
				order.showHidePassword();
			}
			else
			{
				$(".password-section").hide();
			}*/
			
			// Change the login link to logout
			//debug.log($(".customer-login a").text());
			//$(".customer-login a").text( "Logout" );
		}
		else
		{
			userLogin.showFailedLogin(data.returnData.message);
			userLogin.loggedIn = false;
		}
	},
	
	loginError: function(XMLHttpRequest, textStatus, errorThrown) {
		
		debug.log("Request failed: " + textStatus + " - " + errorThrown);
		$("#login-message").attr("innerHTML", "Oops! There was an error. Please try again shortly.");
	},
	
	tryLogout: function() {
		
		var _this = this;
		
		$.ajax({
			type: "POST",
			url: '/ajax/logout/',
			
			// These two need to be functions so the response is deferred and not executed immediately
			success: _this.logoutReturn,
			
			error: _this.logoutError
		});
	},
	
	logoutReturn: function( data, textStatus, XMLHttpRequest ) {
		
		var dataArr = jQuery.parseJSON( data );
		debug.log("Response from server: ", textStatus);
		debug.log("Data:", parseInt(data.returnData.loggedIn));
		
		if ( parseInt( data.returnData.loggedIn ) == 0 )
		{
			debug.log("logged out!");
			userLogin.loggedIn = false;
			
			// Only initialised if we're on the order page and the form exists
			if ( order && order.initialised ) {
				
				order.blankForm();
			}
			else
			{
				window.location.href = "/register/";
			}
			
			// Show the password area if they are logged out
			/*if (order && order.initialised )
			{
				order.showHidePassword();
			}
			else
			{
				$(".password-section").show();
			}*/
			
			// Change the login link to logout
			//$(".customer-login a").text( "Customer Login" );
			
			// Blank the login form
			$("#login-email").val("");
			$("#login-password").val("");
		}
		else
		{
			userLogin.showFailedLogout();
			userLogin.loggedIn = true;
		}
	},
	
	logoutError: function(XMLHttpRequest, textStatus, errorThrown) {
		
		debug.log("Request failed: " + textStatus + " - " + errorThrown);
		order.showFailedLogin();
	},
	
	showFailedLogin: function(text) {
		var msg = "Oops! We couldn't log you in.";
		if(text)
			msg = msg+" "+text;
		$("#login-message").text(msg);
	},
	
	showFailedLogout: function() {
		
		alert("Oops! We couldn't log you out. Please try again.");
	},
	
	openDialog: function() {
		
		//this.dialog.dialog('open');
		
		// No more dialog - instead redirect to the register page
		window.location.href = "/register/";
	},
	
	connectEvents: function() {
		
		var _this = this;
		
		$(".login-link").click(function(e) {
			
			e.preventDefault();
			
			if ( $(this).attr( "innerHTML" ) == "Logout" )
			{
				_this.tryLogout();
			}
			else
			{
				_this.openDialog();
			}
		});
		
		$(".logout-link").click(function(e) {
			
			e.preventDefault();
			_this.tryLogout();
		});
		
		$("#login-form").keydown( function( e ) {
			
            // Enter pressed?
            if (e.which == 10 || e.which == 13) {
    			e.preventDefault();
				_this.tryLogin();
            }
        });
		
		$("#login-submit").click(function(e) {
			
			e.preventDefault();
			_this.tryLogin();
		});
	},
	
	init: function() {
		
		/*this.dialog = $("#login-dialog");
		this.dialog.dialog({
			autoOpen: false, 
			title: "Customer Login",
			resizable: false,
			modal: true,
			dialogClass: "login-dialog",
			draggable: false
		});*/
		
		this.connectEvents();
		
		this.initialised = true;
	}
};


