/*
 * jQuery Infinite Carousel
 * @author admin@catchmyfame.com - http://www.catchmyfame.com
 * @version 2.0.2
 * @date June 12, 2010
 * @category jQuery plugin
 * @copyright (c) 2009 admin@catchmyfame.com (www.catchmyfame.com)
 * @license CC Attribution-Share Alike 3.0 - http://creativecommons.org/licenses/by-sa/3.0/
 */
(function($){
	$.fn.extend({ 
		infiniteCarousel: function(options)
		{
			var defaults = {
				transitionSpeed: 800,
				displayTime: 4000,
				easeLeft: 'swing',
				inView: 1,
				padding: '0px',
				advance: 1
			};
			var options = $.extend(defaults, options);
	
    		return this.each(function(){
				var randID = Math.round(Math.random()*100000000);
				var o=options;
				var obj = $(this);

				var numImages = $('img', obj).length; // Number of images
				var imgHeight = 100;
				var imgWidth = 220;

				if(o.inView > numImages-1) o.inView=numImages-1; // check to make sure inview isnt greater than the number of images. inview should be at least two less than numimages (otherwise hinting wont work and animating left may catch a flash), but one less can work
				$(obj).css({'position':'relative','overflow':'hidden'}).width((imgWidth*o.inView)+(o.inView*parseInt(o.padding)*2)).height(imgHeight+(parseInt(o.padding)*2)); //,'overflow':'hidden'
				$('ul', obj).css({'list-style':'none','margin':'0','padding':'0','position':'relative'}).width(imgWidth*numImages);
				$('li', obj).css({'display':'inline','float':'left','padding':o.padding});

				// Move rightmost image over to the left
				$('li:last', obj).prependTo($('ul', obj));
				$('ul', obj).css('left',-imgWidth-(parseInt(o.padding)*2)+'px').width(9999);

				function moveLeft(dist)
				{
					if(dist==null) dist=o.advance;
					if(o.displayTime == 0){clearInterval(clearInt);} // If running a contonuous show with no display time, fist clear the interval. Then below, recursively call moveLeft
					$('li:lt('+dist+')', obj).clone(true).insertAfter($('li:last', obj)); // Copy the first image (offscreen to the left) to the end of the list (offscreen to the right)
					$('ul', obj).animate({left:-imgWidth*(dist+1)-(parseInt(o.padding)*(dist+1))*2},o.transitionSpeed,o.easeLeft,function(){ // Animate the entire list to the left
						$('li:lt('+dist+')', obj).remove(); // When the animation finishes, remove the first image (on the left). It has already been copied to the end of the list (right)
						$(this).css({'left':-imgWidth-parseInt(o.padding)*2});
						if(o.displayTime == 0){moveLeft();}
					});
				}

				// Kickoff the show
				var clearInt = setInterval(function(){moveLeft();},o.displayTime+o.transitionSpeed);
			});
		}
	});
})(jQuery);
