	$(document).ready(function(){
	
	$('a[rel*=external]').click(function(){ this.target = "_blank"; }); // make rel="external" links open new tab/window

	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
		iphone = true;
	} else { iphone = false;}
	
	$.scrollTo(0);	
	
	if (iphone === false){
		alignImages();	
	}
	
	if (iphone === false){
		$("#img-container .img").click(function(){
			nextImg();
		});
		
		bindNavKeys();
		
		$(window).resize(function(e){
			alignImages();
		});	
		
		navNoteOrigOpac = $(".nav-note").css("opacity");
		$(".nav-note").hover(
			function () {
				$(this).animate({ opacity: ".99999" }, 400);
			}, 
			function(){
				$(this).animate({ opacity: navNoteOrigOpac }, 400);
			}
		);
		
		$(".nav-note").show();
		
	}


});

bindNavKeys = function(){
	$(document).keydown(function(event){
			switch (event.keyCode) {
	            case 32: //space
					event.preventDefault()  ;                
	                nextImg();
	                break;
	            case 37: //left
					event.preventDefault()  ;                
	                prevImg();
	                break;
	            case 38: //up
					event.preventDefault()  ; 
	                prevImg();            
	                break;
	            case 39: //right
					event.preventDefault()  ;                
	                nextImg();
	                break;
	            case 40: //down
					event.preventDefault()  ;             
	                nextImg();
	                break;               
	            case 74: //j 
					event.preventDefault()  ;                
	                prevImg();
	                break;
	            case 75: //j 
					event.preventDefault()  ;                
	                nextImg();
	                break;	                	            
	        }
	
		});

};

alignImages = function(){
	viewportHeight = $(window).height();
	$("#img-container .img").each(function(){
		neededPadding = (viewportHeight - $(this).height())/2;
		if (neededPadding < 1) { neededPadding = 0;}
		neededTop = neededPadding;
		neededBottom = neededPadding;		
		if (neededBottom < 20){neededBottom = 20;} //to stop images from touching when browser is shorter than image.
		$(this).css("padding-top",neededTop+"px");
		$(this).css("padding-bottom",neededBottom+"px");	
	});
	if ($("#img-container").data("currentImg")){
		$.scrollTo($("#img-container .img").eq($("#img-container").data("currentImg")));
	}
};

$.fn.reverse = [].reverse; 

nextImg = function(){
	viewportTop = $(window).scrollTop();
	viewportBot = $(window).scrollTop() + $(window).height();
	$("#img-container .img").each(function(i){
		if( viewportTop < $(this).offset().top-1 ){
			$.scrollTo($(this), 250, {easing: 'easeInOutCubic', axis: "y"});
			$("#img-container").data("currentImg", i);
			return false;
		}
	});
};	


prevImg = function(){
	viewportTop = $(window).scrollTop();
	viewportBot = $(window).scrollTop() + $(window).height();
	$("#img-container .img").reverse().each(function(i){
		if( viewportTop > ($(this).offset().top)){ 
			$.scrollTo($(this), 250, {easing: 'easeInOutCubic', axis: "y"});
			$("#img-container").data("currentImg", $("#img-container .img").size()- i-1);
			return false;
		}
	});
};

