jQuery.slideshow = {
		build : function(settings)
		{
			settings = jQuery.extend({
							photoContainer : '#photoBig',
							imageLoading : "/img/gallery-loader.gif",
							photoFadeInDuration : 2000,
							slideDuration : 5000
						},settings);
			
			return jQuery(this).each(
				function()
				{
					var images = jQuery(this).children('LI').children('IMG');
					var ulist = jQuery(this);
					var curPhoto = 0;
					
					function _activate()
					{
						jQuery(ulist).hide();
						_setup_interface();
						_startSlideshow();
					}
					
					function _setup_interface()
					{
						jQuery(settings.photoContainer).append('<div id="photoContainer"><img src="' +  settings.imageLoading + '" id="photoGallery-image-view">');	
					}
					
					function _positionImageView(intImageWidth, intImageHeight)
					{
						var intCurrentWidth = $(settings.photoContainer).width();
						var intCurrentHeight = $(settings.photoContainer).height();
						marginLeft = (intCurrentWidth - intImageWidth)/2;
						marginTop = (intCurrentHeight- intImageHeight)/2;
						jQuery("#photoGallery-image-view").css({'margin-left' : marginLeft});
					}
					
					function _startSlideshow()
					{
						var image_source = jQuery(images[curPhoto]).attr('src');
						_set_image_to_view(image_source);
						var t = setInterval(function(){ _nextPhoto() },settings.slideDuration);
					}
					
					function _nextPhoto()
					{
						curPhoto++;
						if(curPhoto >= images.length)
						{
							curPhoto = 0;	
						}
						var image_source = jQuery(images[curPhoto]).attr('src');
						_set_image_to_view(image_source);
					}
					
					function _set_image_to_view(image_source)
					{
						var objImagePreloader = new Image();
						objImagePreloader.onload = function()
						{
							jQuery('#photoGallery-image-view').fadeOut("quick", function() 
							{
								jQuery("#photoGallery-image-view").attr('src',image_source);
								_positionImageView(objImagePreloader.width,objImagePreloader.height);
								jQuery('#photoGallery-image-view').fadeIn(settings.photoFadeInDuration);
							});
							objImagePreloader.onload=function(){};
						}
						objImagePreloader.src = image_source;
					}
					
					_activate();
				});
		}
}
jQuery.fn.slideshow = jQuery.slideshow.build;
