window.DPI = window.DPI || {};

if (undefined == window.console || undefined == window.console.log)
{
	window.console = {log : function (message) {/*alert(message);*/}};
}

window.DPI.Apps = {
	DeHaaf : {
		Debug			: false,
		CMSActive	: false,
		FlashVersion : {},
		
		Content : {
			ajaxRequest : null,
			ajaxInitTime : null,
			loading: false,
			switching: false,
			type:	'page',

			Open : function (url) {
				// Navigate to the new page
				YAHOO.util.History.navigate("url", url);
			},

			Contract : function () {
				var control = $('#content-control');
				var content = $('div.block');

				control.removeClass('content-hide');
				control.addClass('content-show');

				content.css({overflow: 'hidden'});

				content.animate({
					top: $(window).height() - (2*34) -5
				}, {
					duration: 400,
					complete : function () {
						$(this).css('height', '5px');
					}
				});
			},

			IsContracted : function () {
				return $('#content-control').hasClass('content-show');
			},

			Expand : function () {
				var control = $('#content-control');
				var content = $('div.block');

				control.removeClass('content-show');
				control.addClass('content-hide');

				content.css({overflow: ''});
				content.css({height: ''});

				var outerHeight = content.outerHeight();
				var verticalTop = ($(window).height() - outerHeight) / 2;
				var topMargin = 65;
				var newTop = (verticalTop > topMargin) ? verticalTop : topMargin;

				content.animate({
					top: newTop
				}, 400);
			},

			ToggleContract : function () {
				if (window.DPI.Apps.DeHaaf.Content.IsContracted())
				{
					window.DPI.Apps.DeHaaf.Content.Expand();
				}
				else
				{
					window.DPI.Apps.DeHaaf.Content.Contract();
				}
			},

			Hide : function () {
				// Cancel any running requests
				$.doTimeout('content-show');

				window.DPI.JS.Effect.fadeOut('div.block', 300);
			},

			Show : function () {
				if (window.DPI.Apps.DeHaaf.Debug)
				{
					console.log('window.DPI.Apps.DeHaaf.Content.Show');
				}

				// Cancel any running requests
				$.doTimeout('content-show');

				window.DPI.JS.Effect.fadeIn('div.block', 300);

				window.DPI.Apps.DeHaaf.Content.switching = false;
			},
			
			Reposition : function () {
				// Cancel timeouts
				$.doTimeout('reposition');

				if (window.DPI.Apps.DeHaaf.Debug)
				{
					console.log('window.DPI.Apps.DeHaaf.Content.Reposition');
				}

				var contentBlock = $('div.block');
				var topMargin = 65;
				var leftMargin = 260; /* 25 + left bar width + 25 */
				var contentReady = true;

				// Reset positioning styles
				contentBlock.css({left : '', width: '', top: '', height: ''});
				
				var verticalAlign = ('page' === window.DPI.Apps.DeHaaf.Content.type || 'map' === window.DPI.Apps.DeHaaf.Content.type || ('video' === window.DPI.Apps.DeHaaf.Content.type && !window.DPI.Widgets.Video.fullscreen));
				if (verticalAlign)
				{
					var contentHeight = contentBlock.outerHeight();

					if (0 === contentHeight)
					{
						contentReady = false;
					}
					else
					{
						if ('video' === window.DPI.Apps.DeHaaf.Content.type && contentHeight < 50)
						{
							// While the video is loading the object tag has no height
							contentHeight = 430;
						}

						var verticalTop = ($(window).height() - contentHeight) / 2;
						var newTop = (verticalTop > topMargin) ? verticalTop : topMargin;

						contentBlock.css('top', newTop);
					}
				}

				var horizontalAlign = ('page' === window.DPI.Apps.DeHaaf.Content.type || 'map' === window.DPI.Apps.DeHaaf.Content.type || ('video' === window.DPI.Apps.DeHaaf.Content.type && !window.DPI.Widgets.Video.fullscreen));
				if (horizontalAlign)
				{
					var width = contentBlock.outerWidth();

					if (0 === width)
					{
						contentReady = false;
					}
					else
					{
						var newLeft = (leftMargin + (($(window).width() - leftMargin) / 2) - (width / 2));
						newLeft = Math.max(newLeft, leftMargin);

						contentBlock.css('left', newLeft);
					}
				}

				if ('video' === window.DPI.Apps.DeHaaf.Content.type
						&& window.DPI.Widgets.Video.fullscreen
						&& 0 !== window.DPI.Apps.DeHaaf.FlashVersion.major)
				{
					contentBlock.css('left', 0);
					contentBlock.css('top', 0);
					$('#movie').attr('width', $(window).width());
					$('#movie').attr('height', $(window).height());
				}

				if ('layout-design' === window.DPI.Apps.DeHaaf.Content.type)
				{
					contentBlock.css({						
						height	: $(window).height() - topMargin,
						top		: topMargin,
						width		: $(window).width() - leftMargin
					});

					var layoutEl = contentBlock.children('div.layout');

					var layoutElcontentWidth = layoutEl.outerWidth();
					if (0 === layoutElcontentWidth)
					{
						contentReady = false;
					}
					else
					{
						layoutEl.css('left', Math.max(((($(window).width() - leftMargin) / 2) - (layoutElcontentWidth / 2)), 0));
					}

					var layoutElcontentHeight = layoutEl.outerHeight();

					if (0 === layoutElcontentHeight)
					{
						contentReady = false;
					}
					else
					{
						layoutEl.css('top', Math.max(($(window).height() - topMargin - layoutElcontentHeight) / 2, 0));
					}
				}

				if (!contentReady)
				{
					$.doTimeout('reposition', 20, function () {
						window.DPI.Apps.DeHaaf.Content.Reposition();
					});
				}
			},

			Retrieve : function (url) {
				if (window.DPI.Apps.DeHaaf.Debug)
				{
					console.log('window.DPI.Apps.DeHaaf.Content.Retrieve', url);
				}

				url = ('' === url) ? '/' : url;

				// Cancel any running
				$.doTimeout('content-retrieve');

				if (null !== this.ajaxRequest)
				{
					// Stopt previous request
					this.ajaxRequest.abort();
					this.ajaxRequest = null;
				}

				window.DPI.Apps.DeHaaf.Content.Hide();

				this.ajaxInitTime = new Date();

				this.loading = true;
				this.switching = true;
				
				this.ajaxRequest = jQuery.ajax({
					url		: url,
					success	: window.DPI.Apps.DeHaaf.Content.onContentRetrieved,
					dataType	: 'json',
					error : window.DPI.Apps.DeHaaf.Content.onContentError
				});
			},

			onContentError : function (XMLHttpRequest, textStatus, errorThrown)
			{
				window.DPI.Apps.DeHaaf.Content.handleContent({
					html : 'Er is een fout opgetreden: ' + textStatus,
					type : 'page',
					title_for_layout : 'Fout opgetreden'
				});
			},

			onContentRetrieved : function (data) {
				if (window.DPI.Apps.DeHaaf.Debug)
				{
					console.log('window.DPI.Apps.DeHaaf.Content.onContentRetrieved');
				}
				
				window.DPI.Apps.DeHaaf.Content.ajaxRequest = null;
				window.DPI.Apps.DeHaaf.Content.loading = false;

				if (null === data)
				{
					// @todo no data recieved, possible cause is abort() call when done?
					return;
				}

				var timeDiff = (new Date().valueOf()) - window.DPI.Apps.DeHaaf.Content.ajaxInitTime.valueOf();
				if (timeDiff > 400)
				{
					window.DPI.Apps.DeHaaf.Content.handleContent(data);
				}
				else
				{
					$.doTimeout('content-retrieve', 400 - timeDiff, function () {
						window.DPI.Apps.DeHaaf.Content.handleContent(data);
					});
				}
			},

			handleContent : function (data) {
				if (window.DPI.Apps.DeHaaf.Debug)
				{
					console.log('window.DPI.Apps.DeHaaf.Content.handleContent', data);
				}

				// Update page title
				window.DPI.JS.setWindowTitle(data.title_for_layout);

				var prevType = window.DPI.Apps.DeHaaf.Content.type;
				window.DPI.Apps.DeHaaf.Content.type = data.type;

				var container = $('div.block-' + prevType);
				container.removeClass('block-' + prevType);
				container.addClass('block-' + data.type);

				var backgroundMapEl = $('div.background-map');
				var layoutDesignEl = $('div.layout-design');

				switch (data.type)
				{
					case 'map':
						if ('page' === prevType)
						{
							window.DPI.Widgets.Slideshow.hide(true);
							backgroundMapEl.stop(true, false);
							window.DPI.JS.Effect.fadeIn(backgroundMapEl, 400);

							//backgroundMapEl.css({opacity: 1, display: 'block'});
						}
						else if ('layout-design' === prevType || 'video' === prevType)
						{
							window.DPI.JS.Effect.fadeOut(layoutDesignEl, 400);
							window.DPI.JS.Effect.fadeIn(backgroundMapEl, 400);
						}

						break;
					case 'video':
					case 'layout-design':
						if ('page' === prevType)
						{
							window.DPI.Widgets.Slideshow.hide(true);
						}
						else if ('map' === prevType)
						{
							window.DPI.JS.Effect.fadeOut(backgroundMapEl, 400);
						}

						window.DPI.JS.Effect.fadeIn(layoutDesignEl, 400);
						break;
					default:
					case 'page':
						if ('layout-design' === prevType || 'video' === prevType)
						{
							window.DPI.JS.Effect.fadeOut(layoutDesignEl, 400);
						}
						else if ('map' === prevType)
						{
							window.DPI.JS.Effect.fadeOut(backgroundMapEl, 400);
						}

						window.DPI.Widgets.Slideshow.hide(false);
						break;
				}

				// Set new content
				$('div.block').empty();
				$('div.block').html(data.html);

				if ('page' === data.type)
				{
					$('#content-control').click(window.DPI.Apps.DeHaaf.Content.ToggleContract);
				}

				// Run font replacement for new content
				Cufon.replace('div.block .AvantGardeExtraLight', {fontFamily: 'ITC Avant Garde Gothic Extra Li'});
				Cufon.replace('div.block .DearJoe5', {fontFamily: 'dearJoe 5'});
				Cufon.replace('div.block .AvantGardeMedium', {fontFamily: 'ITC Avant Garde Gothic'});

				window.DPI.Apps.DeHaaf.Content.Reposition();

				if (!window.DPI.Apps.DeHaaf.CMSActive)
				{
					$('div.block a').click(window.DPI.Apps.DeHaaf.Content.onLinkClick);
					$('div.block area').click(window.DPI.Apps.DeHaaf.Content.onLinkClick);
				}

				if (data.type !== prevType && ('map' === data.type || 'layout-design' === data.type))
				{
					$.doTimeout('content-show', 400, function () {
						window.DPI.Apps.DeHaaf.Content.Show();
					});
					return;
				}

				if (!window.DPI.Widgets.Slideshow.initialized)
				{
					window.DPI.Apps.DeHaaf.Content.Show();
					return;
				}

				if (null == data.galleryID || null == data.imageID) {
					window.DPI.Apps.DeHaaf.Content.Show();
					return;
				}

				if (window.DPI.Widgets.Slideshow.imageID === data.imageID) {
					// Same image
					window.DPI.Apps.DeHaaf.Content.Show();
					return;
				}

				window.DPI.Widgets.Slideshow.showById(data.galleryID, data.imageID);
			},

			onLinkClick : function (e) {
				if (window.DPI.Apps.DeHaaf.Debug)
				{
					console.log('window.DPI.Apps.DeHaaf.Content.onLinkClick');
				}

				var href = window.DPI.Apps.DeHaaf.NormalizeHref($(e.currentTarget).attr('href'));

				if (window.DPI.Apps.DeHaaf.IsLocalHref(href))
				{
					// Stopt default event
					e.preventDefault();
					e.stopPropagation();

					// Activate the link
					window.DPI.Apps.DeHaaf.Content.Open(href);

					return false;
				}

				return true;
			}
		},

		/**
		 * Normalizes a href
		 * IE6 returns a absolute URL when you get the href attribute
		 */
		NormalizeHref : function (href) {
			var domain = 'http://' + window.location.hostname;

			if (-1 !== href.indexOf(domain))
			{
				href = href.replace(domain, '');
			}

			return href;
		},

		IsLocalHref : function (href) {
			if (href === '' || '/' !== href.substring(0, 1)) {
				return false;
			}

			return true;
		},

		onHomeClick : function ()
		{
			window.DPI.Apps.DeHaaf.Content.Open('/');
		},

		onHistoryChange : function (url)
		{
			//console.log('onHistoryChange', url);
			//console.log(YAHOO.util.History.getBookmarkedState("url"), YAHOO.util.History.getCurrentState("url"));
			
			if (window._gaq)
			{
				// Google analytics page tracking
				window._gaq.push(['_trackPageview', url]);
			}

			var link = window.DPI.Apps.DeHaaf.Menu.find(url);

			if (link)
			{
				window.DPI.Apps.DeHaaf.Menu.activate(link);
			}
			
			window.DPI.Apps.DeHaaf.Content.Retrieve(url);
		},

		Init : function () {
			$(window).resize(window.DPI.Apps.DeHaaf.Window.onResize);
			$(window).scroll(window.DPI.Apps.DeHaaf.Window.onScroll);

			$(document.body).addClass('js');

			Cufon.replace('.AvantGardeExtraLight', {fontFamily: 'ITC Avant Garde Gothic Extra Li'});
			Cufon.replace('.DearJoe5', {fontFamily: 'dearJoe 5'});
			Cufon.replace('.AvantGardeMedium', {fontFamily: 'ITC Avant Garde Gothic'});
			Cufon.now();
			
			if ('page' === window.DPI.Apps.DeHaaf.Content.type)
			{
				$('#content-control').click(window.DPI.Apps.DeHaaf.Content.ToggleContract);
			}

			window.DPI.Apps.DeHaaf.FlashVersion = swfobject.getFlashPlayerVersion();

			window.DPI.Apps.DeHaaf.Content.Reposition();
			window.DPI.Apps.DeHaaf.Menu.Reposition();

			if (!window.DPI.Apps.DeHaaf.CMSActive)
			{
				// Left bar links
				$('#menu a').click(window.DPI.Apps.DeHaaf.Content.onLinkClick);

				$('#menu > li > a').mouseover(window.DPI.Apps.DeHaaf.Menu.onMouseOver);
				$('#menu > li > a').mouseout(window.DPI.Apps.DeHaaf.Menu.onMouseOut);

				$('div.bottom a').click(window.DPI.Apps.DeHaaf.Content.onLinkClick);

				// Content links
				$('div.block a').click(window.DPI.Apps.DeHaaf.Content.onLinkClick);
				$('div.block area').click(window.DPI.Apps.DeHaaf.Content.onLinkClick);
			}

			// Initialize the history manager
			var startState = YAHOO.util.History.getBookmarkedState("url") || "";
			YAHOO.util.History.register("url", startState, this.onHistoryChange);
			YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");

			if (startState !== '')
			{
				if (!window.DPI.Apps.DeHaaf.CMSActive) {
					// Activate the page from history
					YAHOO.util.History.onReady(function () {
						window.DPI.Apps.DeHaaf.Content.Open(startState);

						// State changes has to be called manually because the event is not dispatched when
						// the state is unchanged
						window.DPI.Apps.DeHaaf.onHistoryChange(startState);
					});
				}
			}
			else
			{
				// Homepage, show video first
				//if ('/' === window.location.pathname && !window.DPI.Apps.DeHaaf.CMSActive)
				//{
					//window.DPI.Widgets.Video.Init();
				//}

				if ('/' === window.location.pathname && !window.DPI.Apps.DeHaaf.CMSActive)
				{
					$('div.leftbar').css({opacity: 0});
					$('div.content').css({opacity: 0});

					window.DPI.Apps.DeHaaf.Website.homepage = true;

					if ((window.DPI.Widgets.Slideshow.initialized && !window.DPI.Widgets.Slideshow.isLoading()) || 0 === window.DPI.Apps.DeHaaf.FlashVersion.major)
					{
						window.DPI.Apps.DeHaaf.Website.homepage = false;

						// Protection for cases where Flash and the image is loaded before the JavaScript initialization
						window.DPI.Apps.DeHaaf.Website.Show();
					}
				}
			}
		},

		Menu : {
			activate : function (linkEl) {
				if (window.DPI.Apps.DeHaaf.Debug)
				{
					console.log('window.DPI.Apps.DeHaaf.Menu.activate', linkEl);
				}

				var newActive = $(linkEl).parent('li');
				var newParent = null;
				var prevActive = $('#menu li.activeChild');
				var prevParent = null;

				if (newActive.get(0) === prevActive.get(0)) {
					// No changes
					return;
				}

				if (newActive.parent().parent('li').length > 0)
				{
					newParent = newActive.parent().parent('li').get(0);
				}

				if (prevActive.parent().parent('li').length > 0)
				{
					prevParent = prevActive.parent().parent('li').get(0);
				}

				// Activate the new item
				$(newActive).addClass('activeChild');

				var realPrevParent = ((null === prevParent) ? prevActive.get(0) : prevParent);
				var realNewParent = ((null === newParent) ? newActive.get(0) : newParent);
				
				//console.log('parentcomp', realNewParent, realPrevParent);
				if (realNewParent !== realPrevParent)
				{
					// Parent is changed
					if (prevActive.get(0) !== realPrevParent)
					{
						// Remove child active status from old parent
						$(prevActive).removeClass('activeChild');
					}

					if (realNewParent !== newActive.get(0)) {
						// Child to child navigation by internal links, set parent class
						$(realNewParent).addClass('hasActiveChild');
					}

					window.DPI.Apps.DeHaaf.Menu.collapse(realPrevParent);
					window.DPI.Apps.DeHaaf.Menu.expand(realNewParent);
				}
				else
				{
					// Same parent
					$(newParent).addClass('hasActiveChild');
					$(prevActive).removeClass('activeChild');
				}
				
				// Update menu font replacement
				Cufon.refresh('.AvantGardeMedium');
			},

			collapse : function (liEl) {
				if (window.DPI.Apps.DeHaaf.Debug)
				{
					console.log('window.DPI.Apps.DeHaaf.Menu.collapse', liEl);
				}

				$(liEl).removeClass('activeChild');
				$(liEl).removeClass('hasActiveChild');
				
				$(liEl).children('ul').css('display', 'block').stop(true, false).animate({
					height		: 0,
					marginTop	: 0,
					opacity		: 0
				}, {
					duration: 400,
					complete: function() {
						$(this).css({
							display		: '',
							marginTop	: '',
							opacity		: ''
						});
					},
					step: function () {
						window.DPI.Apps.DeHaaf.Menu.Reposition();
					}
				});
			},

			expand : function (liEl) {
				if (window.DPI.Apps.DeHaaf.Debug)
				{
					console.log('window.DPI.Apps.DeHaaf.Menu.expand', liEl);
				}
				
				var childUl = $(liEl).children('ul');

				// Reset CSS to calculate correct height
				childUl.css('height', '');
				//childUl.css('display', 'block');
				var height = childUl.innerHeight();
				childUl.css('height', '0px');
				childUl.css('opacity', '0');

				childUl.stop(true, false);
				childUl.animate({
					height: height,
					opacity: 1
				}, {
					duration: 400,
					complete: function() {
						$(this).css({
							height: '',
							opacity: ''
						});
					},
					step: function () {
						window.DPI.Apps.DeHaaf.Menu.Reposition();
					}
				});				
			},

			Reposition : function ()
			{
				//var totalHeight = $('.leftbar').height();
				//var usedHeight = $('.bottom').outerHeight() + $('.logo').outerHeight();
				//var availableHeight = totalHeight - usedHeight;
				var menuHeight = $('#menu').height();

				$('#menu').css('marginTop', Math.round(-menuHeight / 2));
			},

			find : function (url)
			{
				var link = $("#menu a[href='" + url + "']").get(0);

				if (!link && '/plattegronden/' === url.substring(0, 15))
				{
					// Activate the floor link
					link = $("#menu a[href^='" + url.split('/').splice(0, 3).join('/') + "']").get(0);
				}

				return link;
			},

			updateFont : function (item) {
				Cufon.replace(item, {fontFamily: 'ITC Avant Garde Gothic'});
			},

			onMouseOver : function () {
				window.DPI.Apps.DeHaaf.Menu.updateFont(this);
			},

			onMouseOut : function () {
				setTimeout(window.DPI.Apps.DeHaaf.Menu.updateFont, 20, this);
			}
		},

		Website : {
			homepage : false,
			restore : false,
			
			Show : function () {
				// Cancel delays
				$.doTimeout('website-show');

				window.DPI.JS.Effect.fadeIn('div.leftbar', 300);
				window.DPI.JS.Effect.fadeIn('div.content', 300);
			},

			Hide : function () {
				// Cancel delays
				$.doTimeout('website-show');

				window.DPI.JS.Effect.fadeOut('div.leftbar', 300);
				window.DPI.JS.Effect.fadeOut('div.content', 300);
			},

			DisplayFSImage : function (galleryID, imageID) {
				window.DPI.Widgets.Slideshow.showById(galleryID, imageID);

				window.DPI.Widgets.Slideshow.Start();

				window.DPI.Widgets.Slideshow.Pause();
			}
		},

		Window : {
			onResize : function () {
				if (window.DPI.Apps.DeHaaf.Debug)
				{
					console.log('Window resized');
				}

				//if (0 === $('html').scrollTop()) {
					// Only reposition the content when the user is not scrolling
					// @todo this scrollTop method does not work crossbrowser according to page comments
					//window.DPI.Apps.DeHaaf.Content.Reposition();
				//}

				window.DPI.Apps.DeHaaf.Content.Reposition();
			},

			onScroll : function () {
				//$('.leftbar').css({top : $(document).scrollTop()});
				//$('#background').css({top : $(document).scrollTop()});
			}
		}
	}
};

window.DPI.JS = window.DPI.JS || {};

window.DPI.JS.setWindowTitle = function (newTitle) {
	if (undefined !== window.document.title)
	{
		window.document.title = newTitle;
	}

	// Browser does not support DOM2
	// @todo fallback to getting the title element and setting its content
};

window.DPI.JS.Effect = {
	fadeIn : function (item) {
		item = $(item);
		item.stop(true, false);

		if (item.css('display') === 'none') {
			item.css({opacity: 0, display: 'block'});
		}

		item.animate({opacity: 1}, {
			duration: 300,
			complete: function () {
				$(this).css({opacity: ''})
			}
		});
	},

	fadeOut : function (item, duration) {
		item = $(item);
		item.stop(true, false);

		if (item.css('display') !== 'none')
		{
			item.animate({
					opacity: 0
				}, {
					duration: duration,
					complete: function () {
						this.style.display = 'none';
					}
				}
			);
		}
	}
};

window.DPI.Widgets = {
	AppartmentLayout : {
		Close : function () {
			var points = $('ul.points > li');
			points.removeClass('active');

			var infowindows = $('div.windows > div.window');

			infowindows.each(function () {
				window.DPI.Widgets.AppartmentLayout.Hide(this);
			});

			window.DPI.JS.Effect.fadeOut('div.windows > div.close', 300);
		},

		Hide : function (item) {
			window.DPI.JS.Effect.fadeOut(item, 300);
		},

		Init : function () {
			var points = $('ul.points > li');

			points.click(window.DPI.Widgets.AppartmentLayout.onPointClick);
			points.mouseover(window.DPI.Widgets.AppartmentLayout.onPointOver);
			points.mouseout(window.DPI.Widgets.AppartmentLayout.onPointOut);

			$('div.windows > div.close').click(window.DPI.Widgets.AppartmentLayout.onCloseClick);
		},
		
		Show : function (item) {
			window.DPI.JS.Effect.fadeIn(item, 300);
			
			window.DPI.JS.Effect.fadeIn('div.windows > div.close', 300);
		},

		onCloseClick : function (e) {
			window.DPI.Widgets.AppartmentLayout.Close();
		},

		onPointClick : function (e) {
			$(this).removeClass('active');

			var infowindow = $('div.windows > div[class*="' + this.className + '"]');

			if (infowindow.css('display') === 'block') {
				// Already visible
				$(this).addClass('active');
				return;
			}

			window.DPI.Widgets.AppartmentLayout.Close();

			$(this).css({zIndex : ''});
			$(this).addClass('active');

			$.doTimeout('infowindow-show', 300, function () {
				window.DPI.Widgets.AppartmentLayout.Show(infowindow);
			}, 300);
		},

		onPointOver : function (e) {
			var tooltip = $(this).children('div.tooltip');
			$(this).css({zIndex : 10});

			window.DPI.JS.Effect.fadeIn(tooltip, 200);
		},

		onPointOut : function (e) {
			var tooltip = $(this).children('div.tooltip');
			$(this).css({zIndex : ''});
			
			window.DPI.JS.Effect.fadeOut(tooltip, 200);
		}
	},

	Contact : {
		onSubmit : function (e) {
			var form = $(this);

			var data = form.serialize();

			// Cancel any running
			$.doTimeout('content-retrieve');

			if (null !== window.DPI.Apps.DeHaaf.Content.ajaxRequest)
			{
				// Stopt previous request
				window.DPI.Apps.DeHaaf.Content.ajaxRequest.abort();
				window.DPI.Apps.DeHaaf.Content.ajaxRequest = null;
			}

			window.DPI.Apps.DeHaaf.Content.Hide();

			window.DPI.Apps.DeHaaf.Content.loading = true;
			window.DPI.Apps.DeHaaf.Content.switching = true;
			
			window.DPI.Apps.DeHaaf.Content.ajaxInitTime = new Date();

			window.DPI.Apps.DeHaaf.Content.ajaxRequest = jQuery.ajax({
			  url			: form.attr('action'),
			  data		: data,
			  type		: 'POST',
			  success	: function (data) {
				  window.DPI.Apps.DeHaaf.Content.ajaxRequest = null;
				  window.DPI.Apps.DeHaaf.Content.loading = false;

				  window.DPI.Apps.DeHaaf.Content.handleContent(data);
			  },
			  dataType	: 'json'
			});

			return false;
		}
	},
	
	Slideshow : {
		galleryID	: null,
		imageID		: null,
		initialized	: false,
		restoreWebsite: false,

		hide : function (value) {
			if (window.DPI.Apps.DeHaaf.Debug)
			{
				console.log('window.DPI.Widgets.Slideshow.hide');
			}

			var embedEl = this.getEmbedEl();

			if (null !== embedEl && embedEl.SlideshowHide)
			{
				embedEl.SlideshowHide(value);
			}
		},

		getEmbedEl : function () {
			return document.getElementById('background');
		},

		isLoading : function () {
			var embedEl = this.getEmbedEl();

			if (null !== embedEl && embedEl.SlideshowIsLoading)
			{
				return embedEl.SlideshowIsLoading();
			}

			return false;
		},

		showById : function (galleryID, imageID) {
			if (window.DPI.Apps.DeHaaf.Debug)
			{
				console.log('window.DPI.Widgets.Slideshow.showById', galleryID, imageID);
			}
			
			this.galleryID = galleryID;
			this.imageID = imageID;
			
			var embedEl = this.getEmbedEl();
			var result = false;

			if (null !== embedEl && embedEl.SlideshowShowById)
			{
				result = embedEl.SlideshowShowById(galleryID, imageID);
			}

			if (window.DPI.Apps.DeHaaf.Debug)
			{
				console.log('window.DPI.Widgets.Slideshow.showById result=', result);
			}

			return result;
		},

		Play : function () {
			if (window.DPI.Apps.DeHaaf.Debug)
			{
				console.log('window.DPI.Widgets.Slideshow.Play');
			}

			var embedEl = this.getEmbedEl();

			if (null !== embedEl && embedEl.SlideshowPlay)
			{
				embedEl.SlideshowPlay();
			}
		},

		Pause : function () {
			if (window.DPI.Apps.DeHaaf.Debug)
			{
				console.log('window.DPI.Widgets.Slideshow.Pause');
			}

			var embedEl = this.getEmbedEl();

			if (null !== embedEl && embedEl.SlideshowPause)
			{
				embedEl.SlideshowPause();
			}
		},

		Start : function () {
			if (window.DPI.Apps.DeHaaf.Debug)
			{
				console.log('window.DPI.Widgets.Slideshow.start');
			}

			var embedEl = this.getEmbedEl();

			if (null !== embedEl && embedEl.SlideshowStart)
			{
				embedEl.SlideshowStart();
			}
		},

		Stop : function () {
			if (window.DPI.Apps.DeHaaf.Debug)
			{
				console.log('window.DPI.Widgets.Slideshow.stop');
			}

			var embedEl = this.getEmbedEl();

			if (null !== embedEl && embedEl.SlideshowStop)
			{
				embedEl.SlideshowStop();
			}
		},

		onImageComplete : function () {
			if (window.DPI.Apps.DeHaaf.Debug)
			{
				console.log('window.DPI.Widgets.Slideshow.onImageComplete', window.DPI.Apps.DeHaaf.Content.switching);
			}

			if (window.DPI.Apps.DeHaaf.Content.switching)
			{
				$.doTimeout('content-show', 1500, function () {
					window.DPI.Apps.DeHaaf.Content.Show();
				});
			}

			if (window.DPI.Apps.DeHaaf.Website.restore)
			{
				$.doTimeout('website-show', 1000, function () {
					window.DPI.Apps.DeHaaf.Website.Show();
				});

				window.DPI.Apps.DeHaaf.Website.restore = false;
			}

			if (window.DPI.Apps.DeHaaf.Website.homepage)
			{
				window.DPI.Apps.DeHaaf.Website.homepage = false;

				$.doTimeout('website-show', 1500, function () {
					window.DPI.JS.Effect.fadeIn('div.leftbar', 800);

					$.doTimeout('content-show', 800, function () {
						window.DPI.JS.Effect.fadeIn('div.content', 800);
					});
				});
			}
		},

		onImageError : function () {
			if (window.DPI.Apps.DeHaaf.Debug)
			{
				console.log('window.DPI.Widgets.Slideshow.onImageError', window.DPI.Apps.DeHaaf.Content.switching);
			}

			if (window.DPI.Apps.DeHaaf.Content.switching)
			{
				window.DPI.Apps.DeHaaf.Content.Show();
			}

			if (window.DPI.Apps.DeHaaf.Website.restore)
			{
				window.DPI.Apps.DeHaaf.Website.Show();

				window.DPI.Apps.DeHaaf.Website.restore = false;
			}

			if (window.DPI.Apps.DeHaaf.Website.homepage)
			{
				window.DPI.Apps.DeHaaf.Website.homepage = false;

				window.DPI.Apps.DeHaaf.Website.Show();
			}
		},

		onSlideshowStart : function () {
			if (window.DPI.Apps.DeHaaf.Debug)
			{
				console.log('window.DPI.Widgets.Slideshow.onSlideshowStart');
			}
			
			if ('page' === window.DPI.Apps.DeHaaf.Content.type) {
				window.DPI.Apps.DeHaaf.Website.Hide();
			}
			else
			{
				window.DPI.Apps.DeHaaf.Website.Hide();

				$.doTimeout('slideshow-show', 400, function () {
					window.DPI.Widgets.Slideshow.hide(false);
				});
			}
		},
		
		onSlideshowStop : function () {
			if (window.DPI.Apps.DeHaaf.Debug)
			{
				console.log('window.DPI.Widgets.Slideshow.onSlideshowStop');
			}

			// Cancel any delayed show/hide
			$.doTimeout('slideshow-show');

			if ('page' === window.DPI.Apps.DeHaaf.Content.type) {
				// Restore image
				window.DPI.Widgets.Slideshow.showById(window.DPI.Widgets.Slideshow.galleryID, window.DPI.Widgets.Slideshow.imageID);

				var loading = false;
				var embedEl = this.getEmbedEl();

				if (null !== embedEl && embedEl.SlideshowIsLoading)
				{
					loading = embedEl.SlideshowIsLoading();
				}

				if (!loading)
				{
					window.DPI.Apps.DeHaaf.Website.Show();
				}
				else
				{
					window.DPI.Apps.DeHaaf.Website.restore = true;
				}
			} else {
				window.DPI.Widgets.Slideshow.hide(true);

				$.doTimeout('slideshow-show', 250, function () {
					window.DPI.Apps.DeHaaf.Website.Show();
				});
			}
		},

		onSWFLoaded : function () {
			if (window.DPI.Apps.DeHaaf.Debug)
			{
				console.log('window.DPI.Widgets.Slideshow.onSWFLoaded');
			}

			window.DPI.Widgets.Slideshow.initialized = true;

			if ('page' !== window.DPI.Apps.DeHaaf.Content.type)
			{
				window.DPI.Widgets.Slideshow.hide(true);
			}

			if (null !== this.galleryID && null !== this.imageID)
			{
				this.showById(this.galleryID, this.imageID);
			}
		}
	},

	Video : {
		fullscreen : true,

		Hide : function () {
			/*$('div.player').css('display', 'none');
			
			window.DPI.JS.Effect.fadeOut($('div.video'), 800);

			$.doTimeout('website-show', 800, function () {
				window.DPI.JS.Effect.fadeIn($('div.leftbar'), 400);

				$.doTimeout('website-show', 800, function () {
					$('div.video').detach();
					window.DPI.Apps.DeHaaf.Website.Show();
				});
			});*/
		},

		Init : function () {
			// Hide website
			//$('div.leftbar').css('display', 'none');
			//$('div.content').css('display', 'none');

			// Show video
			//$('div.video').css('display', 'block');
			// Using local player version var because video init may be called before app init
			var flashVersion = swfobject.getFlashPlayerVersion();

			if (0 !== flashVersion.major)
			{
				window.DPI.Widgets.Video.fullscreen = true;

				//swfobject.embedSWF("/video/player.swf", "movie", "710", "430", "9.0.0","expressInstall.swf", {
				swfobject.embedSWF("/video/player.swf", "movie", $(window).width(), $(window).height(), "9.0.0","expressInstall.swf", {
						file: '/video/intro.flv',
						autostart : true,
						bufferlength : 3
					}, {
						allowscriptaccess:'always',
						allowfullscreen: 'true'
					}, {
						name:'movie'
				});
			}
			else
			{
				// Flash is not supported, fallback to a YouTube embed for iPhone/iPads
				// If it is not such a device it will fallback to the non-flash content
				swfobject.embedSWF("http://www.youtube.com/v/sfsDhwv_5yM?version=3", "movie", 710, 400, "0","expressInstall.swf", {}, {
						allowscriptaccess:'always',
						allowfullscreen: 'true'
				}, {});
			}

			/*$('div.video .skip').click(function() {
				window.DPI.Widgets.Video.Hide();
			});*/
		},

		SetFullscreen : function (value) {
			window.DPI.Widgets.Video.fullscreen = value;

			if (value)
			{
				$('#movie').attr('width', $(window).width());
				$('#movie').attr('height', $(window).height());
			}
			else
			{
				$('#movie').attr('width', '710');
				$('#movie').attr('height', '430');
			}

			window.DPI.Apps.DeHaaf.Content.Reposition();
		},

		ToggleFullscreen : function () {
			window.DPI.Widgets.Video.SetFullscreen(!window.DPI.Widgets.Video.fullscreen);
		},

		onVideoEnd : function (obj) {
			/*$.doTimeout('video-show', 400, function () {
				window.DPI.Widgets.Video.Hide();
			});*/
		}
	}
};

