/**********************
 * Slideshow Plugin   *
 * Iain MacNeill      *
 * V1.00.00           *
 * June 22, 2010      *
 **********************/
(function($)
{
	$.fn.slideshow = function(config)
	{
		// Setup default configuration
		var opts = $.extend(
		{
			ImagePath: 'images/',
			AnimSpeed: 500,
			TransDelay: 5000,	
			StartPlay: true,
			Looping: 'Rewind',
			Cycles: 2,
			CaptionFont: 'Arial',
			CaptionColor: '#333',
			CaptionFontSize: '11px',
			CaptionLineHeight: '13px',
			CaptionBackColor: '#999',
			ShowCapNumbers: false,
			PlayPauseButton: null,
			NextButton: null,
			PreviousButton: null,
			PlayImage: '',
			PauseImage: ''
		}, config);
		
		// Gets run on each element bound to slideshow at startup
		return this.each(function()
		{
			// Ensure slideshow is bound to a UL
			if(this.nodeName.toLowerCase()!="ul")
			{
				alert("This slideshow needs to be associated with an unordered-list item.");
				return false;
			}
			$.slideshow(this,opts);
		});
	};

	// Functions
	$.slideshow = function(oSShow, opts)
	{
		oSShow.CurSld = 0;
		oSShow.Slding = false;
		oSShow.TotSld = $(oSShow).children().length;
		oSShow.SldWdt = $(oSShow).children('li:first-child').children('img:first-child').width();

		// Cycle through each LI to create captions
		CapNum=1;
		$(oSShow).children('li').each(function()
		{
			theCap = $(this).children('img').attr('title');
			if (opts.ShowCapNumbers)
				theCap+=' ('+CapNum+' of '+oSShow.TotSld+')';
			$(this).append('<div style="width:'+(oSShow.SldWdt-10)+'px;background-color:'+opts.CaptionBackColor+';color:'+opts.CaptionColor+';font:normal '+opts.CaptionFontSize+'/'+opts.CaptionLineHeight+' '+opts.CaptionFont+';height:auto;">'+theCap+'</div>');
			CapNum++;
		});

		$(oSShow).css('background-color',opts.CaptionBackColor);
		$(oSShow).children('li').css('background-color',opts.CaptionBackColor);

		// If next button specified, link it up
		if (opts.NextButton)
		{
			$('#'+opts.NextButton).click(function() { $.slideshow.advance(oSShow, -1, opts); });
		}

		// If previous button specified, link it up
		if (opts.PreviousButton)
		{
			$('#'+opts.PreviousButton).click(function() { $.slideshow.advance(oSShow, 1, opts); });
		}

		// If play/pause button specified, link it up
		if (opts.PlayPauseButton)
		{
			$('#'+opts.PlayPauseButton).click(function()
			{
				if (oSShow.Plying)
				{
					// Pause (Clear the queue)
					oSShow.Plying = false;
					$(oSShow).parent('div').clearQueue();
					$('#'+opts.PlayPauseButton).children('img').attr('src',opts.ImagePath+opts.PlayImage);
				}
				else
				{
					oSShow.Plying = true;
					$.slideshow.advance(oSShow, -1, opts);
					$('#'+opts.PlayPauseButton).children('img').attr('src',opts.ImagePath+opts.PauseImage);
					for (i = 0; i < (oSShow.TotSld * opts.Cycles); i++)
					{
						$(oSShow).parent('div').delay(opts.TransDelay);
						$.slideshow.advance(oSShow, -1, opts);
					}
				}
			});
		}

		// Queue up all animations
		if (opts.StartPlay)
		{
			oSShow.Plying = true;
			if (opts.PlayPauseButton)
				$('#'+opts.PlayPauseButton).children('img').attr('src',opts.ImagePath+opts.PauseImage);
			for (i = 0; i < (oSShow.TotSld * opts.Cycles); i++)
			{
				$(oSShow).parent('div').delay(opts.TransDelay);
				$.slideshow.advance(oSShow, -1, opts);
			}
		}
	};

	// Function to advance the slideshow container
	$.slideshow.advance = function( oSShow, dir, opts)
	{
		$(oSShow).parent('div').queue(function()
		{
			if (!oSShow.Slding)
			{
				oSShow.Slding = true;
				oSShow.CurSld+=(dir>0?-1:1);

				// If opts Looping is 'Rewind'...
				if (opts.Looping=='Rewind')
				{
					// If we are at the end of the show, and 
					// user wants next photo, reverse to start
					if (oSShow.CurSld >= oSShow.TotSld)
					{
						oSShow.CurSld = 0;
						dir = oSShow.TotSld - 1;
					}
					else if (oSShow.CurSld < 0)
					{
						oSShow.CurSld = oSShow.TotSld - 1;
						dir = (oSShow.TotSld-1)*-1;
					}
				}

				$.slideshow.move( oSShow, dir, opts);
			}
			$(this).dequeue();
		});
	};

		// Function to mode the slideshow container one position left or right
	$.slideshow.move = function( oSShow, dir, opts)
	{
		var iCurPos = Number($.slideshow.stripNN($(oSShow).css("left")));
		var iNewPos = iCurPos + (oSShow.SldWdt * dir);
		
		//alert(oSShow.id+"\niCurPos="+iCurPos+"\niNewPos="+iNewPos+"\nLooping="+opts.Looping);

		// Handle left to right cycling
		if ((opts.Looping=='Cycle') && (dir>0))
		{
			iCurPos=iNewPos*-1;
			iNewPos=0;
			$(oSShow).prepend($(oSShow).children('li:last-child')).remove('li:last-child').css('left',iCurPos);
		}

		$(oSShow).animate(
		{
			left: iNewPos
		}, opts.AnimSpeed, 'linear', function()
		{
			oSShow.Slding = false;
			if (opts.Looping=='Rewind')
			{
				// If just rewound, reset direction
				if (Math.abs(dir) > 1)
					dir = (dir>0?-1:1);
			}
			// Handle right to left cycling
			else if ((opts.Looping=='Cycle')&&(dir<0))
				$(oSShow).append($(oSShow).children('li:first-child')).remove('li:first-child').css('left',iCurPos);
		});
	};

	$.slideshow.stripNN = function(str)
	{
		sNew = "";
		for (var iPos = 0; iPos < str.length; iPos++)
		{
			if ("-0123456789".indexOf(str.substr(iPos,1)) > -1)
				sNew += str.substr(iPos,1);
		}
		return sNew;
	};

})(jQuery);
