(function ($, win) {
	
	win.Page = {
		
		/**
		 * Animation duration
		 */
		duration: 750,
		
		/**
		 * Animation easing function
		 */
		easing: 'easeInOutCubic',
		
		/**
		 * head height
		 * @type {Number}
		 */
		head_height: 1100,
		
		/**
		 * Content height
		 * @type {Number}
		 */
		content_height: 0,
		
		/**
		 * Page loading state, when page is opened
		 * loading is true, after animations it becomes false
		 * @type {Boolean}
		 */
		loading: true,
		
		loop: false,
		
		setheadHeight: function (head_height, instant, offset) {
			if (head_height == this.head_height) return;
			var offset = offset || 0;
			
			this.head_height = head_height;
			this.updateBackground(instant);
			
			$('#backgrounds>div.backgrounds-inner>div').animate({
				'top': offset + 'px'
			}, this.duration, this.easing);
		},
		
		setContentHeight: function (content_height, instant) {
			if (content_height == this.content_height) return;
			this.content_height = content_height;
			this.updateBackground(instant);
		},
		
		updateBackgroundAnimate: function (height) {
			$('#backgrounds').stop().animate({'height': height + 'px'}, this.duration, this.easing);
		},
		
		updateBackground: function (instant) {
			var height = this.head_height + this.content_height;
			
			if (instant) {
				$('#backgrounds').stop().css({'height': height + 'px'});
			} else {
				//Delay animation to prevent user from seeing background at the bottom
				//of the page
				var old_height = parseInt($('#backgrounds').css('height'));
				if (old_height < height) {
					setTimeout($.proxy(function () {
						this.updateBackgroundAnimate(height);
					}, this), 50);
				} else {
					this.updateBackgroundAnimate(height);
				}
			}
		},
		
		openHomePage: function () {
			if (this.loop) return; this.loop = true;
			
			if (!this.loading) {
				Illustrations.hide();
				SlideShow.show();
			}
			
			SlideShow.button.addClass('hidden');
			Illustrations.button.removeClass('hidden');
			Inner.button.addClass('hidden');
			
			PageHistory.push('');
			
			this.loop = false;
		},
		
		openInner: function () {
			if (this.loop) return; this.loop = true;
			
			SlideShow.hide();
			Illustrations.hide();
			Inner.show();
			
			SlideShow.button.addClass('hidden');
			Illustrations.button.addClass('hidden');
			Inner.button.removeClass('hidden');
			
			this.loop = false;
		},
		
		openIllustrations: function () {
			if (this.loop) return; this.loop = true;
			
			SlideShow.hide();
			Illustrations.show();
			
			SlideShow.button.removeClass('hidden');
			Illustrations.button.addClass('hidden');
			Inner.button.addClass('hidden');
			
			PageHistory.push('showcase');
			
			this.loop = false;
		},
		
		onload: function () {
			setTimeout($.proxy(function () {
				this.initAfterLoad();
			}, this), 0);
		},
		
		init: function () {
			Page.onload();
			
			SlideShow.init();
			Illustrations.init();
			Inner.init();
			
			//Get content height
			//Page.setContentHeight($('#content').height(), true);
			Page.content_height = $('#content').height();
			
			//Showcase
			PageHistory.addState('showcase', Page.openIllustrations, Page);
			
			//All which are not found open as page
			PageHistory.addState('*', Inner.openPage, Inner);
		},
		
		initAfterLoad: function () {
			PageHistory.init();
			this.loading = false;
		}
	};
	
	$($.proxy(Page.init));
	
	/*
	 * head language selector
	 */
	$(function () {
		var block = $('#languageSelector');
		if (block.size()) {
			var opened = false;
			
			block.find('a.current-language').click(function () {
				block.addClass('open');
				opened = true;
				return false;
			});
			$(document).click(function () {
				if (opened) {
					block.removeClass('open');
					opened = false;
				}
			})
			
		}
	});
	
})(jQuery, window);

