// INTRO ANIMATION

var intro = {
	
	cookieSet: false,
	image_count: 0,
	fallback: 60,
	
	init: function()
	{
		intro.image_count = $('#intro_images').children().length;
		intro.checkLoadStatus();
	},
	
	checkLoadStatus: function()
	{
		var images_to_load = intro.image_count;
		
		$('#intro_images').children().each(function(){
			if( $(this)[0].complete )
			{
				images_to_load--;
			}
			else
			{
			}
		});
		
		intro.fallback--;
		
		if( intro.fallback < 1 || images_to_load < 1 )
		{
			
			intro.checkCookie();

			if( intro.cookieSet )
			{
				intro.showContent();
			}
			else
			{
				setTimeout(intro.animateIntro, 1500);
			}
		}
		else
		{
			setTimeout(intro.checkLoadStatus, 100);
		}
	},
	
	checkCookie: function()
	{
		if( $.cookie('visited_site') == "yes" )
		{
			intro.cookieSet = true;
		}
		else
		{
			$.cookie('visited_site', "yes");
		}
	},
	
	showContent: function()
	{
		$('#intro_images').children().last().fadeIn(500, function(){
			$('#page .content_wrap').fadeIn(1500, function(){
				$('.scrolling_area').jScrollPane({
					scrollbarWidth: 5
				});
			});
		});
	},
	
	animateIntro: function()
	{
		var _bg_images = $('#intro_images').children();
		
		_bg_images.eq(0).fadeIn(500, function(){
			_bg_images.eq(1).delay(2000).fadeIn(1500, function(){
				_bg_images.eq(2).delay(2000).fadeIn(1500, function(){
					intro.menuOpen();
				});
			});
		});
	},
	
	menuOpen: function() {
		var $menu        = $('#site_menu_dropdown'),
			$button      = $('#site_menu'),
			buttonOffset = $button.offset(),
			openHeight   = 0;
		
		$button.removeClass('home_closed').addClass('home_open');
		
		// show and position the menu #site_menu
		$menu.css({
			display:'block',
			position: 'absolute',
			top: buttonOffset.top + $button.height() + 2,
			left: buttonOffset.left + 1,
			overflow: 'visible'
		});
		
		// record menu height
		openHeight = $menu.height();
		
		// animate the menu open
		$menu.css({height: 0}).animate({height: openHeight}, 500);
		
		// FadeIn Overlay
		$('#modal_overlay').fadeTo(500,0.5);
		
		setTimeout(intro.menuClose, 1500);
	},
	
	menuClose: function() {
		
		var $menu        = $('#site_menu_dropdown'),
			$button      = $('#site_menu');
			
		$button.removeClass('home_open').addClass('home_closed');
		$menu.css({overflow: 'hidden'}).animate({height: 0}, 500);
		$('#modal_overlay').fadeOut(500);
		
		setTimeout(intro.contentFadeIn, 500);
	},
	
	contentFadeIn: function()
	{
		$('#page .content_wrap').fadeIn(1500, function(){
			$('.scrolling_area').jScrollPane({
				scrollbarWidth: 5
			});
		});
	}
};


$(function(){
	
	intro.init();
	
});

