/* ============================================================
// Simple Slideshow plugin - manual control only (no auto play)
// Use the #drawSlidehow velocity macro to draw the UI
// $Id$
// ============================================================ */

(function($) {

$.fn.slideshow = function(options) {

	this.each(function(){

		var currentImage = 0;

		var settings = {
			top: 10,
			left: 80
		};

		if (options) {
			$.extend(settings, options);
		}

		var images = $(".slideshow_image", this);
		var captions = $(".slideshow_caption", this);
		var captionTop = parseInt($("img", images).get(0).height, 10) + 20;

		if (images.length > 1) {

			// Unbind any event handlers in case this slideshow has been shown before (or is still being shown)
			$("a", this).unbind("click");

			// Hide all but the first slide
			for (var i=0; i < images.length; i++) {
				$(images[i]).css('z-index', images.length - i).css('position', 'absolute');
				// Check if slide is reference to HTML and not just an image
				var text = $(images[i]).text();
				if (text.length > 0 && text.charAt(0) == '#') {
					// Replace identifier text with actual HTML copied from the identifier elsewhere in the document
					$(images[i]).html($(text).html());
				}
				// Caption is absolutely positioned 20 pixels below the height of the images
				$(captions[i]).css('z-index', captions.length - i).css('position', 'absolute').css('top', captionTop);
				$(images[i]).hide();
				$(captions[i]).hide();
			}
			$(images[0]).show();
			$(captions[0]).show();

			// Check for label and insert HTML
			var label = $(".slideshow_label", this);
			if (label.length == 1) {
				var text = $(label).text();
				if (text.length > 0 && text.charAt(0) == '#') {
					$(label).html($(text).html());
					// Be sure to position above the captions because they overlap and there may be links in the label
					$(label).css("z-index", captions.length + 1).css('position', 'absolute').css('top', captionTop + 54);
				}
			}

			// Reposition and show main container (it is intially drawn off-page)
			$(this).css('top', settings.top);
			$(this).css('left', settings.left);
			$(this).show();

			// Attach callbacks to the control buttons
			var buttons = $(".slideshow_button", this);
			$(buttons[0]).click(function() {
				// Back
				this.blur();
				prevImage = currentImage-1;
				if (prevImage == -1) {
					prevImage = images.length - 1;
				}
				$.slideshow.showImage(images, captions, currentImage, prevImage);
				currentImage = prevImage;
				return false;
			});
			$(buttons[1]).click(function() {
				// Next
				this.blur();
				nextImage = (currentImage+1) % images.length;
				$.slideshow.showImage(images, captions, currentImage, nextImage);
				currentImage = nextImage;
				return false;
			});
			var me = $(this);
			$(buttons[2]).click(function() {
				// Close
				me.hide();
				return false;
			});
		}

	});
};

$.slideshow = function() {}
$.slideshow.showImage = function(images, captions, currentImage, imageToShow) {
	$(images[currentImage]).fadeOut("normal");
	$(images[imageToShow]).fadeIn("normal");
	$(captions[currentImage]).fadeOut("normal");
	$(captions[imageToShow]).fadeIn("normal");
};
})(jQuery);
