//= require <mootools-core-1.3-full-nocompat>
//= require <puremvc-mootools-port>
//= require <findsalons/ApplicationFacade>
//= require <findsalons/AsyncProxy>
var TopRightColumnMediator = function()
{
	this.Extends = Mediator;
	/**
	 * The TopRightColunm instance
	 * 
	 * @type TopRightColumn
	 */
	this.topRightColumn = null;
	/**
	 * Reference to the AsyncProxy instance registered to the Facade
	 * 
	 * @type AsyncProxy
	 */
	this.asyncProxy = null;

	this.initialize = function(viewComponent /* TopRightColumn */)
	{
		this.parent(TopRightColumnMediator.NAME, viewComponent);
		this.topRightColumn = this.viewComponent;
	};

	this.listNotificationInterests = function()
	{
		return [ AsyncProxy.COOKIE_CHECK_RESULT, AsyncProxy.AUTH_USER_RESULT, AsyncProxy.LOGOUT_RESULT, AsyncProxy.ASYNC_SENT ];
	};

	this.onRegister = function()
	{
		// Check if the user has been authenticated through a cookie
		this.asyncProxy = this.facade.retrieveProxy(AsyncProxy.NAME);
		this.setStateByUserResult(this.asyncProxy.data.userInfo);
		// --------------------------------------------------------
		this.topRightColumn.addEventListener("login", this.loginLogoutSubmitHandler, false, 0, this);
		this.topRightColumn.addEventListener("logout", this.loginLogoutSubmitHandler, false, 0, this);
		this.topRightColumn.addEventListener("createAccount", this.createAccountHandler, false, 0, this);
	};

	this.onRemove = function()
	{
		this.topRightColumn.removeEventListener("login", this.loginLogoutSubmitHandler, false);
		this.topRightColumn.removeEventListener("logout", this.loginLogoutSubmitHandler, false);
		this.topRightColumn.removeEventListener("createAccount", this.createAccountHandler, false);
	};

	this.handleNotification = function(note /* Notification */)
	{
		switch (note.getName())
		{
			case AsyncProxy.LOGOUT_RESULT:
			case AsyncProxy.AUTH_USER_RESULT:
			case AsyncProxy.COOKIE_CHECK_RESULT:
				this.setStateByUserResult(note.getBody(), note.getName());
				break;

			case AsyncProxy.ASYNC_SENT:
				this.requestSentHandler(note);
				break;
		}
	};

	// -----------------------------------------------------------------------
	// Event Handlers
	this.loginLogoutSubmitHandler = function(event)
	{
		switch (event.type)
		{
			case "login":
				this.asyncProxy.authenticateUser(event.data);
				break;

			case "logout":
				this.asyncProxy.logoutUser();
				break;
		}
	};

	this.createAccountHandler = function()
	{
		this.sendNotification(ApplicationFacade.SET_HASH, "action=getListed");
	};

	// -----------------------------------------------------------------------
	// notification handler functions
	this.requestSentHandler = function(note /* Notification */)
	{
		var type = note.getBody();
		switch (type)
		{
			case "authUser":
				this.topRightColumn.setStatusMessage("Logging in ...");
				break;

			case "logout":
				this.topRightColumn.setStatusMessage("Logging out ...");
				break;
		}
	};

	this.setStateByUserResult = function(userInfo /* Object */, noteName /* string */)
	{
		this.topRightColumn.userInfo = userInfo;
		var message = "";
		if (userInfo && userInfo.validated)
		{
			if (noteName != AsyncProxy.COOKIE_CHECK_RESULT)
				this.topRightColumn.state('accountStatus');
			else if (userInfo.rememberMe)
				this.topRightColumn.state('accountStatus');
		}
		else
		{
			this.topRightColumn.state('');
			if (noteName == AsyncProxy.AUTH_USER_RESULT)
				message = "Bad username or password.";
		}
		this.topRightColumn.setStatusMessage(message);
	};
};
TopRightColumnMediator = new Class(new TopRightColumnMediator());
TopRightColumnMediator.NAME = 'TopRightColumnMediator';
