// Turns a list of linked images to an image swapping gallery. First image has class .largeimages 
// Thumbs are kept in a separate directory and must be named the same as the full-size images.
// Also swaps alt tags and an h3 content in the same list item as the images.
// Uses jQuery1.1

$(document).ready(function(){
						   						   
//set directories
var photoDirectory = "photography/";
var thumbsDirectory = "photography/thumbs/";

//$.preloadImages("over.png", "out.gif");
 
$("ul.imageblock/li/a").click(function(){
	//disable click whilst images are swapped

									   
	//get data for current full size image
	currentImageSrc = $(this).parent().siblings(".largeimages").children("img").attr("src");
	currentImageAlt = $(this).parent().siblings(".largeimages").children("img").attr("alt");
	currentImageTitle = $(this).parent().siblings(".largeimages").children("h3").html();	
	
	//get data for clicked image
	chosenImage = $(this).attr("href");
	chosenImageAlt = $(this).children("img").attr("alt");
	chosenImageTitle = $(this).parent().children("h3").html();
	
	//strip extraneous data from image identifiers. This step could be removed quite safely but keeps the links relative rather than absolute.
	slashLoc = currentImageSrc.lastIndexOf("/");
	currentImage = currentImageSrc.slice((slashLoc+1));
	
	//Fade out large image, replace with clicked image. Fade image back in after fade out cmpleted.
	$(this).parent().siblings(".largeimages").children("h3 | img").fadeOut("slow",function(){
			$(this).attr({ src: chosenImage, alt: chosenImageAlt });
			$(this).siblings("h3").html(chosenImageTitle);
			$(this).fadeIn("slow");
	});
	
	//Fade out thumb image, replace with clicked image. Fade image back in after fade out completed.
	$(this).fadeOut("slow",function(){		
		$(this).attr({href: photoDirectory + currentImage});
		$(this).children("img").attr({ src: thumbsDirectory + currentImage, alt: currentImageAlt });
		$(this).parent().children("h3").html(currentImageTitle);
		$(this).fadeIn("slow");
	});
	
	//Disable the direct link so we don't leave the page.
	return false;
}); 
});
