(function()
{
	if (undefined == window.jQuery)
		throw "jQuery is required";

	if (jQuery.fn.jquery < "1.3.2")
		throw "A minimum of jQuery 1.3.2 is required";
		
	if (window.photoViewer)
		return;
		
	/*
		REMOVE THIS COMMENT BLOCK FOR PRODUCTION.
		
		The photoViewer presumes:
			* images are in the IMAGES_DIR relative to "this" page (default "images/")
			* all images use the IMAGE_EXT (default ".jpg")
			* all thumbnails have a suffix of THUMBS_SUFFIX (default "_thumb")
			* images and thumbs start with the same thing and only differ by
			  THUMBS_SUFFIX, e.g. 001.jpg and 001_thumb.jpg
			* the preview of the image is in a div with id PREVIEW_ID (default "photo_preview")
			* the thumbnails are shown in a div with id THUMBS_ID (default "photo_thumbnails")
			
		To use, just init with an array of image names (without the extension) in the order, e.g.

			$(document).ready(function() {
				photoViewer.init([
					"001", "002", "003", "004"
				]);
			});
	 */
	photoViewer = {
		IMAGES_DIR: "images/",
		THUMBS_SUFFIX: "_thumb",
		IMAGE_EXT: ".jpg",
		PREVIEW_ID: "photo_preview",
		THUMBS_ID: "photo_thumbnails",
		items: null,
		init: function(items) {
			if (!items)
				return;
				
			this.items = items;
				
			var d = $("#" + this.THUMBS_ID);
			
			for (var i = 0; i < items.length; i++)
				d.append("<img src=\"images/" + items[i] + this.THUMBS_SUFFIX + this.IMAGE_EXT + "\" rel=\"" + i + "\"/>");
			
			$("#" + this.THUMBS_ID + " img").click(function() { photoViewer.handleClick($(this)); });
			
			this.handleClick($("#" + this.THUMBS_ID + " img[rel='0']"));
		},
		handleClick: function(who) {
			var i = parseInt(who.attr("rel"));
			var d = $("#" + this.PREVIEW_ID);
			
			d.empty();
			
			if (i > 0)
				d.append("<span rel=\"" + (i - 1) + "\">&lt;</span> ");
			
			if (i + 1 < this.items.length)
				d.append("<span rel=\"" + (i + 1) + "\">&gt;</span>");
				
			d.append("<img src=\"" + this.IMAGES_DIR + this.items[i] + this.IMAGE_EXT + "\"/>");

			$("#" + this.PREVIEW_ID + " span").click(function() { photoViewer.handleClick($(this)); });
		}
	};
})();
