// Javascript that gets loaded if the user isn't logged in

function login_submit_handler() {
	jQuery(".mojo_login form").submit(function(e) {
		e.preventDefault();

		// First thing we do is validate on the client end to ensure there's no honest inputting mistake
		// We aren't paranoid here, just make sure its not an honest error
		if (
				jQuery("#mojo_email").val().indexOf("@") == -1 || // its got an "@"
				jQuery("#mojo_email").val().indexOf(".") == -1 || // its got a "."
				jQuery("#mojo_email").val().length < 5 || // its at least 5 chars
				jQuery("#mojo_password").val() == "" // password isn't blank
			) {
			jQuery('#mojo_login_error').addClass('error').html(Mojo.Lang.email_password_warning);
			return false;
		}

		// What page to submit to?
		var submit_url = jQuery(".mojo_login form").attr('action');

		// @todo - ajax image on submission
		jQuery.ajax({
			type: "POST",
			url: submit_url,
			dataType: "json",
			data: jQuery(".mojo_login form").serialize(),
			success: function( result ){
				if (result.login_status === 'success')
				{
					group_name = result.group_name;
					jQuery(".mojo_activate_login").remove();
					jQuery('#mojo_login_error').removeClass('error').html(result.message);
					jQuery.modal.close();
				}
				else
				{
					jQuery('#mojo_login_error').addClass('error').html(result.message);
				}
			}
		});
	});
}

var mojoLogin = {
	container: null,
	open: function (dialog) {
		dialog.overlay.fadeIn('fast');
		dialog.container.fadeIn('fast');
		dialog.data.fadeIn('fast', function () {
			jQuery("#mojo_email").select();
		});
	},
	close: function ( dialog ) {
		dialog.overlay.fadeOut();
		dialog.container.fadeOut();
		dialog.data.fadeOut(function() {
			// the refresh will have the admin bar as the user is logged in now.
			window.location.reload();
		});
	}
};

jQuery(document).ready(function() {
	jQuery.noConflict();

	// used to figure out which js to load
	var group_id = '';

	// load the CSS for this
	jQuery('head').append('<link type="text/css" rel="stylesheet" href="'+Mojo.URL.css_path+'modal_login" />');

	jQuery("a.mojo_activate_login").click(function(e) {
		e.preventDefault();

		jQuery.modal("<div id=\"mojo-modal-content\" class=\"mojo_login\"><div id=\"mojo-modal-title\"><img src=\"http://jamierumbelow.net/index.php/assets/img/mojomotor_logo.jpg\" width=\"156\" height=\"53\" alt=\"MojoMotor\" /></div><div id=\"mojo-modal-data\"><h2>Login</h2><form action=\"http://jamierumbelow.net/index.php/login/process_ajax\" method=\"post\" accept-charset=\"utf-8\"><p id=\"mojo_login_error\">Please login below. You\'re about to have fun!</p><p class=\"mojo_login_field\"><label for=\"email\">Email:</label> <input type=\"text\" name=\"email\" value=\"Email\" id=\"mojo_email\" /></p><p class=\"mojo_login_field\"><label for=\"password\">Password:</label> <input type=\"password\" name=\"password\" value=\"Password\" id=\"mojo_password\" /></p><p class=\"mojo_submit_holder\"><input type=\"checkbox\" name=\"remember_me\" value=\"yes\" id=\"remember_me\"  /> <label for=\"remember_me\" class=\"mojo_remember_me\">Remember my login info</label><button name=\"login\" type=\"submit\" value=\"login\" id=\"mojo_submit\" class=\"button\" >Login</button></p><p><small><a href=\"http://jamierumbelow.net/index.php/login/forgotten_password\">Forgotten password?</a></small></p></form></div></div>", {
			overlayId: 'mojo-overlay',
			containerId: 'mojo-container',
			opacity:35,
			overlayClose:true,
			onOpen:mojoLogin.open,
			minHeight: 330,
			onClose:mojoLogin.close
		});

		// Focus events
		jQuery(".mojo_login_field input").focus(function() {
			if (jQuery(this).val() == Mojo.Lang.email)
			{
				jQuery(this).val('');
			}
		});

		login_submit_handler();
	});

});