$(window).load(function () {

	document.onselectstart = function() {return false;}
	
	// fade home, bio, and contact images in
	$("#homeimage img").fadeIn(500);
	$("#bioimage img").fadeIn(500);
	$("#contactimage img").fadeIn(500);
	
	// slide home, bio, and contact images in
	//$("#homeimage img").animate({left:"0px"},1000);
	//$("#bioimage img").animate({left:"0px"},400);
	//$("#contactimage img").animate({left:"0px"},400);

	// determine page name
	var path = location.pathname.split('/');
	var page = path[path.length-1].split('.')[0];
	
	// portfolio drop down
	var timeoutid;
	$("#navportfolio").mouseenter(function() {
		clearTimeout(timeoutid);
		$("#subnav").css("display","");
	}).mouseleave(function() {
		timeoutid = setTimeout(function(){$("#subnav").css("display","none");},600);
	});
	$("#subnav").mouseenter(function() {
		clearTimeout(timeoutid);
		$("#subnav").css("display","");
	}).mouseleave(function() {
		timeoutid = setTimeout(function(){$("#subnav").css("display","none");},600);
	});
	
	// set thumbnail width/height and center in box
	$(".thumb img").each(function(index) {
		h = $(this).height();
		w = $(this).width();
		if (h > w) { $(this).css("height","90px"); }
		else {
			$(this).css("width","90px");
			$(this).css("marginTop",((90-$(this).height())/2)+"px");
		}
	});

	// set width of scrollblock
	var scrollwidth = 100;
	$(".thumb").each(function(index) {
		scrollwidth = scrollwidth + 112;
	});
	$("#scrollblock").css("width",scrollwidth+"px");

	// hide scrollbar if 8 or fewer images
	var images = $(".thumb img").length;
	if (images <= 8) {
		$("#scrollbar").css("display","none");
		$("#portscroll").css("height","100px");
	}

	// set width of scrollbar
	var barwidth = Math.round((996/scrollwidth)*1000);
	$("#bar").css("width",barwidth+"px");

	// determine amount of scrollblock hidden
	var hiddenthumbs = $(".thumb").length - 8;
	var hiddenwidth = scrollwidth - 996;
	var incr = Math.round((1000-barwidth)/hiddenthumbs);
	
	// set first thumbnail outline
	$(".thumb:first").toggleClass("thumbon");

	// toggle thumb outline
	function thumbToggle() {
		$(".thumb:eq("+(currentimg-1)+")").toggleClass("thumbon");
	}

	// display thumb images on successful load of page
	$(".thumb").css("background","#777");
	$(".thumb img").css("display","");
	
	// scrollarea and scrollbar animation
	function thumbScroll() {
		if (currentimg == images) { // selected image is last
			$("#scrollblock").animate({left:-((images-8)*112)},250);
			$("#bar").animate({left:(1000-barwidth)},250);
			temp = barx = 1000 - barwidth;
			currentthumbl = currentscrolll = images-8;
			currentthumbr = currentscrollr = images;
			incrs = hiddenthumbs;
		} else if (currentimg == 1) { // selected image is first
			$("#scrollblock").animate({left:0},250);
			$("#bar").animate({left:0},250);
			temp = barx = 0;
			currentthumbl = currentscrolll = 0;
			currentthumbr = currentscrollr = 8;
			incrs = 0;
		} else if ((currentimg > currentscrollr) && (scrolled)) { // selected image is to the right and user just scrolled
			scrolldiff = currentimg-currentscrollr;
			$("#scrollblock").animate({left:"-="+(scrolldiff*112)},250);
			$("#bar").animate({left:((currentimg-8)*incr)},250);
			temp = barx = (currentimg-8)*incr;
			currentthumbl = currentscrolll = currentimg-8;
			currentthumbr = currentscrollr = currentimg;
			incrs = currentimg-8;
			scrolled = false;
		} else if ((currentimg < currentscrolll) && (scrolled)) { // selected image is to the left and user just scrolled
			scrolldiff = currentscrolll - currentimg;
			$("#scrollblock").animate({left:"+="+(scrolldiff*112)},250);
			$("#bar").animate({left:(currentimg*incr)},250);
			temp = barx = currentimg*incr;
			currentthumbl = currentscrolll = currentimg;
			currentthumbr = currentscrollr = currentimg+8;
			incrs = currentimg;
			scrolled = false;
		} else if (currentimg > currentthumbr) { // selected image is to the right just out of view
			$("#scrollblock").animate({left:"-=112"},250);
			$("#bar").animate({left:((currentimg-8)*incr)},250);
			temp = barx = (currentimg-8)*incr;
			currentthumbl++; currentscrolll++;
			currentthumbr++; currentscrollr++;
			incrs++;
		} else if (currentimg < currentthumbl) { // selected image is to the left just out of view
			$("#scrollblock").animate({left:"+=112"},250);
			$("#bar").animate({left:(currentimg*incr)},250);
			temp = barx = currentimg*incr;
			currentthumbl--; currentscrolll--;
			currentthumbr--; currentscrollr--;
			incrs--;
		}
	}
	
	// previous/next button functionality
	var currentimg = 1;
	var currentthumbl = 0;
	var currentthumbr = 8;
	$("#portright").mousedown(function() {
		thumbToggle();
		if (currentimg == images) { currentimg = 1; }
		else { currentimg++; }
		thumbScroll();
		$("#portimage img").attr("src","images/port"+page+currentimg+".jpg");
		thumbToggle();
	});
	$("#portleft").mousedown(function() {
		thumbToggle();
		if (currentimg == 1) { currentimg = images; }
		else { currentimg--; }
		thumbScroll();
		$("#portimage img").attr("src","images/port"+page+currentimg+".jpg");
		thumbToggle();
	});

	// thumbnail functionality
	$(".thumb").each(function(index) {
		$(this).mousedown(function() {
			thumbToggle();
			currentimg = index+1;
			$("#portimage img").attr("src","images/port"+page+currentimg+".jpg");
			thumbToggle();
			currentthumbl = currentscrolll;
			currentthumbr = currentscrollr;
		});
	});

	// scrollbar functionality
	var x;
	var incrs = 0;
	var barx = 0;
	var temp = 0;
	var movement = 0;
	var end = false;
	var currentscrolll = 0;
	var currentscrollr = 8;
	var scrolled;
	var scrolldiff;
	$("html").mouseup(function() {
		barx = temp;
		$(this).unbind("mousemove");
		$("html").css("cursor","default");
		$("#overlay").css("width","");
		$("#overlay").css("height","");
	});
	$("#bar").mousedown(function(e) {
		scrolled = true;
		incrs = Math.floor(barx/incr);
		x = e.pageX;
		$("#overlay").css("width",$("html").width());
		$("#overlay").css("height",$("html").height());
		$("html").mousemove(function(e) {
			movement = e.pageX-x;
			$(this).css("cursor","ew-resize");
			if ((barx+movement) < 0) { // scrollbar is at left end
				$("#bar").css("left","0px");
				incrs = 0;
				temp = 0;
				if (!end) {
					$("#scrollblock").animate({left:0},250);
					currentscrolll = 0;
					currentscrollr = 8;
					end = true;
				}
			} else if ((barx+movement) > (1000-barwidth)) { // scrollbar is at right end
				$("#bar").css("left",(1000-barwidth)+"px");
				incrs = hiddenthumbs;
				temp = 1000 - barwidth;
				if (!end) {
					$("#scrollblock").animate({left:-((images-8)*112)},250);
					currentscrolll = images-8;
					currentscrollr = images;
					end = true;
				}
			} else { // scrollbar is somewhere in middle
				$("#bar").css("left",(barx+movement)+"px");
				temp = barx + movement;
				if (Math.floor(temp/incr) > incrs) { // scrollbar has moved at least one increment to the right
					$("#scrollblock").animate({left:"-=112"},250);
					currentscrolll++;
					currentscrollr++;
					incrs++;
				} else if (Math.floor(temp/incr) < incrs) { // scrollbar has moved at least one increment to the left
					if (Math.floor(temp/incr) == 0 ) {
						$("#scrollblock").animate({left:0},250);
					} else {
						$("#scrollblock").animate({left:"+=112"},250);
					}
					currentscrolll--;
					currentscrollr--;
					incrs--;
				}
				end = false;
			}
		});
	});
	
});

