/**
*	User widget.
*	
*	@param	string	id							The widget id.
*	@param	string	recoverPasswordHandlerURL	The url to submit the email when the recover password submit button is clicked.
*/
var UserWidget = function(id, recoverPasswordHandlerURL)
{
	var mailRegexp = /^[-a-z0-9_.]+@[-a-z0-9_.]+\.[a-z]{2,5}$/i
	var wDiv = jQuery("#"+id);
	
	/**
	*	Open the login area.
	*/
	this.openLogin = function(event, callback)
	{	
		if(!callback) 
		{
			callback = function() {;};
		}
		
		wDiv.find(".open-login").slideUp("fast").blur().end().
			find(".login-form").slideDown("fast", callback).
			find(".username").focus();
	}
	var openLogin = this.openLogin;
	
	/**
	*	Close the login area.
	*/
	this.closeLogin = function(event, callback)
	{
		if(!callback) 
		{
			callback = function() {;};
		}
		
		wDiv.find(".login-form").slideUp("fast", callback).
			find(".username").val("").end().
			find(".password").val("").end().end().
			find(".open-login").slideDown("fast");
	}
	var closeLogin = this.closeLogin;
	
	/**
	*	Open the recover password area.
	*/
	this.openRecover = function(event, callback)
	{
		if(!callback) 
		{
			callback = function() {;};
		}
		
		closeLogin(null, function()
		{
			wDiv.find(".recover-password-form").slideDown("fast", callback).
				find(".recover-mail").focus();
		});
	}
	var openRecover = this.openRecover;
	
	/**
	*	Close the recover password area.
	*/
	this.closeRecover = function(event, callback)
	{
		if(!callback) 
		{
			callback = function() {;};
		}
		
		wDiv.find(".recover-password-form").slideUp("fast", function() {openLogin(null, callback);}).
			find("input[type='text']").val("");
	}
	var closeRecover = this.closeRecover;
	
	//Initialize everything
	wDiv.find(".open-login").show().
							click(this.openLogin);
							
	wDiv.find(".login-form").hide().
		find(".close-login").show().
							click(this.closeLogin).end().
		find(".open-recover").show().
							click(this.openRecover);
							
	wDiv.find(".recover-password-form").hide();
	wDiv.find(".close-recover").show().
							click(this.closeRecover);
	
	//Attach to the submit event of the recover password form
	wDiv.find(".recover-password-form").submit(function(event)
	{
		event.preventDefault();
		jQuery(this).find(".recover-password-message").html("").end().	//Delete the recover password message
					find(".loading").show().end().						//Show the loading animation
					find(".close-recover").attr("disabled", true).end().	//Disable the Cancel buton
					find(".reset-password").attr("disabled", true);		//Disable the Reset password button
					
		jQuery.post(recoverPasswordHandlerURL, jQuery(this).serialize(), function(data, status)
		{
			wDiv.find(".recover-password-message").html(data);			//Set the recover password message
			wDiv.find(".recover-password-form .loading").hide().end().	//Hide the loading animation
				find(".close-recover").attr("disabled", false).end().	//Enable the Cancel button
				find(".reset-password").attr("disabled", false).end().	//Enable the Reset password button
				find("input[type='text']").val("");						//Delete the content of the input box
		});
	});
}