$(document).ready(function() {
	
	var d_w																							= $(document).width();
	var d_h																							= $(window).height();
	
	function resize() {
	
		$('body').css({
			height: d_h*2
		});
	
		$('body>nav').css({
			minHeight: d_h
		});
	
		$('body>section').css({
			minHeight: d_h
		});
	
		$('body>section article').css({
			height: d_h
		});
	
		$('#hello , body>section article section , body>section article section iframe').css({
			width: d_w,
			height: d_h
		});
		
	}
	
	resize();
	
	$(window).resize(function() {
		resize();
	});
	
	jQuery.fn.switchPage = function (page) {
		
		$('section').fadeOut('slow');
		$('section#' + page).show();
		
		var s_p_pos	 																				= $("section#"+page).offset();
		var s_p_pos_top 																			= s_p_pos.top;
		
		$('html, body').animate({
			
			scrollTop: s_p_pos_top
			
		}, 500 , function() {
			
			$('section#' + page + ' article').slideDown('slow' , function() {
				
				window.location.hash 																= page;
				
				var section_count																	= 0;
				var wrap_w																			= 0;
				
				$('section#' + page + ' article section').each(function () {
					
					section_count++;
					section_h																		= $(this).height();
					section_w																		= $(this).width();
					wrap_w 																			= wrap_w + section_w;
					
					section_image_img																= $(this).find('img');
					section_image_margin_top														= ((section_h - section_image_img.height()) / 2);
					section_image_margin_left														= ((section_w - section_image_img.width()) / 2);

					section_image_img.css({
						marginTop: section_image_margin_top,
						marginLeft: section_image_margin_left
					});

					
				});
				
				$('section#' + page + ' article .wrap').css({
					width: wrap_w
				});
				
				if (section_count == 1) {
					
					$('section#' + page + ' article header nav ul li.previous').hide();
					$('section#' + page + ' article header nav ul li.next').hide();
					
				}
				
				$('section#' + page + ' nav ul').fadeIn();
				$('section#' + page + ' article .wrap').fadeIn();
				
			});
			
		});
		
		
	}
	
	$('#hello').live("click", function(event) {
	
		$('#hello').fadeOut('fast');
		
	});
	
	$('body>nav li').live("click", function(event) {
		
		event.preventDefault();
		
		var page																					= $(this).attr('rel');
		
		$(this).animate({
			opacity: '0.25'
		} , function() {
			
			$(this).addClass("visited");
			$().switchPage(page);
			
		});
		
		
	});
	
	$("img.a").hover(function() {
		
		$(this).stop().animate({"opacity": "0"}, "slow");
	
	}, function() {
		
		$(this).stop().animate({"opacity": "1"}, "slow");
		
	});
	
	$('section article header nav li.previous a').live("click", function(event) {
		
		event.preventDefault();
		
		var wrap 																					= $('section:visible .wrap');
		var wrap_pos	 																			= wrap.offset();
		var wrap_pos_left 																			= wrap_pos.left;
		
		wrap.animate({
			left: wrap_pos_left+d_w
		} , function() {
			
			var wrap_pos	 																		= wrap.offset();
			var wrap_pos_left 																		= wrap_pos.left;
			
			if (wrap_pos_left == 0) {
				wrap.parent('article').find('li.previous a').animate({
					left: '-40px'
				});
			}
			
			if (wrap_pos_left > '-' + (wrap.width() - d_w)) {
				wrap.parent('article').find('li.next a').animate({
					left: '0'
				});
			}
			
		});
		
	});
	
	$('section article header nav li.next a').live("click", function(event) {
		
		event.preventDefault();
		
		var wrap 																					= $('section:visible .wrap');
		var wrap_pos	 																			= wrap.offset();
		var wrap_pos_left 																			= wrap_pos.left;
		
		wrap.animate({
			left: wrap_pos_left-d_w
		} , function() {
			
			var wrap_pos	 																		= wrap.offset();
			var wrap_pos_left 																		= wrap_pos.left;
			
			if (wrap_pos_left < 0) {
				wrap.parent('article').find('li.previous a').animate({
					left: '0'
				});
			}
			
			if (wrap_pos_left == '-' + (wrap.width() - d_w)) {
				wrap.parent('article').find('li.next a').animate({
					left: '40px'
				});
			}
			
		});
		
	});
	
		/*
		key('left', function() {
			$('section:visible nav li.previous a').click();
		});

		key('right', function() { 
			console.log($('section:visible nav li.next')); 
			$('section:visible nav li.next a').click();
		});
		*/
	
	$('section article header nav li.close a').live("click", function(event) {
		
		event.preventDefault();
		
		var section_nav																					= $('section:visible nav');
		section_nav.fadeOut();

		$(this).closest('section article').slideUp('slow' , function() {
			
			$('body>nav').hide();
			
			$('html, body').animate({
				scrollTop: 0
			} , function() {
				
				$('body>nav').fadeIn('slow');
				
				window.location.hash 																	= '';
				
			});
			
		});
		
	});
	
	$(window).load(function () {
		
		$('body>nav').fadeIn('slow');
		
		var h 																						= location.href.split('#')[1];
		if (h) {
			
			$().switchPage(h);
			
		} else {
			
			$('#hello').fadeIn('fast');
			
		}
		
	});
	
});
