$(document).ready(function(){
	
	// Gallery Thumbs
	
	var working = false;
	
	$("ul#gallery_thumbs a").click(function(){
		if( working ){
			return false;
		}
		
		working = true;
		
		caption = $(this).children("img").attr("alt");
		
		$("div#gallery_photo").children("img").before('<img src="'+$(this).attr("href")+'" alt="" />');
		
		if( $("div#gallery_caption").is(":visible") ){
			$("div#gallery_caption").fadeOut(100);
		}
		
			if( caption != "" ){
				setTimeout( function(){
					$("div#gallery_caption").html('<p>'+caption+'</p>').fadeIn(100);
				}, 100);
			}
		
		$("div#gallery_photo").children("img:last").fadeOut(300,function(){
			$(this).remove();
			working = false;
		});
		
		return false;
	});
	
	// Gallery Nav
	
	if( $("ul#gallery_thumbs li").length <= 3 ){
		$("div#gallery_nav a").addClass("disabled");
	}
	
	var scrolling = false;
	
	$("div#gallery_nav a#gallery_next").click(function(){
		if( $("ul#gallery_thumbs li").length > 3 ){
			
			if( scrolling ){
				return false;
			}
			
			scrolling = true;
			
			$("ul#gallery_thumbs").animate({left: "-120px"}, 300, "swing", function(){
				$(this).children("li:first").appendTo($(this));
				$(this).css("left","0px")
				
				scrolling = false
			});
			
		}
	});
	
	$("div#gallery_nav a#gallery_prev").click(function(){
		if( $("ul#gallery_thumbs li").length > 3 ){
			
			if( scrolling ){
				return false;
			}
			
			scrolling = true;
			
			$("ul#gallery_thumbs").css("left","-120px").children("li:last").prependTo($("ul#gallery_thumbs"));
			
			$("ul#gallery_thumbs").animate({left: "0px"}, 300, "swing", function(){
				scrolling = false
			});
			
		}
	});
	
});