/**
 * @augments Mediator
 */
var URLStateMediator = new Class
({
	Extends: Mediator,

	initial:true,
	currentUrl: null,
	checkInt: null,
	fragment: null,
	u: null,
	
	initialize: function()
	{
		this.parent( URLStateMediator.NAME );
		this.currentUrl = window.location.href;
	},

	onRegister: function()
	{
		this.registerChange();
		this.checkInt = this.checkCycle.periodical( 500, this );
		if( this.fragment ) {

			this.sendNotification( NotificationList.URLSTATE_INITIAL_CHANGED_WITH_FRAGMENT );
		}else{
			
			this.sendNotification( NotificationList.URLSTATE_INITIAL_CHANGED_NO_FRAGMENT );
		}
	},

	onRemove: function()
	{
		$clear( this.checkInt );
		this.u = null;
	},

	listNotificationInterests: function()
	{
		return [
			NotificationList.URLSTATE_REQUEST
		];
	},

	handleNotification: function( note )
	{
		switch( note.getName() )
		{
			case NotificationList.URLSTATE_REQUEST :

				var fragment = note.getBody();
				this.u.set("fragment", "/"+fragment );
				this.u.go();
				this.registerChange();
				break;
				
			default:
				
				break;
		}
	},

	checkCycle: function()
	{
		if( this.currentUrl != window.location.href ) {
			this.registerChange();
		}
	},

	registerChange: function()
	{
		this.u = new URI( window.location.href );
		this.currentUrl = window.location.href;
		this.fragment = this.u.get("fragment");
		if( this.fragment ) {
			if( this.initial ) {
				this.saveActionForDelay();
			}else{
				this.action();
			}
		}
		this.initial = false;
	},

	/**
	 * @deprecated
	 */
	saveActionForDelay: function()
	{
		
	},
	
	action: function()
	{
		this.sendNotification( NotificationList.URLSTATE_CHANGED, this.fragment );

		this.fragment = null;
	}	
});
URLStateMediator.NAME = "URLStateMediator";



var StageMediator = new Class
({
	Extends: Mediator,

	resizeClosure:null,
	
	initialize: function()
	{
		this.parent( StageMediator.NAME );
		this.resizeClosure = this.resizeListener.bind(this);
	},

	onRegister: function()
	{
		window.addEvent( "resize", this.resizeClosure );
		this.resize();
	},

	onRemove: function()
	{

	},

	listNotificationInterests: function()
	{
		return [];
	},

	handleNotification: function( note )
	{
		switch (note.getName()) {
			default:

				break;
		}
	},

	resize: function()
	{
		this.sendNotification( NotificationList.BROWSER_RESIZE );
	},

	resizeListener: function()
	{
		this.resize();
	}
});
StageMediator.NAME = "StageMediator";



var BackgroundImageMediator = new Class
({
	Extends: Mediator,

	iWOrig:null,
	iHOrig:null,
	iFac:null,
	
	initialize: function( viewComponent )
	{
		this.parent( BackgroundImageMediator.NAME, viewComponent );
	},

	/**
	 * 
	 * @param img
	 * @param fade
	 */
	injectImage: function( img, fade )
	{

		var imgs = document.id("backgroundImageShell").getElements( "img" );
		if( imgs.length > 1 ) {
			imgs.each( function( el ) {
				el.erase("id");			
			});
			var toDel = imgs[ 0 ];
			toDel.destroy();
		}
		img.set( "id", "backgroundImage" );
		img.inject( document.id("backgroundImageShell"), "bottom" );
		this.setViewComponent( new ui.BackgroundImage( img ) );
		this.initNewPic();
		this.sendNotification( NotificationList.BROWSER_RESIZE );

		if( fade ) {
			img.set( "opacity", 0).tween( "opacity", 1 );
		}
	},
	
	adjustSize: function( canvas )
	{
		//d( canvas.getScrollSize().y, canvas.getSize().y, window.getScrollSize().y, document.id("contentLayer").getScrollSize().y );
		var cS = canvas.getSize();
		//var cS = window.getScrollSize();
		//canvas.setStyle("height", cS.y );
		var cW = cS.x;
		var cH = cS.y;
		var cFac = cW / cH;
		var scMode;
		var imgH = this.getBg().getSize().y;
		var imgW = this.getBg().getSize().x;
		var img = this.getBg().getDomElement();


		var wRatio = cW / imgW;
		var hRatio = cH / imgH;
		var scaleMode = wRatio > hRatio ? "hor" : "vert";


		if( scaleMode == "hor" ) {
			img.setStyles( { width:cW, height:Math.ceil( cW / this.iFac ) } );
		}else{
			img.setStyles( { height:cH, width:Math.ceil( cH * this.iFac ) } );
		}

	},
	onRegister: function()
	{
		this.initNewPic();
	},

	onRemove: function()
	{

	},

	listNotificationInterests: function()
	{
		return [];
	},

	handleNotification: function( note )
	{
		switch (note.getName()) {
			default:

				break;
		}
	},

	initNewPic: function()
	{
		var iS = this.getBg().getSize();
		this.iWOrig = iS.x;
		this.iHOrig = iS.y;
		this.iFac = this.iWOrig / this.iHOrig;
	},

	/**
	 * @return ui.BackgroundImage
	 */
	getBg: function()
	{
		return this.getViewComponent();
	}
});
BackgroundImageMediator.NAME = "BackgroundImageMediator";



var GridMediator = new Class
({
	Extends: Mediator,
	
	initialize: function( viewComponent )
	{
		this.parent( GridMediator.NAME, viewComponent );
	},

	/**
	 *
	 * @param {ui.GridItem} tile
	 * @param {String} title
	 * @param {String} nicename
	 */
	updateHoverBoard: function( title, nicename )
	{
		var b = this.getGrid().hoverBoard;
		b.getElement( "strong" ).set( "html", title );
		b.getElement( "input[name=triggerHash]" ).set("value", SyConfig.NAV_WORK + "/" + nicename );
	},

	onRegister: function()
	{
		this.getGrid().addEvent( "hoverTile", this.hoverTileListener.bind( this )  );
		this.getGrid().addEvent( "requestCaseStudy", this.requestCaseStudyListener.bind( this )  );
		document.id( "loadingInit" ).setStyle("display", "block");
	},

	onRemove: function()
	{
		this.getGrid().removeEvents( "hoverTile" );
		this.getGrid().removeEvents( "requestCaseStudyListener" );
		this.getGrid().dispose();
		this.getGrid().hide();
	},

	listNotificationInterests: function()
	{
		return [
			NotificationList.GRID_IMAGES_LOADED,
			NotificationList.URLSTATE_REQUEST
		];
	},

	handleNotification: function( note )
	{
		switch (note.getName() )
		{
			case NotificationList.GRID_IMAGES_LOADED :
			
				this.getGrid().addEvent( EventList.GRID_READY, this.gridReadyListener.bind( this ) );
				var images = note.getBody();
				images.each( function( el ) {
					var m = el.get("src").match(/\/([^\.\/]+)\.jpg$/);
					var tileId = m[1];
					this.getGrid().injectImage( el, tileId );
				}.bind(this));
				this.getGrid().show();

				document.id( "loadingInit" ).destroy();
				
				break;

			case NotificationList.URLSTATE_REQUEST :

				this.facade.removeMediator( GridMediator.NAME );

				break;

			default:

				break;
		}
	},

	/**
	 * @return ui.GridCanvas
	 */
	getGrid: function()
	{
		return this.viewComponent;
	},

	gridReadyListener: function()
	{
		this.getGrid().removeEvents( EventList.GRID_READY );
		this.sendNotification( NotificationList.GRID_READY );
	},

	hoverTileListener: function( tile )
	{
		this.sendNotification( NotificationList.GRID_HOVER_ITEM, tile );
	},

	requestCaseStudyListener: function( nicename )
	{
		this.sendNotification( NotificationList.URLSTATE_REQUEST, nicename );
	}
});
GridMediator.NAME = "GridMediator";





var NavigationMediator = new Class
({
	Extends: Mediator,

	workActive:false,
	isInInitstate: true,

	initialize: function( viewComponent )
	{
		this.parent( NavigationMediator.NAME, viewComponent );
	},

	initNormal: function()
	{
		var nav = this.getNav();
		nav.build();
	},
	activate: function()
	{
		this.getNav().deactivator.hide();
	},
	deactivate: function()
	{
		this.getNav().deactivator.show();
	},
	setActiveMainPt: function( nicename )
	{
		this.getNav().setActiveMainPt( nicename );
		this.leaveInitState();
		d("##### NavigationMediator::setActiveMainPt()");
	},

	setActiveWorkSubPt: function( nicename )
	{
		this.getNav().setActiveWorkSubPt( nicename );
	},

	reset: function()
	{
		this.getNav().resetMenStates();
	},

	resetSubs: function()
	{
		this.getNav().resetSubMenStates();
	},
	/**
	 * @return void
	 */
	initDeeplink: function()
	{
		this.getNav().buildQuickly();
	},

	leaveInitState: function()
	{
		if( this.isInInitstate  ) {
			this.getNav().hideCopyLine();
			this.isInInitstate = false;
		}
	},


	showWorkSub: function( doDelay )
	{
		if( !this.workActive ) {
			var del = doDelay ? 800 : 400;
			this.getNav().showWork.delay( del, this.getNav() );
		}
		this.workActive = true;
	},

	hideWorkSub: function()
	{
		if( this.workActive ) {
			this.getNav().hideWork();
			this.workActive = false;
		}
	},



	onRegister: function()
	{

	},

	onRemove: function()
	{

	},

	listNotificationInterests: function()
	{
		return [
			NotificationList.URLSTATE_REQUEST
		];
	},

	handleNotification: function( note )
	{
		switch( note.getName() )
		{
			case NotificationList.URLSTATE_REQUEST :
				if(  note.getBody().match(/^work/) ) {
					this.leaveInitState();
				}
				break;

			default:

				break;
		}
	},

	/**
	 * @return ui.Navigation
	 */
	getNav: function()
	{
		return this.viewComponent;
	}
	
});
NavigationMediator.NAME = "NavigationMediator";


/**
 * 
 */
var ContentpaneMediator = new Class
({
	Extends: Mediator,

	active: false,
	
	initialize: function( viewComponent )
	{
		this.parent( ContentpaneMediator.NAME, viewComponent );
	},

	show: function( height )
	{
		var p = this.getPane();
		if( this.active ) {
			p.updateHeight( height );
			return;
		}

		p.show( height );
		this.active = true;
	},

	hide: function()
	{
		if( !this.active ) {
			return;
		}
		this.getPane().hide();
		this.active = false;
	},
	
	onRegister: function()
	{
		this.getPane().addEvent( EventList.CONTENTPANE_HIDE_COMPLETE, this.paneListener.bind( this ) );
		this.getPane().addEvent( EventList.CONTENTPANE_SHOW_COMPLETE, this.paneListener.bind( this ) );
	},

	onRemove: function()
	{

	},

	listNotificationInterests: function()
	{
		return [

		];
	},

	handleNotification: function( note )
	{
		switch( note.getName() )
		{
			default:

				break;
		}
	},
	/**
	 * @return ui.ContentPane
	 */
	getPane: function()
	{
		return this.viewComponent;
	},


	paneListener: function( completeDirection )
	{
		switch( completeDirection )
		{
			case "show" :

				this.sendNotification( NotificationList.PANE_SHOW_COMPLETE );
				break;

			case "hide" :

				this.sendNotification( NotificationList.PANE_HIDE_COMPLETE );
				break;
		}
	}
});
ContentpaneMediator.NAME = "ContentpaneMediator";


/**
 * abtract
 */
var ContentMediator = new Class
({
	Extends: Mediator,

	active: false,

	initialize: function( name, viewComponent )
	{
		this.parent( name, viewComponent );
	},
	
	dimDown: function( elementsToDim )
	{
		elementsToDim.each( function( el ) {
			if( !Browser.Engine.trident && !( el instanceof Element ) ) {
				return;
			}
			el.tween( "opacity", 0 );
		}.bind(this));
	},
	onRegister: function()
	{
		this.active = false;
	}

});


var CaseStudyMediator = new Class
({
	Extends: ContentMediator,
	showDelayInt:null,
	/**
	 * @type CaseStuduyVo
	 */
	currentVo:null,

	initialize: function( viewComponent )
	{
		this.parent( CaseStudyMediator.NAME, viewComponent );
	},

	/**
	 *
	 * @param {CaseStuduyVo} cVo
	 */
	updateContent: function( cVo )
	{
		if( !cVo ) {
			return;
		}
		this.currentVo = cVo;
		var c = this.getCont();
		c.reset();
		c.title.getElement("h2").set( "text", cVo.title );
		var el = c.getDomElement();
		var displayType;
		if( cVo.videoURL ) {
			displayType = "video";
		}else if( cVo.gallery && cVo.gallery.length ) {
			displayType = "gallery";
		}else{
			displayType = "text";
		}

		$clear( this.showDelayInt );
		
		var elementsToDim;

		if( displayType == "video" || displayType == "gallery" ) {

			el = document.id( "contentMedia" );
			if( Browser.Engine.trident ) {
				el.getElement(".r h4").set( "text", cVo.textP1 );
				var div = new Element( "div" ).setProperty( "html", cVo.textP2 );
				if( el.getElement(".r .inner p") ) {
					el.getElement(".r .inner p").destroy();
				}
				if( el.getElement(".r .inner div") ) {
					el.getElement(".r .inner div").destroy();
				}
				el.getElement(".r .inner").adopt( div );
			}else{
				el.getElement(".r h4").set( "html", cVo.textP1 );
				el.getElement(".r p").set( "html", cVo.textP2 );
			}

			//alert( el.getElement(".r h4") );

			elementsToDim = [ c.contentNoMedia ];
			c.getDomElement().set( "class", "media" );
			var del = this.active ? 100 : 1200;
			this.showDelayInt = c.contentMedia.show().set("opacity", 0).tween.delay( del, c.contentMedia, ["opacity", 1] );

			if( displayType == "video" )  {
				this.getCont().initVideoBar();
				this.getCont().initVideo( cVo.videoURL );
			}else{
				this.getCont().resetVideoBar();
			}
			if( cVo.gallery && cVo.gallery.length ) {
				this.getCont().initGalleryBar();
				if( displayType != "video" ) {
					this.getCont().initGallery( cVo.gallery );
				}
			}else{
				this.getCont().resetGalleryBar();
			}

		}else{
		
			el = document.id( "contentNomedia" );
			el.getElement( ".l .inner" ).set( "html", cVo.textP1 );
			el.getElement( ".r .rl" ).set( "html", cVo.textP2 );
			el.getElement( ".r .rr" ).set( "html", cVo.textP3 );
			
			this.getCont().resetGalleryBar();
			this.getCont().resetVideoBar();
			this.getCont().initTextBar();

			elementsToDim = [ c.contentMedia ];
			c.getDomElement().set( "class", "nomedia" );
			var del = this.active ? 100 : 1200;
			this.showDelayInt = c.contentNoMedia.show().set("opacity", 0).tween.delay( del, c.contentNoMedia, ["opacity", 1] );
			
		}

		this.dimDown( elementsToDim );
		
		if( !this.active ) {
			c.getDomElement().setStyle("display", "block").set( "opacity", 0 ).tween( "opacity", 1 );
		}

		this.active = true;

		return el.getSize().y + SyConfig.CASESTUDY_BOTTOM_ADD;
	},
	
	onRegister: function()
	{
		//d("CaseStudyMediator::onRegister");
		this.parent();
		this.getCont().configureBehaviour();
		this.getCont().controlBar.addEvent( EventList.CONTROLBAR_CLICK_CLOSE, this.clickCloseListener.bind(this) );
		this.getCont().controlBar.addEvent( EventList.CONTROLBAR_CLICK_GALLERYTOGGLE, this.clickGalleryToggle.bind( this ));
		this.getCont().controlBar.addEvent( EventList.CONTROLBAR_CLICK_VIDEOTOGGLE, this.clickVideoToggle.bind( this ));

	},

	onRemove: function()
	{
		this.getCont().controlBar.removeEvents( EventList.CONTROLBAR_CLICK_CLOSE );
		this.getCont().controlBar.removeEvents( EventList.CONTROLBAR_CLICK_GALLERYTOGGLE );
		this.getCont().controlBar.removeEvents( EventList.CONTROLBAR_CLICK_VIDEOTOGGLE );
		this.getCont().dispose();
		var elementsToDim = [ this.getCont().contentNoMedia, this.getCont().contentMedia ];
		this.dimDown( elementsToDim );

		//d( "CaseStudyMediator::onRemove" );
	},

	listNotificationInterests: function()
	{
		return [
			NotificationList.NOTIFY_PLAYER_STATECHANGE
		];
	},
	handleNotification: function( note )
	{
		switch( note.getName() )
		{
			case NotificationList.NOTIFY_PLAYER_STATECHANGE :

				this.getCont().syncPlayerState( note.getBody() );
				break;
			default:

				break;
		}
	},
	/**
	 * @return ui.ContentCasestudies
	 */
	getCont: function()
	{
		return this.viewComponent;
	},


	clickCloseListener: function()
	{
		d( "CaseStudyMediator:: ClickCloseListener " );
		this.sendNotification( NotificationList.PANE_CLOSE );
	},

	clickGalleryToggle: function()
	{
		d( "CaseStudyMediator:: ClickGalleryListener " );
		this.getCont().dispose( true );
		this.getCont().initGallery( this.currentVo.gallery );
	},

	clickVideoToggle: function()
	{
		d( "CaseStudyMediator:: ClickVideoListener " );

		this.getCont().dispose( true );

		this.getCont().initVideo( this.currentVo.videoURL );
	}

});
CaseStudyMediator.NAME = "CaseStudyMediator";




var ContentWhyMediator = new Class
({
	Extends: ContentMediator,

	initialize: function( viewComponent )
	{
		this.parent( ContentWhyMediator.NAME, viewComponent );
	},

	onRegister: function()
	{
		this.parent();
		var el = this.viewComponent.getDomElement();
		el.show().set( "opacity", 0 ).tween.delay( 1200,  el, ["opacity", 1] );
		document.id("contControlbar").setStyle( "display", "none" ).setStyle.delay( 1200, document.id("contControlbar"), [ "display", "block" ] );
		document.id("contrTitle").getElement("h2").set("html", SyConfig.TITLE_WHY );
		this.getWhy().enableBehaviour();
		this.getWhy().addEvent( "clickClose", this.clickCloseListener.bind(this) );
	},

	onRemove: function()
	{
		d( "ContentWhyMediator::onRemove" );
		this.getWhy().removeEvents( "clickClose" );
		this.getWhy().disableBehaviour();
		var elementsToDim = [ this.viewComponent.getDomElement() ];
		this.dimDown( elementsToDim );

	},

	listNotificationInterests: function()
	{
		return [

		];
	},

	handleNotification: function( note )
	{
		switch( note.getName() )
		{
			default:

				break;
		}
	},

	/**
	 * @return ui.ContentWhy
	 */
	getWhy: function()
	{
		return this.viewComponent;
	},


	clickCloseListener: function()
	{
		d( "click close" );
		this.sendNotification( NotificationList.PANE_CLOSE_EXT );
	}
	
});
ContentWhyMediator.NAME = "ContentWhyMediator";






var ContentContactMediator = new Class
({
	Extends: ContentMediator,

	initialize: function( viewComponent )
	{
		this.parent( ContentContactMediator.NAME, viewComponent );
	},

	onRegister: function()
	{
		d( "ContentContactMediator::onRegister" );
		this.parent();
		var el = this.viewComponent.getDomElement();
		el.show().set( "opacity", 0 ).tween.delay( 1200,  el, ["opacity", 1] );
		this.viewComponent.addEvent( "clickClose", this.clickCloseListener.bind( this ) );
		this.viewComponent.enableBehaviour()
		document.id( "contControlbar" ).setStyle( "display", "none");
	},

	onRemove: function()
	{
		this.viewComponent.removeEvents( "clickClose" );
		this.viewComponent.disableBehaviour();
		var elementsToDim = [ this.viewComponent.getDomElement() ];
		this.dimDown( elementsToDim );
		this.viewComponent = null;
	},

	listNotificationInterests: function()
	{
		return [

		];
	},

	handleNotification: function( note )
	{
		switch( note.getName() )
		{
			default:

				break;
		}
	},

	clickCloseListener: function()
	{
		d("ContentContactMediator::clickCloseListener()");
		this.sendNotification( NotificationList.PANE_CLOSE_EXT );
	}

});
ContentContactMediator.NAME = "ContentContactMediator";


var ContentBlogMediator = new Class
({
	Extends: ContentMediator,
	initialize: function( viewComponent )
	{
		this.parent( ContentBlogMediator.NAME, viewComponent );
		
	},
	onRegister: function()
	{
		d( "ContentBlogMediator::onRegister" );
		this.parent();
		var el = this.viewComponent.getDomElement();
		el.show().set( "opacity", 0 ).tween.delay( 1200,  el, ["opacity", 1] );
		this.viewComponent.addEvent( "clickClose", this.clickCloseListener.bind( this ) );
		this.viewComponent.enableBehaviour()
		document.id( "contControlbar" ).setStyle( "display", "none");
	},

	onRemove: function()
	{
		d( "ContentContactMediator::onRemove" );
		this.viewComponent.removeEvents( "clickClose" );
		this.viewComponent.disableBehaviour();
		var elementsToDim = [ this.viewComponent.getDomElement() ];
		this.dimDown( elementsToDim );
		this.viewComponent = null;
	},
	clickCloseListener: function()
	{
		d("ContentBlogMediator::clickCloseListener()");
		this.sendNotification( NotificationList.PANE_CLOSE_EXT );
	}
});
ContentBlogMediator.NAME = "ContentBlogMediator";
