curImage = 0;
hideAll = 0;

function nextImage() {
	curImage++;
	if(curImage>$(".gallery").length-1) curImage = 0;

	return $(".gallery").eq(curImage).attr("href");
}

function prevImage() {
	curImage--;
	if(curImage<0) curImage = $(".gallery").length-1;

	return $(".gallery").eq(curImage).attr("href");
}

$(document).ready(function() {
	
	$(".column ul.itemList li div").each(function() { $(this).css("top","100%"); });
	
	$(".column ul.itemList li").each(
			function() { 
				$(this).hover(
					function() {
						$('div',this).animate({ top: 0 },500, function() {});
					},
					function() {
						$('div',this).animate({ top: '100%' },500, function() {});
					}
				); 
			});
	
	$("#contact img").hover(
		function() {
			$("#content").animate({ opacity: 0 }, 0, function() { $("#contact img").attr("src","/wp-content/themes/icecream2011/assets/van_hide.gif"); } );
		},
		function() {
			if(hideAll==0) $("#content").animate({ opacity: 1 }, 500, function() { $("#contact img").attr("src","/wp-content/themes/icecream2011/assets/van.gif"); } );
		}
	);
	
	$("#contact img").click(
			function() {
				if(hideAll==0) { $("#content").animate({ opacity: 0 }, 0, function() {} ); hideAll = 1; }
				else { $("#content").animate({ opacity: 1 }, 500, function() {} ); hideAll = 0; }
			}
		);
	
	$(".photoList .gallery").click(function(e) {
	    e.preventDefault();

	    $(".viewPhoto img").attr("src",$(this).attr("href"));
	    $(".viewPhoto #zoom a").attr("href",$(this).attr("href"));
	    
	});
	
	($("#controls #prev")).click(function() {
		newURL = prevImage();
		$(".viewPhoto img").attr("src",newURL);
		$(".viewPhoto #zoom a").attr("href",newURL);
		
	});
	
	($("#controls #next")).click(function() {
		newURL = nextImage();
		$(".viewPhoto img").attr("src",newURL);
		$(".viewPhoto #zoom a").attr("href",newURL);
	});
	
	$(window).scroll(function() {
		//alert("hi");
		docHeight = $(document).height()-$(window).height();
		curScroll = $(document).scrollTop();
		
		percentageScrolled = curScroll/docHeight;
		
		newTop = -($("#background img").height()-$(window).height())*percentageScrolled;
		
		console.log("Height: " + docHeight + "  Scrolled: " + curScroll + "  %:" + percentageScrolled + "  top: " + newTop);
		
		$("#background img").css("top",newTop + "px");
	});
	
	
});
