//= require <mootools-core-1.3-full-nocompat>
//= require <puremvc-mootools-port>
//= require <findsalons/ApplicationFacade>
//= require <findsalons/AsyncProxy>

var ShellMediator = function()
{
	this.Extends = Mediator;

	/**
	 * The Shell instance
	 * 
	 * @type Shell
	 */
	this.shell = null;
	/**
	 * Constructor
	 */
	this.initialize = function(viewComponent /* Shell */)
	{
		this.parent(ShellMediator.NAME, viewComponent);
		this.shell = this.getViewComponent();
	};

	/**
	 * @private
	 */
	this.listNotificationInterests = function()
	{
		return [ApplicationFacade.URL_CHANGE];
	};

	this.handleNotification = function(note /* Notification */)
	{
		switch(note.getName())
		{
			case ApplicationFacade.URL_CHANGE:
				this.notification_urlChange(note);
				break;
				
			case ApplicationFacade.EDIT_LISTING:
				this.editListing_notificationHandler(note.getBody());
				break;
		}
	};

	/**
	 * Listens for additional mediator registration events from the shell and
	 * checks for a cookie that might be present.
	 */
	this.onRegister = function()
	{
		this.parent();
		this.shell.addEventListener("registerMediator", this.shell_registerMediatorHandler, false, 0, this);
		this.shell.addEventListener("setHash", this.shell_setHashHandler, false, 0, this);
		// Check for a cookie
		var asyncProxy = this.facade.retrieveProxy(AsyncProxy.NAME);
		asyncProxy.checkCookie();
	};
	
	// ---------------------------------------------------------
	//
	// Notification handlers
	
	this.notification_urlChange = function(note /*Notification */)
	{
		var hash = note.getBody();
		this.shell.showAccoutSetupForm(hash.action == "getListed");
	};
	
	this.editListing_notificationHandler = function(listing /*Object*/)
	{
		
	};

	// ---------------------------------------------------------
	//
	// Event handlers

	/**
	 * Registers the specified mediator with the View. This is used for
	 * late-creation child components.
	 * 
	 * @param event
	 *            UICEvent containing an array of 2 elements in the data
	 *            property.
	 */
	this.shell_registerMediatorHandler = function(event)
	{
		var mediatorClass = event.data[0];
		var view = event.data[1];
		this.facade.registerMediator(new mediatorClass(view));
	};

	/**
	 * Notifies the system when the view requests a hash change as a result of
	 * user interaction.
	 * 
	 * @param event
	 *            UICEvent containing the hash object in the data property.
	 */
	this.shell_setHashHandler = function(event)
	{
		this.facade.sendNotification(ApplicationFacade.SET_HASH, event.data);
	};
};
ShellMediator = new Class(new ShellMediator());
ShellMediator.NAME = 'ShellMediator';
