$(document).ready(function(){	
	var selected = 0;
	var current_page = $("#current_page").val();
	$(".nav_item").each(function(){
		if ($(this).hasClass("active")) {
			$(this).removeClass("active");
		}
		if ($(this).attr("title") == current_page) {
			$(this).addClass("active");
			var position = $(this).position();
			var width = $(this).width();
			$(".selected_nav").css("left",position.left);
			$(".selected_nav").css("width",width+"px");
			selected=1;
			$(".selected_nav").show();
		}
	});		
	if (selected == 0) {
		$(".selected_nav").hide();
	}
	
	$(".thumbnail").each(function(){			
		$(this).mouseover(function(){				
			// remove current active thumbnail		
			$(".thumbnail").each(function(){
				if ($(this).hasClass("active_thumb")) {
					$(this).removeClass("active_thumb");
				}
			});					
			// load active thumbnail style
			$(this).addClass("active_thumb");
			
			// load data, img and caption
			$("#gallery_caption div").html("<p>"+$(this).children("span").html()+"</p>");
			$("#gallery_img_container img").hide();
			
			// show new gallery image and caption
			var id = $(this).attr("id");
			$("#"+id).show();
			
		}).click(function(){
			location.href = "http://sewattitude.com/gallery/"+$(this).attr("id")+".html";
		});		
	});	
	
	$("#thumbnail_scroller_up").click(function(){
		$(this).unbind("click");
		scrollGallery('up');
	});
	$("#thumbnail_scroller_down").click(function(){
		$(this).unbind("click");
		scrollGallery('down');
	});
	
});

function scrollGallery(direction) {
	
	var wrapper = parseInt($("#thumbnail_wrapper").css("top"));
	var items = parseInt($(".thumbnail").size());
	var num_items = (items-4)*128;
	
	if (direction == "down") {
		var move_down = wrapper-128;		
		if ((move_down*-1) <= num_items) { 
			$("#thumbnail_wrapper").animate({top:move_down+'px'},{duration:100,easing:'linear',complete:function(){				
				$("#thumbnail_scroller_down").click(function(){
					$("#thumbnail_scroller_down").unbind("click");
					scrollGallery('down');
				});				
			}});
		} else {
			// already at the bottom of the thumbnail wrapper, bind the scroll click
			$("#thumbnail_scroller_down").click(function(){
				$("#thumbnail_scroller_down").unbind("click");
				scrollGallery('down');
			});	
		}		
	} else if (direction == "up") {
		var move_up = wrapper+128;
		if ((wrapper*-1) > 0) {		
			$("#thumbnail_wrapper").animate({top:move_up+'px'},{duration:100,easing:'linear',complete:function(){				
				$("#thumbnail_scroller_up").click(function(){
					$("#thumbnail_scroller_up").unbind("click");
					scrollGallery('up');
				});				
			}});
		} else {
			// already at the top of the thumbnail wrapper, bind the scroll click
			$("#thumbnail_scroller_up").click(function(){
				$("#thumbnail_scroller_up").unbind("click");
				scrollGallery('up');
			});
		}		
	}			
}
