//login module
sfd.login = {
		
	init : function()
	{
		this.initForm();
		this.initForgetPassword();
	},
	
	initForm : function()
	{
		$('#identification .btn02 a').click(function()
		{
			$('#identification').submit();
			return false;
		});
		
	},
	
	initForgetPassword : function()
	{
		function hideLoaderAndEmptyForm()
		{
			$('#forgetFormDiv').show();
			$('#forgetFormContent').empty();
			$('#forgetPasswordForm input[name="email"]').val('');
			sfd.ajaxLoader.hide();
		}

		//@see scripts/index/index.phtml
		$("#forgetFormDialog").dialog({
			autoOpen: false,
			modal: true,
			width: 400,
			open : function()
			{
				hideLoaderAndEmptyForm();
			},
			close : function()
			{
				hideLoaderAndEmptyForm();
			}
		});
		
		//@see scripts/index/login.phtml
		$('#forgetPassword').click(function()
		{
			$("#forgetFormDialog").dialog('open');
			return false;
		});

		var extractMessages = sfd.utility.extractMessages;
		$('#forgetPasswordForm .btn02 a').click(function()
		{
			if ($('#forgetPasswordForm input[name="email"]').val() != '')
			{
				$('#forgetFormDialog').bind('ajaxStart', function()
				{
					$(this).append(sfd.ajaxLoader.show());
					$(this).unbind('ajaxStart');
				});
				
				$.post(sfd.baseUrl + '/index/email-reset-password', $('#forgetPasswordForm').serialize(), function(json)
				{
					if (json.success)
					{
						$('#forgetFormDiv').hide();
						$('#forgetFormContent').html("Un email vient d'être envoyé à l'adresse email indiquée. Cliquez sur le lien contenu dans cet email pour définir un nouveau mot de passe.");
						
					}
					else
					{
						var msg = extractMessages(json.message);
						$('#forgetFormContent').html(msg);
					}
					
					sfd.ajaxLoader.hide();
				}
				,
				'json');
			}
			return false;
		});
	}
};




$(function()
{
	sfd.login.init();
});


