
	$(document).ready(function() {
		
		jQuery.easing.def = "easeInOutQuart";

// LOAD OPTIONS
// -------------------------------------------------------------------------------------------------------------------

		var use_cufon = true;
		var log_to_console = false;

// -------------------------------------------------------------------------------------------------------------------



// DEFINE DOM ELEMENTS
// -------------------------------------------------------------------------------------------------------------------

		var body      = $('body');
		var container = $('#container');
		var header = $('header');
		var nav    = $('nav');
		var main   = $('section#main');
		var footer = $('footer');
		var loader = $('#loader');

// -------------------------------------------------------------------------------------------------------------------



// GET MAIN DIMENSIONS
// -------------------------------------------------------------------------------------------------------------------

		var main_height = main.height();
		var main_margin_top = parseInt(main.css('margin-top').replace(/px/,''));

// -------------------------------------------------------------------------------------------------------------------



// CREATE LOADER ELEMENT
// -------------------------------------------------------------------------------------------------------------------
		
		loader.activity({ segments: 14, width: 1, space: 6, length: 10, color: '#999', speed: 0.9});

// -------------------------------------------------------------------------------------------------------------------



// LOAD CUFON
// -------------------------------------------------------------------------------------------------------------------
		
		if(use_cufon && !$.browser.msie ) {   // Museo on cufon looks awful in IE
			
			if(log_to_console) console.log('Loading Cufon...');
			
			$.getScript('js/cufon-yui.js', function() {
				$.getScript('js/Museo_300-Museo_400.font.js', function() {
				  Cufon.replace('body');
				  
				  if(log_to_console) console.log('Cufon Loaded!');
				  
				  loader.fadeOut(1000, function() {
						container.animate({'opacity':1}, 2000);
					});
				  
				});
			});
		} else {
			loader.fadeOut(1000, function() {
				container.animate({'opacity':1}, 2000);
			});
		}

// -------------------------------------------------------------------------------------------------------------------



// NAVIGATION
// -------------------------------------------------------------------------------------------------------------------
		
		
		// Home button/link
		$('a.go_home').click(function(e) {
			
			if(log_to_console) console.log('Home link clicked');
			
			e.preventDefault();
			$('nav ul li a').each(function() { $(this).removeClass('current'); });
			setTimeout(function() {
				$('div.home_content').fadeIn(2000);
			}, 600);
			move_in();
		});
		
		
		// Main navigation links
		$('nav ul li a').each(function() {
			
			var a = $(this);			
			a.click(function(e) {
				e.preventDefault();	
				if(log_to_console) console.log('Menu item clicked');
				
				$('nav ul li a').each(function() { $(this).removeClass('current'); });
				position_underline(a, 800, 500);
				a.addClass('current');
				
				if(is_home()) {
					$('div.home_content').fadeOut(1000, function() {
						set_main_position();
						position_loader(main, true);
					});
					move_out(100);
				}
				
			});
		
		});

		// Append underline to nav
		$('nav ul').append($('<li class="underline"></li>'));
		
// -------------------------------------------------------------------------------------------------------------------



// Run pageload functions
// -------------------------------------------------------------------------------------------------------------------
		
		if(is_home()) {
			position_header();
			position_footer();
			position_nav();
		} else {
			position_underline($('nav ul li a.current'), 800, 500);
			position_footer_line(0, 0, 1000);
		}

// -------------------------------------------------------------------------------------------------------------------
				
		
// Run window.resize functions
// -------------------------------------------------------------------------------------------------------------------

		$(window).resize(function() {
			if(is_home()) {
				position_header();
				position_footer();
				position_nav();
			}
	
			position_loader(main, false);
			
		});
		
// -------------------------------------------------------------------------------------------------------------------
		
		function position_header() {			
				
			if(log_to_console) console.log('Positioning header');
			
			if(header.hasClass('state_in')) {
				
				// Get main offset
				var main_offset = main.offset();
				
				// Position header if home
				if(header.hasClass('home')) {
					header.css({
						/* 'margin' : 0, */
						'top' : (main_offset.top)
						/* 'left' : (main_offset.left + 40) */
					});
				}
				
			}
			
		}
		
		function position_footer() {			
				
			if(log_to_console) console.log('Positioning footer');
			
			if(footer.hasClass('state_in')) {
			
				// Get main offset
				var main_offset = main.offset();
				
				// Position footer if home
				if(footer.hasClass('home')) {
					footer.css({
						/* 'margin' : 0, */
						'bottom' : ($(window).height() - ((main_offset.top + main_height + 40) - 30))
						/* 'left' : (main_offset.left) */
					});
				}
				
			}
				
		}
		
		function position_nav() {			
		
			if(log_to_console) console.log('Positioning nav');
			
			if(nav.hasClass('state_in')) {
				
				// Get header offset
				var header_offset = header.offset();
				
				// Position nav if home
				if(nav.hasClass('home')) {
					nav.css({
						'top' : (header_offset.top + 113 + 40)
					});
				}
				
			}
							
		}
		
		
		
/*
		$('a.animate_out').click(function(e) {
			e.preventDefault();
			
			//main.animate({'opacity':0}, 1000);
			$('div.home_content').fadeOut(1000, function() { set_main_position(); });
			
			setTimeout(function() {
				move_out();
			}, 300);
			
		});
*/
		

		
		
		function move_out(timeout) {
				
			if(log_to_console) console.log('Move elements out');
			
			setTimeout(function() {
			
				body.removeClass('home');
				header.removeClass('state_in');
				nav.removeClass('state_in');
				footer.removeClass('state_in');
				
				header.addClass('state_out');
				nav.addClass('state_out');
				footer.addClass('state_out');
				
				header.animate({
					'top' : 40
				}, 800);
				
				setTimeout(function() {
					nav.animate({
						'top' : 193
					}, 700);
				}, 100);
				
				setTimeout(function() {
					footer.animate({
						'bottom' : 40
					}, 600);
				}, 200);
				
			}, timeout);
			
			position_footer_line(0, 0, 1000);
			
		}
		
		
		function move_in() {
			
			if(log_to_console) console.log('Move elements in');
			
			main.css('margin-top', main_margin_top);
		
			body.addClass('home');
			header.addClass('state_in');
			nav.addClass('state_in');
			footer.addClass('state_in');
			
			header.removeClass('state_out');
			nav.removeClass('state_out');
			footer.removeClass('state_out');
		
			// Get main offset
			var main_offset = main.offset();
			
			footer.animate({
					'bottom' : ($(window).height() - ((main_offset.top + main_height + 40) - 30))
				}, 800);
			
			
			setTimeout(function() {
				nav.animate({
					'top' : ((main_offset.top) + 113 + 40)
				}, 700);
			}, 100);
			
			setTimeout(function() {
				header.animate({
				'top' : (main_offset.top)
				}, 600);
			}, 200);
				
				
			setTimeout(function() {
				$('li.underline').animate({
					'width': nav.width(),
					'left' : 0
				}, 1000);
			}, 800);
			
			loader.fadeOut();
			
			position_footer_line(470, 800, 1000);

		}
		
		function set_main_position() {
			// Set main content position
			var window_height = $(window).height();			
			var top_offset = 219;
			var bottom_offset = 70;
			var content_space = window_height - (top_offset + bottom_offset);
			var content_mid_point = content_space / 2;
			var window_mid_point = window_height / 2;
			var main_top_point = (content_mid_point - window_mid_point) + 50;			
			main.css({'margin-top':main_top_point});
		}
		
		
		function position_underline(elem, timeout, speed) {			
			
			var a = {
				elem : elem,
				width : elem.width(),
				offset : elem.position(),
				left : elem.position().left
			}
			
			if(is_home()) {
					setTimeout(function() {
						$('li.underline').animate({
							'width': (a.width + 40),
							'left' : (a.left + 5)
						}, speed);
					}, timeout);
				} else {
					$('li.underline').animate({
						'width': (a.width + 40),
						'left' : (a.left + 5)
					}, speed);
					
				}		
		}
		
		function position_loader(offset_elem, fade_in) {
			var elem_offset = offset_elem.offset();			
			loader.css({ 'top' : (offset_elem.height() / 2) + elem_offset.top });			
			if(fade_in) loader.fadeIn();				
		}
		
		function position_footer_line(width, timeout, speed) {			
			
			if(is_home()) {
					setTimeout(function() {
						$('footer div.line').animate({
							'width': (width),
							'margin-left' : (width / 2) - ((width / 2) * 2)
						}, speed);
					}, timeout);
				} else {
					$('footer div.line').animate({
							'width': (width),
							'margin-left' : (width / 2) - ((width / 2) * 2)
					}, speed);
				}		
		}
		
		function is_home() {
			return body.hasClass('home');
		}
		

	});
	

