
(function($) {

	$.fn.customSlideshow = function(options){
	  
		// default configuration properties
		var defaults = {			
			speed: 			2000,
			auto:			true,
			pause:			4000,
			continuous:		true
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 
			var s = $(this).children('ul').children('li').length;
			var w = $(this).children('ul').children('li').width(); 
			var h = $(this).children('ul').children('li').height(); 
						
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			
			resize(w);
			
			function resize(w) {
				$(obj).children('ul').children('li').width(w);
				$(obj).children('ul').children('li').css("float","left");
				obj.width(w);
				obj.height(h);
				$(obj).children('ul').css('width',s*w);
				p = (t*w*-1);
				$(obj).children('ul').css('marginLeft', p);
			};
					
			function animate(dir){
				var ot = t;	
				switch(dir){
					case "next":
						t = (ot>=ts) ? (options.continuous ? 0 : ts) : t+1;						
						break; 
					default:
						if (dir > 0) {
							t = dir-1;
							break; 
						} else {
							return;
						}
				};	
				
				if (p != (t*w*-1)) {
					var diff = Math.abs(ot-t);
					var speed = diff*options.speed;						
					p = (t*w*-1);
					$(obj).fadeOut("slow", function() { 
						$(this).children('ul').css({ marginLeft: p });
                    });	
					$(obj).fadeIn("slow");
										
					if(options.auto && dir=="next"){;
						timeout = setTimeout( function(){
							animate("next");
						},(diff*options.speed) + options.pause);
					};
					
				};
			};
			// init

			var timeout;
			if(options.auto){;
					timeout = setTimeout(function(){
					animate("next");
				},options.pause );
			};		
				
		});
	  
	};

})(jQuery);



