(function(jQuery) {
	jQuery.fn.infiniSlider = function(options) {

		options = jQuery.extend({
			duration: 500, 				/* transition duration */
			easing: 'swing', 				/* transition duration */
			autoSlideTime: 300, 			/* auto slide - as milliseconds ( ie: 10000 = 10 seconds ). Leave as false to not auto-slide */
			indexItems: null,			/* array of items to use as a clickable index of all the rotating items. Optional */
			addMouseEvents: true,		/* Automatically add pause and resume mouse events on hover (only if autoslide has a value) */
			nextButton: '',
			previousButton: ''
		}, options);
		
		//loop, in case we want multiple sliders.
		jQuery(this).each(function(index,container) { 

			//set up the container
			var container= jQuery(container);

			//set up the positioning of each item, and hide it. 
			container.children().css({
				'position' : 'absolute',
				'left' : '0'
			}).hide();
			
			//get the width of the first item
			var itemWidth = 687;
			
			//get the height of the first item
			var itemHeight = container.find(':first').show().addClass('current').height();
			
			var itemsCount = container.children().size();

			// set up the container
			container.css({
				'position' : 'relative',
				'width' : itemWidth
			});

			
			var stoppedAnimating = function() {
				isAnimating = false;
			}
			
			var animationOptions = {
				'duration' : options.duration,
				'easing' : options.easing,
				'queue' : true,
				'complete' : stoppedAnimating
			};
			
			var isAnimating = false;
			
			// function to show a specific item, based on it's index
			var showItem = function(index) {
				
				var oldItem = jQuery(container.find('.current'));
				
				var currentIndex = oldItem.index();
				var reverse = false;
				var direction = null;
				
				if (isAnimating) return false;

				if (typeof(index)=="undefined") { 
					index = 'next';
				}
						
				if (index == 'next') {
					direction = 'next';
					index = currentIndex + 1
				}
				
				if (index == 'previous') {
					direction = 'previous';
					index = currentIndex - 1;
				}
				
				if (index == 'first') {
					index = 0;
				}
		
				if (index == 'last') {
					index = itemsCount-1;
				}
				
				if (index < 0) {
					index = itemsCount - 1;
				}
				
				if (index > (itemsCount - 1)) {
					index = 0;
				}
				
				if (index < currentIndex) {
					reverse = true;
				}
				
				jQuery(options.indexItems).removeClass('selected');
				jQuery(options.indexItems[index]).addClass('selected');
				
				var newItem = jQuery(container.children()[index]);
				
				if (newItem.index() == oldItem.index()) return false;
				
				isAnimating = true;
				
				if ((reverse || direction == 'previous') && direction != 'next') {
					var oldItemEnd = itemWidth;
					var newItemStart = -itemWidth;
				} else {
					var oldItemEnd = -itemWidth;
					var newItemStart = itemWidth;
				}
				
				oldItem.animate({
					'left' : oldItemEnd
				},animationOptions).removeClass('current');
				
				newItem.show().css({'left' : newItemStart});
				newItem.animate({
					'left' : '0' 
				},animationOptions).addClass('current');
				
				

			}
			
			jQuery(options.nextButton).click(function() {
				showItem('next');
				return false;
			});
			jQuery(options.previousButton).click(function(){
				showItem('previous');
				return false;
			});
			
			//set up the index clickable items if necessary
			options.indexItems.each(function(index,indexItem) {
				jQuery(indexItem).click(function() {
					showItem(index);
					return false;
				});
			});
			
		});
		return this;
 	}
})(jQuery);

