// --------------------------------------------------
// jQuery Plug-in Photo Gallery
// Version: 17 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: 250,
			size: 60
		},config);

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

		var photoGalleryStage = function(pgItem){
			if(pgItem){
				jQuery('.photo-gallery-stage',target).fadeTo(photoGallerySpeed,0,function(){
					jQuery('.photo-gallery-image',target).html(pgItem);
						if(pgItem.alt){
							if(pgItem.title){
								// jQuery('.photo-gallery-caption',target).html('<a href="'+pgItem.href+'">'+pgItem.alt+'</a>').css({'display':'block'});
jQuery('.photo-gallery-caption',target).html(pgItem.alt).css({'display':'block'});
							}else{
								jQuery('.photo-gallery-caption',target).html(pgItem.alt).css({'display':'block'});
							}
							//if(pgItem.external == true) 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(tNum){
			jQuery(photoGalleryObjects).map(function(i){
				if(tNum == i) jQuery('a,span',this).addClass('photo-gallery-selected');
					else jQuery('a,span',this).removeClass('photo-gallery-selected');
			});
		}
		var photoGalleryThumbnailsAdj = function(tObject){
			if(tObject){
				jQuery(tObject).focus(function(){ this.blur();});
				var tWidth = jQuery('img',tObject).attr('width');
				if(photoGalleryThumbnailSize < tWidth){
					jQuery('img',tObject).css({'margin-left':0-(tWidth-photoGalleryThumbnailSize)/2});
				}
			}else return false;
		}

		// 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]      = new Image();
			photoGalleryItems[i].alt  = jQuery('img',this).attr('title');
			photoGalleryItems[i].src  = jQuery('img',this).attr('src');
			photoGalleryItems[i].title = jQuery('a',this).attr('href');
			if(jQuery('a',this).attr('rel') == 'external') photoGalleryItems[i].external = true;
				else photoGalleryItems[i].external = false;

			photoGalleryThumbnailsAdj(this);

			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);
