(function($) {
	$.fn.mySlider = function(options) {
		
	var defaults = {			
		container: '.slider',
		slides: '.slides',
		pagination: '.pagination',
		containerClick: true, // Click container for next slide
		autoStart: 3000, // Set to positive number for auto start and interval time
		restart: 0, // Set to positive number for restart and restart time
		slidespeed: 500, // Speed of slide animation
		fadespeed: 300, // Speed of fade animation
		autoHeight: false // Set to positive number for auto height and animation speed
	};
		
	this.each(function() {
		var obj = $(this);
		var o = $.extend(defaults, options);
		var s = $(o.slides,obj).children().size();
		var sw = $(o.container).width();	// container width
		var w = 0;	// slide width
		var tw = 0; // Total width
		var t = 0;
		var m = 0;
		var p = 0;
		var mover = 0;
		var u = 0;
		var i = 0;
		
		$(o.slides,obj).children().each(function(){
			tw += $(this).outerWidth();
			});
				
		if (tw < (sw + $(o.slides,obj).children(':first').outerWidth())){
			i = 0;
			//alert(a);
			while (tw < sw){
				$(o.slides,obj).children(':eq('+i+')').clone(true).insertAfter($(o.slides,obj).children(':last'));
				tw += $(o.slides,obj).children(':last').outerWidth();
				i++;
				}
				$(o.slides,obj).children(':eq('+i+')').clone(true).insertAfter($(o.slides,obj).children(':last'));
				tw += $(o.slides,obj).children(':last').outerWidth();
				i++;
			}
			
		$(o.slides,obj).children(":eq(0)").children("span").slideDown(400);
		
		$(o.slides,obj).children().each(function(){
			thisw = $(this).outerWidth();
			$(this).css({position:'absolute',left:p,display:'block'});
			p += thisw;
		});
		
		$(o.slides,obj).css({width:(p - thisw)});
		
		
		sliderIntervalID = setInterval(function(){if(u===0){animate();}}, o.autoStart);
		
		$(o.container).hoverIntent({
			timeout: 100,
			over: function(){
				if (u === 0){
					clearInterval(sliderIntervalID);
					mover = 1;
					}
			}, out: function(){
				if (mover === 1 && u === 0){
					sliderIntervalID = setInterval(function(){animate();}, o.autoStart);
					mover = 0;
					}
				}
			});
			
		
			
		function animate(){
			if (!mover){
				$(o.slides,obj).children(':eq(0)').children('span').slideDown({duration:400,complete:movealong()});
				}
			}
		
		function movealong(){
			u = 1;
//			clearInterval(sliderIntervalID);
			t = t+1;
			thisw = $(o.slides,obj).children(':eq(0)').outerWidth();
			clonew = $(o.slides,obj).children(':eq('+i+')').outerWidth();
			m += thisw;
			am = -(m);

			$(o.slides,obj).animate({left:am},o.slidespeed,"linear",function(){
				$(o.slides,obj).children(':eq(1)').find('span').slideDown({duration:400,complete:function(){
					$(o.slides,obj).children(':eq(0)').children('span').hide();
					$(o.slides,obj).children(':eq('+i+')').clone(true).insertAfter($(o.slides,obj).children(':last')).css({position:'absolute',display:'block',left:(p)}).children('span').hide();
					$(o.slides,obj).children(':eq(0)').remove();
					p += clonew;
//					sliderIntervalID = setInterval(function(){movealong();}, o.autoStart);
					}});
				});
			u = 0;
			//$("#num").html(t)
			}
		});
	};
})(jQuery);
