(function($) {

jQuery.fn.slideshow = function(ps, pa, picTitle, loading, delay, random, bordersize)
{
	if (delay==null)
	{
		delay = 1000;
	}
	if (random==null)
	{
		random = false;
	}
	if (bordersize==null)
	{
		bordersize = 6 * 2;
	}

	jQuery.fn.slideshow.slideshow = {};
	jQuery.fn.slideshow.preloadAmount = 5;

    jQuery.fn.slideshow.blockItem(loading);
	
	this.each(function() {
		var id = jQuery(this).attr('id');
		jQuery.fn.slideshow.slideshow[id] = {};
		jQuery.fn.slideshow.slideshow[id].container = jQuery(this);
		jQuery.fn.slideshow.slideshow[id].delay = delay;
		jQuery.fn.slideshow.slideshow[id].random = random;
		jQuery.fn.slideshow.slideshow[id].pause = false;
		jQuery.fn.slideshow.slideshow[id].pictureSrc = ps;
		jQuery.fn.slideshow.slideshow[id].pictureAlt = pa;
		jQuery.fn.slideshow.slideshow[id].maxWidth = 0;
		jQuery.fn.slideshow.slideshow[id].maxHeight = 0;
		jQuery.fn.slideshow.slideshow[id].borderSize = bordersize;
		jQuery.fn.slideshow.slideshow[id].blocked = true;
		jQuery.fn.slideshow.slideshow[id].pictureTitle = picTitle;
		jQuery.fn.slideshow.slideshow[id].loading = loading;
		jQuery.fn.slideshow.slideshow[id].items = {};
		jQuery.fn.slideshow.slideshow[id].waitingImage = {};

		jQuery.fn.slideshow.slideshow[id].container.show();
		if (jQuery.fn.slideshow.slideshow[id].pictureTitle != null)
		{
			jQuery.fn.slideshow.slideshow[id].pictureTitle.show();
		}

		jQuery.fn.slideshow.incrementAndPreload(id);

		jQuery.fn.slideshow.slideshowShow(id);
	});
};

jQuery.fn.slideshowjc = function(ps, pa, picTitle, loading, delay, random, bordersize)
{
	if (delay==null)
	{
		delay = 1000;
	}
	if (random==null)
	{
		random = false;
	}
	if (bordersize==null)
	{
		bordersize = 6 * 2;
	}

	jQuery.fn.slideshow.slideshow = {};
	jQuery.fn.slideshow.preloadAmount = 5;

    jQuery.fn.slideshow.blockItem(loading);
	
	this.each(function() {
		var id = jQuery(this).attr('id');
		jQuery.fn.slideshow.slideshow[id] = {};
		jQuery.fn.slideshow.slideshow[id].container = jQuery(this);
		jQuery.fn.slideshow.slideshow[id].delay = delay;
		jQuery.fn.slideshow.slideshow[id].random = random;
		jQuery.fn.slideshow.slideshow[id].pause = false;
		jQuery.fn.slideshow.slideshow[id].pictureSrc = ps;
		jQuery.fn.slideshow.slideshow[id].pictureAlt = pa;
		jQuery.fn.slideshow.slideshow[id].maxWidth = 0;
		jQuery.fn.slideshow.slideshow[id].maxHeight = 0;
		jQuery.fn.slideshow.slideshow[id].borderSize = bordersize;
		jQuery.fn.slideshow.slideshow[id].blocked = true;
		jQuery.fn.slideshow.slideshow[id].pictureTitle = picTitle;
		jQuery.fn.slideshow.slideshow[id].loading = loading;
		jQuery.fn.slideshow.slideshow[id].items = {};
		jQuery.fn.slideshow.slideshow[id].waitingImage = {};

		jQuery.fn.slideshow.slideshow[id].container.show();
		if (jQuery.fn.slideshow.slideshow[id].pictureTitle != null)
		{
			jQuery.fn.slideshow.slideshow[id].pictureTitle.show();
		}

		jQuery.fn.slideshow.incrementAndPreload(id);

		jQuery.fn.slideshow.slideshowShow(id);
		
		jQuery(this).size(100,100);

	});
};

jQuery.fn.slideshow.incrementAndPreload = function(id)
	{
		var s = jQuery.fn.slideshow.slideshow[id];
		// set i for the next image we want to show
		if (s.random)
		{
			s.i = Math.floor(Math.random()*s.pictureSrc.length);
		}
		else
		{
		    if(s.i==null || s.i+1 >= s.pictureSrc.length)
			{
				s.i=0;
			}
			else
			{
				s.i++;
			}
		}

		// insert next preloadAmount images now they don't exist
		// TODO: this wont work if Random is used
		for(j = s.i; ((j < (s.i + jQuery.fn.slideshow.preloadAmount)) && (j < s.pictureSrc.length)); j++)
		{
			if(jQuery('img#slideshow_' + id + '_picture_' + j).length == 0)
			{
				jQuery.fn.slideshow.insertImage(id, j);
			}
		}
	}

jQuery.fn.slideshow.insertImage = function(id, i)
	{
		var s = jQuery.fn.slideshow.slideshow[id];

		imgStr = '<img id="slideshow_' + id + '_picture_' + i + '" class="picture" src="' + s.pictureSrc[i] + '" alt="' + s.pictureAlt[i] + '" />';
		s.container.append(imgStr);
		s.items[i] = jQuery('img#slideshow_' + id + '_picture_' + i);
		s.items[i].hide();
		s.waitingImage[i] = true;
	}

jQuery.fn.slideshow.blockItem = function(jItem)
	{
        if (jItem == null)
        {
            return;
        }
        
		// enable transparent overlay on FF/Linux
		jQuery.blockUI.defaults.applyPlatformOpacityRules = false;
		jQuery.blockUI({
			message: jItem,
			css: {
	        	border: 'solid 2px #404040',
	        	padding: '15px',
	        	backgroundColor: '#000000',
	        	opacity: '0.9',
	        	color: '#a177a7' },
	        overlayCSS: {
	        	backgroundColor: '#101010',
	        	opacity: '0.9' }
	     });
	}

jQuery.fn.slideshow.unblockItem = function(jItem)
	{
		jQuery.unblockUI();
	}

jQuery.fn.slideshow.block = function(id)
	{
		if (jQuery.fn.slideshow.slideshow[id].blocked == false)
		{
			jQuery.fn.slideshow.slideshow[id].blocked = true;
    	    jQuery.fn.slideshow.blockItem(jQuery.fn.slideshow.slideshow[id].loading);
		}
	}

jQuery.fn.slideshow.unblock = function(id)
	{
		if (jQuery.fn.slideshow.slideshow[id].blocked == true)
		{
			jQuery.fn.slideshow.slideshow[id].blocked = false;
		    jQuery.fn.slideshow.unblockItem(jQuery.fn.slideshow.slideshow[id].loading);
		}
	}

jQuery.fn.slideshow.slideshowShow = function(id)
	{
		var s = jQuery.fn.slideshow.slideshow[id];

		if(s.waitingImage[s.i] == true)
		{
			if(jQuery('img#slideshow_' + id + '_picture_' + s.i)[0].complete == false)
			{
				jQuery.fn.slideshow.block(id);
				setTimeout('jQuery.fn.slideshow.slideshowShow("' + id + '")', 500);
			}
			else
			{
				s.waitingImage[s.i] = false;
			 	jQuery.fn.slideshow.unblock(id);
			}
		}

		if (s.waitingImage[s.i] == false)
		{
		    /////////////////////////////////////////////////
			// Make sure the picture fits in the container //
			/////////////////////////////////////////////////
			
            contWidth = s.container.width();
            contHeight = s.container.height();

			picHeight = s.items[s.i].height() + s.borderSize;
				
			
            if (picHeight > contHeight)
			{
			    s.items[s.i].width("auto");
                s.items[s.i].height(contHeight - s.borderSize);
            }

			picWidth = s.items[s.i].width() + s.borderSize;
			if (picWidth > contWidth)
			{
			    s.items[s.i].height("auto");
                s.items[s.i].width(contWidth - s.borderSize);
            }

			// calculate image width and height, including border
			picWidth = s.items[s.i].width() + s.borderSize;
			picHeight = s.items[s.i].height() + s.borderSize;

		    //////////////////////////////////////
			// Position the picture on the page //
			//////////////////////////////////////

            // Center the image on the container and add the top left corner of the container onto that to make it an absolute position on the page
            pos = s.container.offset();
            thisLeft = pos.left + ((contWidth - picWidth) / 2);
            thisTop = pos.top + ((contHeight - picHeight) / 2);

            // Position image on page
			s.items[s.i].css({position: 'absolute', left: thisLeft, top: thisTop, zIndex: (s.pictureSrc.length - s.i)});

            //////////////////////////////////////////////////////////////
            // Display Picture and Title, fading out last picture shown //
            //////////////////////////////////////////////////////////////

            // Update picture title
            if (s.pictureTitle != null)
            {
                s.pictureTitle.html(s.pictureAlt[s.i]);
            }
            
            // Fade in this picture
			jQuery(s.items[s.i]).fadeIn('slow');
			
			// Fade out last picture, taking care if we've wrapped around to start again
			if (s.i == 0)
            {
                jQuery(s.items[s.pictureSrc.length-1]).fadeOut('slow');
            }
            else
            {
			    jQuery(s.items[s.i-1]).fadeOut('slow');
			}
			
			// Set a timer to call this function again in 'delay' time after all queued effects have executed
		    jQuery(s.items[s.i]).animate({opacity: 1.0}, s.delay, function(){jQuery.fn.slideshow.slideshowShow(id)});

            /////////////////////////////////////////////////////
            // Increment and preload ready for future pictures //
            /////////////////////////////////////////////////////

			jQuery.fn.slideshow.incrementAndPreload(id);
		}
	}

})(jQuery);

