(function(){
	
	// IE 7 and below go home and walk the dog
	if (Browser.Engine.trident && Browser.Engine.version <= 5) return false;
	
	window.addEvent('domready',function(){
		
		Globals = {
			'winSize' : ''
		};
		
		// set jsEnabled flag
		
		$(document.body).addClass('jsEnabled');
		
		// get/create elements
		
		var stage = new Element('div',{
			'id' : 'stage',
			'html' : '<div id="mask"></div><div id="moon"></div>'
		}).inject(document.body);
		
		var pages = $$('.page');
		var pointer = new Element('span',{ 'class' : 'pointer' });
		var controls = new Element('div',{
			'class' : 'controls',
			'html' : '<a>close</a>'
		});
		
		// prepare stage

		function setStageSize() {
			Globals.winSize = window.getSize();
			$('stage').setStyles({
				'width' : Globals.winSize.x,
				'height' : Globals.winSize.y
			});
		};

		setStageSize();
		window.addEvent('resize',function(){ setStageSize(); });
		
		// prepare pages
		
		$each(pages,function(item){
			
			item.set('slide',{
				'duration' : 'short',
				'transition': 'sine:out'
			});
			
			controls.clone().inject(item);
			
			item.slide('hide');
			
			item.getElement('.controls a').addEvent('click',function(){
				item.slide('out');
			});
			
		});
		
		// prepare mainMenu
		
		$each($$('#mainMenu a'),function(item){
			
			if (item.get('class') != 'lnkExternal') {
			
				pointer.clone().inject(item);
			
				item.addEvent('click',function(e){
					e.stop();
					var targetPageName = item.get('href').split('#')[1];
					var targetPage = $(targetPageName);
					targetPage.slide('toggle');
					hideOtherPages(targetPage);
					$('popContact').fade('out');
					window.location = '#' + targetPageName;
				});
			
			}
			
		});
		
		function hideOtherPages(exception) {
			$each(pages,function(item){
				if (item != exception) item.slide('out');
			});
		}
		
		// prepare internal links
		
		$each($$('a.lnkInternal'),function(item){
			item.addEvent('click',function(e){
				e.stop();
				var targetPageName = item.get('href').split('#')[1];
				var targetPage = $(targetPageName);
				targetPage.slide('toggle');
				hideOtherPages(targetPage);
				window.location = '#' + targetPageName;
			});
		});
		
		// popOver
		
		var popContact = $('popContact');
		
		popContact.fade('hide');
		controls.clone().inject(popContact);
		popContact.getElement('.controls a').addEvent('click',function(e){
			e.stop();
			popContact.fade('out');
		});
		
		$('lnkContact').addEvent('click',function(e){
			e.stop();
			window.location = '#contact';
			hideOtherPages();
			$('popContact').fade('toggle');
		});
		
	});
	
	window.addEvent('load',function(){
		
		// deeplinks
		
		try {
			
			var targetAnchor = window.location.href.split('#')[1];
			
			if (targetAnchor === 'contact') {
				$('popContact').fade('in');
			} else {
				$(targetAnchor).slide('in');
			}
			
		} catch(e) { /* fail silently */ }
		
	});
	
	window.addEvent('mousemove',function(e){
		changeMoonPos(e);
	});
	
	// logo animation
	
	function changeMoonPos(e) {

		var cursor = {
			x : e.client.x,
			y : e.client.y
		};
		
		$('moon').setStyle('backgroundPosition' , (cursor.y - Math.floor(Globals.winSize.y / 2 * 1.5)) + 'px ' + (cursor.x - Math.floor(Globals.winSize.x / 2 * 1.5)) + 'px');

	}
	
})();
