/**
 * @author kchevalier@suntouchdesign.com
 * march 2009
 */

$(document).ready(function(){
	removeValues();
	Login.init();
});


function removeValues() {
	$('#loginForm input.submit').val( '' );
	$('#forgotForm input.submit').val( '' );
} 

var Login = {
	
	// constructor method
	init: function() {
		
		loginURL = '/data-bin/code/login_AJAX.php';
		resetURL = '/data-bin/code/reset_AJAX.php';
		form = $('form.logForm');
		username = $('#username');
		password = $('#password');
		messageContainer = $('#message');
		forgotLink = $('p.forgot a');
		forgotFrm = $('#forgotForm');
		resetForm = $('form.resetForm');
		forgotMessage = $('p.forgotMessage');
		usernameR = $('#usernameR');
		messageOpen = false;
		forgotOpen = false;
		attempts = 0;
		
		// remove default submit event and add new AJAX login
		form.bind('submit', function(e){
			e.preventDefault();
			Login.authenticate(e);
		});
		
		// clear message
		username.bind( 'focus', function(e){
			Login.clearMessage(e);
		} );
		password.bind( 'focus', function(e){
			Login.clearMessage(e);
		} );
		
		// forgot link
		forgotLink.bind( 'click', function(e){
			e.preventDefault();
			Login.showForgot(e);
		} );
		
		resetForm.bind( 'submit', function(e) {
			e.preventDefault();
			Login.resetPass(e);
		} );
		
	},
	
	// check login via AJAX
	authenticate: function(e) {
		
		attempts++;
		
		// create md5 hash for password
		passwordHash = hex_md5( password.val() );
		
		$.ajax({
			type: 'POST',
			url: loginURL,
			dataType: 'xml',
			data: { username: username.val(), password: passwordHash },
			success: function(data){
				
				var pass = $(data).find("pass").text();
				var messageText = $(data).find("message").text();
				
				// having some fun with the message text
				if ( attempts > 3 ) {
					messageText = "Need some help?";
				}
				if ( attempts > 6 ) {
					messageText = "Are you in the right place?";
				}
				if ( attempts > 9 ) {
					messageText = "Everything OK?";
				}
				if ( attempts > 12 ) {
					messageText = "Knock, knock.";
				}
				if ( attempts > 13 ) {
					messageText = "Who's there?";
				}
				if ( attempts > 14 ) {
					messageText = "No, really, who's there?";
				}
				if ( attempts > 15 ) {
					messageText = "Because this is ridiculous.";
				}
				if ( attempts > 16 ) {
					messageText = "Please contact the site admin.";
				}
				if ( attempts > 20 )  {
					messageText = "Go away, hacker.";
				}
					
				// if user is authenticated
				if ( pass == 1 ) {
					
					location.reload( true );
					return true;
				}
					
				// invalid login
				else {
					messageContainer.html( messageText );
					messageContainer.animate( { marginTop: '-20px' }, 1000, 'linear', function() {
						username.val( '' );
						password.val( '' );
					} );
					messageOpen = true;
					return false;
				}
					
			},
			
			// AJAX error
			error: function( XMLHttpRequest, textStatus, errorThrown ) {
				messageContainer.html( 'Error. Please try again.' );
				messageContainer.animate( { marginTop: '-20px' }, 1000 );
				messageOpen = true;
				return false;
			}
		});
		
	},
	
	// clear login message
	clearMessage: function(e) {
		if ( messageOpen ) {
			messageContainer.animate( { marginTop: '-71px' }, 1000, 'linear', function() {
				$(this).html('');
				messageOpen = false;
			} );
		}
	},
	
	// show the forgot password form
	showForgot: function(e) {
		if ( !forgotOpen ) {
			forgotMessage.html( 'Type your <em>Username</em> in the field below and we will send a new password to the email address on file for your account.' );
			forgotFrm.animate( { left: '320px' }, 1000, 'linear', function () {
				forgotOpen = true;
			} );
		}
		else {
			forgotFrm.animate( { left: '34px' }, 1000, 'linear', function () {
				forgotOpen = false;
			} );
		}
	},
	
	// reset user password and send and email
	resetPass: function(e) {
		
		forgotMessage.fadeOut( 500 );
		
		$.ajax({
			type: 'POST',
			url: resetURL,
			dataType: 'xml',
			data: { usernameR: usernameR.val() },
			success: function(data){
				
				var passF = $(data).find("pass").text();
				var messageTextF = $(data).find("message").text();
					
				forgotMessage.fadeOut( 500, function() {
					forgotMessage.html( messageTextF ).fadeIn( 500 );
				} );
				return false;
			},
			
			// AJAX error
			error: function( XMLHttpRequest, textStatus, errorThrown ) {
				alert( textStatus + ": " + errorThrown );
				forgotMessage.html( 'Error. Please try again.' );
				return false;
			}
		});
	}
};

