/****************************************************
 * Copyright (c) Legwork Studio. All Rights Reserved.
 * Updated by: Wigz + Jos, 10-Oct-2011
 *
 * Now let's get serious.
 ****************************************************
/                                                  */

var Athenos = Athenos || {};

(function($, Modernizr){
	var self = this;

	self.Intro = (function(){
		var pub = {},
			$wn,
			$intro,
			$clouds,
			$motions,
			offset = 0,
			done = false,
			
			INT_WIDTH = 900,
			MAX_WIDTH = 1450;
			
		/****************************************************
		 * init:void
		 * 
		 * Not much to say here, how's the weather?
		 ****************************************************
		/                                                  */
		pub.init = function(){
			$wn = $(window);
			$intro = $('#intro-scene');
			$clouds = $('#intro-clouds');
			$motions = $('#motions');
			
			_initIntro();
		};
		
		/****************************************************
		 * _initIntro:void
		 * 
		 * Initialize the site intro.
		 ****************************************************
		/                                                  */
		function _initIntro() {
			var w = $wn.width() > MAX_WIDTH ? MAX_WIDTH : $wn.width(),
				m = Math.ceil(w / 2),
				t = w === MAX_WIDTH ? 0 : -((MAX_WIDTH - w) / 2);
						
			$('body')
				.bind({
					'introLoaded': function(){
						$('#wrapper').css({'width':w + 'px', 'margin-left':-m + 'px'});
						$motions.css('left', '-400%');
						_start();
					},
					'initialsLoaded': function(){
						Athenos.Actions.initDeux();
						done = true;
						offset = $('#home-btn').attr('rel');
						$wn.scrollLeft(offset);
						$('#home-stubs-item').css('margin-left', t + 'px');
					}
				})
				.image_eventer({
						collections: [
						{ 	files: ['lib/imgs/load-cloud.png', 'lib/imgs/home-logo.png'], 
							completedEvent: 'introLoaded' },
						{ 	files: ['lib/imgs/social_backlayer.jpg', 'lib/imgs/home-table.png', 'lib/imgs/nav-sprite.png', 'lib/imgs/bg-flyout-sprite.png', 'lib/imgs/bg-flyout-sprite.gif'], 
							completedEvent: 'initialsLoaded' }
					]
				});
		}
		
		function _start() {
				
			// clouds
			(function _cloudLoop() {
				$clouds.css({'top':'600px'}).animate({'top':'-3000px'}, 4000, 'linear', function() {
					if(!done) {
						_cloudLoop(); // recurse
					} else {
						$(this).remove();
						_continue();
					}
				});	
			})();
			
		}
		
		function _continue() {
			// logo
			$('#intro-logo').animate({'top':'-200px'}, 1000, 'easeInQuart', function() {
				$(this).remove();
			});
			
			// bg
			setTimeout(function() {
				$intro.animate({'top':'-500px'}, 3000, 'easeOutExpo', function() {
					$('#intro-wrapper').fadeOut('slow', function() {
						$(this).remove();
						_showtime();
					});
				});
			}, 900);
		}
		
		function _showtime() {
			$('#rsl').animate({'margin-left':'-1050px'}, 1000, 'easeInOutExpo', function() {
				$(this).remove();
			});
			
			$('#rsr').animate({'margin-left':'740px'}, 1000, 'easeInOutExpo', function() {
				$(this).remove();
				if(!$('html').hasClass('ie7') || !$('html').hasClass('ie8')){
					$('#main-nav li').css('opacity', '1');
				}
				$motions.attr('style', '');
				Athenos.Actions.introFinishedBinds();
			});
			
			$('<div id="scroll-tooltip" />')
			.css('left', (($wn.width() * (offset / $('body').outerWidth())) - 125) + 'px')
			.hide()
			.appendTo('#athenos')
			.fadeIn('fast')
			.delay(5000)
			.fadeOut('fast', function() {
				$(this).remove();
			});
		}
		
		return pub;
	})();

	self.Construct = (function(){
		$(document).ready(function(){
			if($('html').hasClass('ie6')) return;
			Athenos.Intro.init();
		});
	})();
	
}).call(Athenos, jQuery, Modernizr);
