/**
 * @augments SimpleCommand
 */
var StartupCommand = new Class
({
	Extends:SimpleCommand,

	execute: function( note )
	{
		/**
		 * @type sy.Application
		 */
		var app = note.getBody();


		// MODEL
		this.facade.registerProxy( new NavigationProxy() );
		this.facade.registerProxy( new ApplicationProxy( app ) );
		this.facade.registerProxy( new BgProxy( SyConfig.bgImages ) );


		// VIEW
		this.facade.registerMediator( new NavigationMediator( app.navigation ) );
		this.facade.registerMediator( new URLStateMediator() );
		this.facade.registerMediator( new BackgroundImageMediator( app.bgImage ) );
		this.facade.registerMediator( new StageMediator() );
		this.facade.registerMediator( new ContentpaneMediator( app.contentPane ) );


	}
});




var BrowserResizeCommand = new Class
({
	Extends: SimpleCommand,

	execute: function( note )
	{
		var bg = this.facade.retrieveMediator( BackgroundImageMediator.NAME );
		bg.adjustSize(  this.facade.retrieveProxy( ApplicationProxy.NAME ).getApplication().gridCanvas._element );
	}
});




var ApplicationInitReadyCommand = new Class
({
	Extends: SimpleCommand,

	execute:  function( note )
	{
		var navMed = this.facade.retrieveMediator( NavigationMediator.NAME );
		var urlStateMed = this.facade.retrieveMediator( URLStateMediator.NAME );

		
		switch( note.getName() )
		{
			
			case NotificationList.APPLICATION_INIT_READY_DEEPLINK_DATA_LOADED :

				navMed.initDeeplink();
				urlStateMed.action.delay( 700, urlStateMed );
				break;

			default:
				
				var application = this.facade.retrieveProxy( ApplicationProxy.NAME ).getApplication();
				application.isReady = true;


				// quick deeplink build
				if( urlStateMed.fragment ) {

					//this.facade.registerProxy( new CaseStudyDeeplinkStartProxy( SyConfig.dataURL ) );
					this.facade.registerProxy( new CasestudyProxy( SyConfig.dataURL, true ) );

				// normal direct call
				}else{
					navMed.initNormal();
				}

				this.sendNotification( NotificationList.APPLICATION_INIT_READY );

				break;
		}
		
	}
});






var URLChangedCommand = new Class
({
	Extends: SimpleCommand,

	execute: function( note )
	{

		d(">> URLChangedCommand::execute()");
		var transitionDelay = 800;
		
		/**
		 * @type NavigationMediator
		 */
		var navMed = this.facade.retrieveMediator( NavigationMediator.NAME );
		/**
		 * @type ContentpaneMediator
		 */
		var paneMed = this.facade.retrieveMediator( ContentpaneMediator.NAME );
		var application = this.facade.retrieveProxy( ApplicationProxy.NAME ).getApplication();

		/**
		 * @type NavigationProxy
		 */
		var navProx = this.facade.retrieveProxy( NavigationProxy.NAME );
		var activeMenPt = navProx.getActiveMenPt();

		var fragment = note.getBody().replace( /^\//, "" );
		var tokens = fragment.split("/");

		var medsToRemove;

		var nicename = null;

		var attemptGridRemoval = true;



		var pageTracker = _gat._getTracker('UA-16656253-2');
		if( pageTracker ) {
			pageTracker._trackPageview("/"+fragment);
			pageTracker._trackEvent("test");
		}


		if( application.isReady ) {

			var bgUrl;
			switch( tokens[0] )
			{
				case SyConfig.NAV_WHY :

					navProx.setActiveMenPt( SyConfig.NAV_WHY );
					bgUrl = SyConfig.bgImages[0];
					navMed.hideWorkSub();
					paneMed.show( SyConfig.paneHeightWhy );
					medsToRemove = [ CaseStudyMediator, ContentContactMediator, ContentBlogMediator ];

					if( !this.facade.hasMediator( ContentWhyMediator.NAME ) ) {
						this.facade.registerMediator( new ContentWhyMediator( this.facade.retrieveProxy( ApplicationProxy.NAME ).getApplication().contentWhy ) );
					}

					nicename = tokens[0];
					
					break;

				case SyConfig.NAV_WORK :

					navProx.setActiveMenPt( SyConfig.NAV_WORK );
					bgUrl = SyConfig.bgImages[1];
					var doDelay = navMed.getNav().copyline.get("opacity");
					navMed.showWorkSub( doDelay );
					medsToRemove = [ ContentContactMediator, ContentWhyMediator, ContentBlogMediator ];

					nicename = SyConfig.NAV_WORK;
					
					// sub menu press
					if( tokens.length == 2 ) {


						if( activeMenPt == tokens[0] ) {
							transitionDelay = 400;
						}

						var csProxy = this.facade.retrieveProxy( CasestudyProxy.NAME );
						var cs = tokens[1];
						/**
						 * @type CaseStuduyVo
						 */
						var vo = csProxy.getVoByNicename( cs );


						
						if( !this.facade.hasMediator( CaseStudyMediator.NAME ) ) {
							this.facade.registerMediator( new CaseStudyMediator( this.facade.retrieveProxy( ApplicationProxy.NAME ).getApplication().contentCasestudies ) );
						}

						var he = this.facade.retrieveMediator( CaseStudyMediator.NAME ).updateContent( vo );

						
						//paneMed.show( SyConfig.paneHeightCasestudyBase );
						paneMed.show( he );

						navMed.setActiveWorkSubPt( tokens[1] );

						nicename = tokens[1];

						bgUrl = "cs/" + nicename;

					}else{
						if( activeMenPt == tokens[0] && !tokens[1] ) {
							bgUrl = null;
						}
						attemptGridRemoval = false;
						paneMed.hide();
					}

					// TODO notify change to navigation

					break;

				case SyConfig.NAV_CONTACT :

					navProx.setActiveMenPt( SyConfig.NAV_CONTACT );
					bgUrl = SyConfig.bgImages[2];
					medsToRemove = [ CaseStudyMediator, ContentWhyMediator, ContentBlogMediator ];
					paneMed.show( SyConfig.paneHeightContact );
					navMed.hideWorkSub();

					if( !this.facade.hasMediator( ContentContactMediator.NAME ) ) {
						this.facade.registerMediator( new ContentContactMediator( this.facade.retrieveProxy( ApplicationProxy.NAME ).getApplication().contentContact ) );
					}

					nicename = tokens[0];
					
					break;

				case SyConfig.NAV_BLOG :

					navProx.setActiveMenPt( SyConfig.NAV_BLOG );
					bgUrl = SyConfig.bgImages[2];
					medsToRemove = [ CaseStudyMediator, ContentWhyMediator, ContentContactMediator ];
					paneMed.show( 300 );
					navMed.hideWorkSub();

					if( !this.facade.hasMediator( ContentBlogMediator.NAME ) ) {
						this.facade.registerMediator( new ContentBlogMediator( this.facade.retrieveProxy( ApplicationProxy.NAME ).getApplication().contentBlog ) );
					}
					
					nicename = tokens[0];
					
					break;

				case "" :

					bgUrl = SyConfig.bgImages[3];
					if( activeMenPt != SyConfig.NAV_CONTACT && activeMenPt != SyConfig.NAV_WHY ) {
						var bgProx = this.facade.retrieveProxy( BgProxy.NAME );
						bgProx.load( "images/backgrounds/" + bgUrl + ".jpg" );
					}
					
					nicename = tokens[0];
					return;
				
			}

			if( !nicename ) {
				this.sendNotification( NotificationList.URLSTATE_REQUEST, "" );
				return;
			}


			//d( "active: " , activeMenPt, tokens[0], tokens[1] );
			if( bgUrl ) {
				var bgProx = this.facade.retrieveProxy( BgProxy.NAME );
				bgProx.load( "images/backgrounds/" + bgUrl + ".jpg" );
				this.facade.retrieveMediator( NavigationMediator.NAME ).leaveInitState();
				if( this.facade.hasMediator( GridMediator.NAME ) && attemptGridRemoval ) {
					this.facade.removeMediator( GridMediator.NAME );
				}
				//this.facade.retrieveMediator( GridMediator.NAME ).leaveInitState(); // TODO
			}

	

			if( medsToRemove instanceof Array ){
				medsToRemove.each( function( medClass ) {
					if( this.facade.hasMediator( medClass.NAME ) ) {
						this.facade.removeMediator( medClass.NAME );
					}
				}.bind(this));
			}

			navMed.setActiveMainPt( tokens[0] );

			this.sendNotification( NotificationList.TRANSITION_START );
			this.sendNotification.delay( transitionDelay, this, NotificationList.TRANSITION_END );

		}else{
			d( "URLChangedCommand.execute() supressed - application not ready" , note.getBody() );
		}

	}
});

var GridItemHoverCommand = new Class
({
	Extends: SimpleCommand,
	execute: function( note )
	{
		var tile = note.getBody();
		var dataProx = this.facade.retrieveProxy( CasestudyProxy.NAME );
		var vo = dataProx.getVoByTileTriggerName( tile.get("id") );
		var gridMed = this.facade.retrieveMediator( GridMediator.NAME );
		gridMed.updateHoverBoard( vo.gridTitle, vo.nicename );
		//d( "GridItemHoverCommand::execute", tile );

	}
});


var URLChangedInitialCommand = new Class
({
	Extends: SimpleCommand,

	execute: function( note )
	{
		switch( note.getName() )
		{
			case NotificationList.URLSTATE_INITIAL_CHANGED_NO_FRAGMENT :
		
				this.facade.registerProxy( new CasestudyProxy( SyConfig.dataURL ) );
				break;
		}
	}
});



var InitiaGridActionCommand = new Class
({
	Extends: SimpleCommand,

	execute: function()
	{
		this.facade.registerMediator( new GridMediator( this.facade.retrieveProxy( ApplicationProxy.NAME ).getApplication().gridCanvas ) );
		this.facade.registerProxy( new GridProxy( SyConfig.gridImages ) );						
	}
});






var BackgroundLoadedCommand = new Class
({
	Extends: SimpleCommand,

	execute: function( note )
	{

		var img = note.getBody();
		var bgMed = this.facade.retrieveMediator( BackgroundImageMediator.NAME );
		bgMed.injectImage( img, !this.facade.hasMediator( GridMediator.NAME ) );

	}
});



var PaneHideCompleteCommand = new Class
({
	Extends: SimpleCommand,
	execute: function()
	{
		d("PaneHideCompleteCommand::execute");
		// TODO : deactivate menu
			// if

		/**
		 * @type NavigationProxy
		 */
		var navMed = this.facade.retrieveMediator( NavigationMediator.NAME );
		var navProx = this.facade.retrieveProxy( NavigationProxy.NAME );
		var activeMenPt = navProx.getActiveMenPt();
		d( "activeMenPt:", activeMenPt );
		switch( activeMenPt )
		{
			case SyConfig.NAV_CONTACT :
				navMed.reset();
				this.sendNotification( NotificationList.URLSTATE_REQUEST, "" );
				if( this.facade.hasMediator( ContentContactMediator.NAME ) ) {
					this.facade.removeMediator( ContentContactMediator.NAME );
				}
				break;

			case SyConfig.NAV_WORK :
				navMed.resetSubs();
				break;

			case SyConfig.NAV_WHY :
				navMed.reset();
				this.sendNotification( NotificationList.URLSTATE_REQUEST, "" );
				if( this.facade.hasMediator( ContentWhyMediator.NAME ) ) {
					this.facade.removeMediator( ContentWhyMediator.NAME );
				}
				break;
		}

	}
});


var PaneCloseCommand = new Class
({
	Extends: SimpleCommand,
	execute: function()
	{
		if( this.facade.hasMediator( CaseStudyMediator.NAME ) ) {
			this.sendNotification( NotificationList.URLSTATE_REQUEST, SyConfig.NAV_WORK );
			this.facade.removeMediator( CaseStudyMediator.NAME );
		}
	}

});

var PaneCloseExtCommand = new Class
({
	Extends: SimpleCommand,
	execute: function()
	{
		if( this.facade.hasMediator( ContentBlogMediator.NAME ) ) {
			this.facade.retrieveMediator( NavigationMediator.NAME ).reset();
		}
		this.facade.removeMediator( ContentContactMediator.NAME );
		this.facade.removeMediator( ContentBlogMediator.NAME );
		this.facade.retrieveMediator( ContentpaneMediator.NAME ).hide(); 
	}
});


var TransitionStartCommand = new Class
({
	Extends: SimpleCommand,
	execute: function()
	{
		this.facade.retrieveMediator( NavigationMediator.NAME ).deactivate();
		d("TransitionStartCommand");
	}
});


var TransitionEndCommand = new Class
({
	Extends: SimpleCommand,
	execute: function()
	{
		this.facade.retrieveMediator( NavigationMediator.NAME ).activate();
		d("TransitionEndCommand")
	}
});


