//= require <mootools-core-1.3-full-nocompat>
//= require <puremvc-mootools-port>
//=  require <uiframework/URLUtil>
//= require <findsalons/ApplicationFacade>
//= require <findsalons/AsyncProxy>
var TabNavigationMediator = function()
{
	this.Extends = Mediator;
	/**
	 * The TabBar instace used as the header nav
	 * 
	 * @type TabBar
	 */
	this.tabNavigation = null;

	this.initialize = function(viewComponent /* TabBar */)
	{
		this.parent(TabNavigationMediator.NAME, viewComponent);
		this.tabNavigation = viewComponent;
	};

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

	this.onRegister = function()
	{
		// Check for an authenticated user
		var asyncProxy = this.facade.retrieveProxy(AsyncProxy.NAME);
		this.tabNavigation.setTabsByUserInfo(asyncProxy.data.userInfo);
		// Set the selected tab
		this.sendNotification(ApplicationFacade.RETRIEVE_CURRENT_HASH, null, TabNavigationMediator.NAME);
		// Listen for tab clicks
		this.tabNavigation.addEventListener('tabChanged', this.tabNavigation_tabChangedHandler, false, 0, this);
	};

	this.handleNotification = function(notification /* Notification */)
	{
		switch (notification.getName())
		{
			case AsyncProxy.AUTH_USER_RESULT:
			case AsyncProxy.LOGOUT_RESULT:
				this.tabNavigation.setTabsByUserInfo(notification.getBody());
				break;
				
			case AsyncProxy.COOKIE_CHECK_RESULT:
				if (notification.getBody().rememberMe)
					this.tabNavigation.setTabsByUserInfo(notification.getBody());
				break;

			case ApplicationFacade.BREADCRUMB_DATA_CHANGED:
				this.tabNavigation.setSelectedTabByBreadcrumbData(notification.getBody());
				break;
		}
	};

	this.tabNavigation_tabChangedHandler = function(event)
	{
		var hash = URLUtil.searchStringToObject(event.data.url);
		var merged;
		if (hash.action == "getListed")
		{
			merged = Object.merge(
			{
				action : false
			}, hash);
		}
		else
		{
			merged = Object.merge(
			{
				action : false,
				category : false,
				listing : false
			}, hash);
		}

		this.sendNotification(ApplicationFacade.SET_HASH, merged);
	};
};
TabNavigationMediator = new Class(new TabNavigationMediator());
TabNavigationMediator.NAME = "TabNavigationMediator";
