$(document).ready(function(){
  
	var initPage = $(".page").width();
	var currentPage = 0;
	var lastIndex;
	var $paneTarget = $('#wrap');
	
	
	var urls = [ 'bg-massive'];
	$.preload( urls, {
		base:'img/',
		ext:'.jpg',
		onComplete:function( data ){
			var img = new Image();
			img.src = data.image;
			
			$("#page0 img").css('visibility','visible');
			$('.maxImage').maxImage({isBackground: true, verticalAlign: 'top', zIndex: -10});
		},
		onFinish:function(){
			$('#loaded').load('pages.html',{},function(){
                  $("#pages").append($('#loaded .page'));
				  activateLoadedHTML();
            });
			
		}
	});
	
	
	$(window).resize(function() {
		if($(window).width()>initPage) {
  			initSize();
		}
	});
	
	function initSize() {
		$("#wrap").width($(window).width());		
		$(".page").width($(window).width());
		
		$("#page0").height($(window).height());
		$("#pages").width($(window).width()*$('.page').size());
		$("#loaded").width(1600);
		
		if($(window).height()>820) {
			$("#pages").height($(window).height());
			$("#wrap").height($(window).height());
		}
		
		$("#page0 h1").css("margin-top",($(window).height()/2)-100+"px");
		$paneTarget.scrollTo( '#page'+currentPage, 0 );
	}
	
	
	$("#nav").slideUp(0);
	Cufon.replace('h1');
	initSize();
	$.scrollTo(0);
	
	
  	function activateLoadedHTML () {
		
		initSize();
	
		Cufon.replace('h1,h2,h3, .copy p');
		Cufon.replace('#nav li a',{hover : true});	
	
		$("#wrap").prepend('<div class="prev"><a></a></div><div class="next"><a></a></div>');	
			
		
		
		function initNav() {
			$('html').css("overflow-y","auto");
			$("#nav").css("visibility","visible");
			$('#nav').delay(1500).slideDown('slow');
			$('.next').delay(1500).fadeIn();
			$('.prev').delay(1500).fadeIn();
			$('.prev a').css("display","none");
		}
		
	
		$('#nav li a').hover(function(index) {   
			$(this).addClass("hover");
			Cufon.refresh('#nav li a');
			}, function() {
			$(this).removeClass("hover");
			Cufon.refresh('#nav li a');
		});
		
		
		
		$('.next').hover(function(index) {   
				if (currentPage<$('.page').size()-1) {
					$('a',this).fadeIn();
				}
			}, function() {
				$('a',this).fadeOut();
		});
		
		$('.next a').click(function(event) {
			event.preventDefault();
			var href="#page"+(currentPage+1);
			$.bbq.pushState({ url: href });
		});
		
		$('.prev a').click(function(event) {
			event.preventDefault();
			var href="#page"+(currentPage-1);
			$.bbq.pushState({ url: href });
		});
		
		$('.prev').hover(function(index) {   
				if (currentPage>0) {
					$('a',this).fadeIn();
				}
			}, function() {
				$('a',this).fadeOut();
		});
		
		
		
		/*$('#nav h1').click(function(event) {
			event.preventDefault();
			navTo(0);						
		});
	
		$('#nav h1 a').click(function(event) {
			event.preventDefault();
			navTo(0);						
		});*/
	
		navTo = function(index) {
			
			
			if(index==0) {
				$('.next').fadeOut();
				$('.prev').fadeOut();
			}
			
			if(currentPage==0) {
				initNav();
			}
			
			$(lastIndex).removeClass("active");
			
			$("#nav li:eq("+(index-1) +") a").addClass("active");
			lastIndex=$("#nav li:eq("+(index-1) +") a");
			Cufon.refresh('#nav li a');		
			
			$paneTarget.scrollTo( '#page'+index, 750 );	
			
			currentPage=index;
			
		}
		
		/*$(document).keypress(function(event) {
		switch(e.keyCode) { 
			case 37: 
			if (currentPage>0) {
				navTo(currentPage-1);
			}
			break;
			case 39:
			if (currentPage<$('.page').size()-1) {
			   navTo(currentPage+1);
			}
			break;
		}
		});*/
		
		$(window).trigger( "hashchange" );
	
	}
	
	
	
});
