$(document).ready(function() {
	if (window.location.hash) {
		var gallery = window.location.pathname.split("/");
		//window.location = "http://" + window.location.host + "/" + gallery[1] + "/" + gallery[2] + "/" + window.location.hash.substr(1);
		window.location = "http://" + window.location.host + "/" + gallery[1] + "/" + window.location.hash.substr(1);
	}
	
	$("#thumbnails a").click(function(e) {
		var imgUrl = $(this).attr("href"); //New image to load
		var title = $(this).attr("data-title"); //Image Title
		var image = $("#gallery-wrapper img"); //The current image
		var imgHeight = $(image).height(); //Current image's height
		var holder = $(image).parent(); //Image holder
		var id = $(this).attr("data-image-id"); //Image id
		
		window.location.hash = id;
		
		//Change the clicked link's class to on
		$("#thumbnails *").removeClass("on");
		$(this).addClass("on");

		//Set the image holder's height to the size of the current image
		$(holder).css("height", imgHeight);
		
		$("#gallery-wrapper .title").fadeOut(100);
		
		//Fade out the current image
		$(image).fadeOut(200, function() {
			//Load the new image in it's place
			$(image).attr("src", "").attr("src", imgUrl);
			
			//Wait for image to load before performing animation
			$(image).load(function() {
				var newHeight = $(this).height() + 30; //The new image's height
				//Set the new image's caption
				$("#gallery-wrapper .title").html(title);
				
				//Animate the holder to the size of the new image
				$(holder).animate({
					height: newHeight
				}, 200, function () {
					//After holder is resized, fade in the new image
					$(image).fadeIn(200);
					$("#gallery-wrapper .title").fadeIn(200);
				});	
  			});
		});
		e.preventDefault();
	});

	var displayer = 0;
	var panels = $("[data-display]:last").attr("data-display");
	
	$(".paginator.disabled").click(function(e) {
		
		e.preventDefault();	
	});
	
	$(".paginator").not(".disabled").click(function(e) {
		thisDisplay = displayer;
		
		if ($(this).hasClass("prev")) {
			displayer--;
		} else {
			displayer++;
		}
		
		$("ul[data-display="+thisDisplay+"]").fadeOut(200, function() {
			var thisEl = $(this);
	
			if (!$(thisEl).closest("li.parent").find("ul[data-display="+displayer+"]").length) {
				$(thisEl).closest("li.parent").hide();
			}		
		
		});
		
		if (displayer > panels) {
			displayer = 0;
		}
		
		if (displayer < 0) {
			displayer = panels;
		}
		
		setTimeout(function() {
			$("[data-display="+displayer+"]").fadeIn(200).closest("li:hidden").show();
			
		}, 210);
		
		e.preventDefault();

	});	
	
});