// --------------------------------------------------
// jQuery Plug-in Photo Gallery
// Version: 28 Aug 2008
// Dependencies: jquery-1.2.6.js
// --------------------------------------------------
(function($){
	jQuery.fn.photoGallery = function(config){

		// Photo Gallery Config
		config = jQuery.extend({
			selected: 0,
			event: "click",
			speed: 160,
			size: 80
		},config);

		// Photo Gallery Properties and Methods
		var target                    = this;
		var photoGalleryThumbnailSize = config.size;
		var photoGallerySpeed         = config.speed;
		var photoGallerySelectedNum   = config.selected;
		var photoGalleryItems         = [];
		var photoGalleryObjects       = jQuery('ul>li',target);

		var photoGalleryStage = function(stageItem){
			jQuery('.photo-gallery-stage',target).fadeTo(photoGallerySpeed,0,function(){
				jQuery('.photo-gallery-image',target).html('<img />');
				jQuery('.photo-gallery-image img',target).attr('src',stageItem.src).attr('alt',stageItem.alt);
				if(stageItem.title) {
					jQuery('.photo-gallery-caption',target).html(stageItem.title).css({'display':'block'});
					if(stageItem.href) jQuery('.photo-gallery-caption',target).wrapInner('<a href="'+stageItem.href+'"></a>');
					if(stageItem.rel == "external") jQuery('.photo-gallery-caption a',target).attr('target','_blank');
				}else{
					jQuery('.photo-gallery-caption',target).empty().css({'display':'none'});
				}
				jQuery('.photo-gallery-stage',target).fadeTo(photoGallerySpeed,1.0);
			});
		}
		var photoGalleryThumbnails = function(thumbnailsNum){
			jQuery(photoGalleryObjects).map(function(i){
				if(thumbnailsNum == i){
					jQuery('a,span',this).addClass('photo-gallery-selected');
				}else{
					jQuery('a,span',this).removeClass('photo-gallery-selected');
					jQuery('a',this).focus(function(){this.blur();});
				}
			});
		}

		// Create Photo Gallery Stage Box
		jQuery('ul',target).before('<div class="photo-gallery-stage"></div>');
		jQuery('.photo-gallery-stage',target)
		.append('<p class="photo-gallery-image"></p>')
		.append('<p class="photo-gallery-caption"></p>');

		jQuery(photoGalleryObjects).map(function(i){

			photoGalleryItems[i] = {};
			photoGalleryItems[i].src = jQuery('img',this).attr('src');
			photoGalleryItems[i].alt = jQuery('img',this).attr('alt');
			photoGalleryItems[i].href = jQuery('a',this).attr('href');
			if(jQuery('a',this).attr('rel') == 'external') photoGalleryItems[i].rel = jQuery('a',this).attr('rel');
			if(jQuery('a',this).attr('title')) photoGalleryItems[i].title = jQuery('a',this).attr('title');
			if(jQuery('span',this).attr('title')) photoGalleryItems[i].title = jQuery('span',this).attr('title');
			if(jQuery('img',this).attr('title')) photoGalleryItems[i].title = jQuery('img',this).attr('title');

			jQuery('a,span',this).addClass('photo-gallery-thumbnail');

			if(config.event == 'click'){
				jQuery(this).click(function(){
					if(photoGallerySelectedNum != i){
						photoGalleryStage(photoGalleryItems[i]);
						photoGalleryThumbnails(i);
						photoGallerySelectedNum = i;
					}
					return false;
				});
			}else if(config.event == 'mouseover'){
				jQuery(this).mouseover(function(){
					if(photoGallerySelectedNum != i){
						photoGalleryStage(photoGalleryItems[i]);
						photoGalleryThumbnails(i);
						photoGallerySelectedNum = i;
					}
					return false;
				});
				jQuery(this).click(function(){ return false;});
			}
		});

		// Default Photo Gallery
		photoGalleryStage(photoGalleryItems[photoGallerySelectedNum]);
		photoGalleryThumbnails(photoGallerySelectedNum);
	}
})(jQuery);
