(function($){
		$.fn.slide2 = function(options){
		var defaults = {
			slideWidth: 465,
			autoplay: true,
			duration: 15000,
			showSlideIndex: false
		};
		var options = $.extend(defaults, options);
		return this.each(function(){
		  	var slideshow2 = $(this);
			var o2 = options;
			var currentPosition2 = 0;
			var slides2 = $('.slide2');
			var numberOfSlides2 = slides2.length;
			  
			// Remove scrollbar in JS
			$('#slidesContainer2').css('overflow', 'hidden');

			// Wrap all .slides with #slideInner div
			slides2.wrapAll('<div id="slideInner2"></div>')
			
			// Float left to display horizontally, readjust .slides width
			.css({
				 'float' : 'left',
				 'width' : o2.slideWidth
			});
			
			// Insert a clone of first slide 
			$('.slide2:first').clone().appendTo('#slideInner2');
			  
			// Set #slideInner width equal to total width of all slides
			$('#slideInner2').css('width', o2.slideWidth * (numberOfSlides2+1));

			// Insert controls in the DOM
			slideshow2
				.prepend('<span class="control" id="leftControl2"></span>')
				.append('<span class="control" id="rightControl2"></span>');

			// Insert slides index
			if(o2.showSlideIndex==true)
			{
				slideshow2.append('<div id="slideIndex"></div>');
				for(var i=1; i<= numberOfSlides2; i++)
				{
					$('#slideIndex').append('<span id="slide-' + i + '" class="numbers">' + i + '</span>' );
				}
				$('.numbers').click(function(){ goto2( ($(this).attr('id')).replace('slide-','') - 1, false)});
			}
	
			// Create event listeners for .controls clicks
			$('#leftControl2').click(function(){ prev2()});
			$('#rightControl2').click(function(){ next2()});
		  
			// Start
			init2();
			
			//Init function
			function init2()
			{
				manageControls2(currentPosition2);
				if(o2.autoplay==true) setNextTimeOut(o2.duration);
			}
			
			// Next
			function next2()
			{
				currentPosition2++;
				if(currentPosition2 >= numberOfSlides2) currentPosition2=0;
				slideTo2(currentPosition2, true);
			}
		  
			// Previous
			function prev2()
			{
				currentPosition2--;
				if(currentPosition2 <0) currentPosition2=numberOfSlides2-1;
				slideTo2(currentPosition2, false);
			}
			
			// Go to a slide 
			function goto2(position)
			{
				currentPosition2=position;
				slideTo2(currentPosition2, false);
			}
			
			// Set time out for next slide
			function setNextTimeOut(time)
			{
				$('#slidesContainer2').stop();
				// Just a crap animation to get timer, I got problem with setTimeout
				$('#slidesContainer2').animate({optical: 1}, time, '', function(){next2();});
			}
		  
			// Slide
			function slideTo2(position, continuously)
			{
				$('#slideInner2').stop();
				// usual cases
				if(continuously==false || o2.autoplay==false || position!=0)
				{
					$('#slideInner2').animate({'marginLeft' : o2.slideWidth*(-position)},'', '', 
						function(){ 
							manageControls2(position);
							if(o2.autoplay==true) setNextTimeOut(o2.duration);
						}
					)
				}
				// autoplay: slide from last to first one continuously
				else
				{
					// slide to the 'fake' first slide (actually at the last)
					$('#slideInner2').animate({'marginLeft' : o2.slideWidth*(-numberOfSlides2)},'', '', 
						function(){ 
							//immediately change to the 'true' first slide
							$('#slideInner2').css('marginLeft',0);
							manageControls2(position)
							if(o2.autoplay==true) setNextTimeOut(o2.duration);
						}
					)
				}
			}
			
			// manageControls: Hides and Shows controls depending on currentPosition
			function manageControls2(position)
			{
				// Hide left arrow if position is first slide
				if(position==0){ $('#leftControl2').show() } else{ $('#leftControl2').show() };
				// Hide right arrow if position is last slide
				if(position==(numberOfSlides2-1)){ $('#rightControl2').show() } else{ $('#rightControl2').show() };
				// Hilight the current page
				if(o2.showSlideIndex==true)
				{
					// remove active class from all pages
					$('.numbers').removeClass("active");
					// add only to the current page
					$('#slide-' + (position +1)).addClass("active");
				}
			}
		});
		};
	})(jQuery);
