/*
 * 	Easy Slider 1.7 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	markup example for $("#minislide").miniSlider();
 *	
 * 	<div id="minislide">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *		<h3>Usage</h3>
 *	
 *<pre>$(document).ready(function(){	
 *	$("#minislide").miniSlider({
 *		speed: 	800,
 *		pause:	2000
 *	});
 *});</pre>
 *
 */

(function($) {

	$.fn.miniSlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			speed: 			800,
			pause:			1000
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {
  			var play = true; 
			var obj = $(this); 				
			var s 	= $("li", obj).length;
			var pad = $("li", obj).css('padding-right');
			var w = $("li", obj).width()+parseInt(pad);
			var h = $("li", obj).height();
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("ul.play", obj).css('width',s*w);			
			$("ul.play", obj).prepend($("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
			$("ul.play", obj).append($("ul li:nth-child(2)", obj).clone());
			$("ul.play", obj).css('width',(s+1)*w);
			 $("li", obj).css('float','left');

			function adjust(){
				if(t>ts) t=0;		
				if(t<0) t=ts;	
				$("ul.play",obj).css("margin-left",(t*w*-1));
				};

			function animate(){
					var ot = t;				
					t = (ot>=ts) ? (t+1) : t+1;						
					var diff = Math.abs(ot-t);
					var speed = diff*options.speed;						
						p = (t*w*-1);
						$("ul.play",obj).animate(
							{ marginLeft: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);				
				timeout = setTimeout(function(){animate();},diff*options.speed+options.pause);
			};

			function stop(){};
			$("#minislide ul").hover(function(){$(this).removeClass('play');}, function(){$(this).addClass('play');})

			// init
			var timeout;
			timeout = setTimeout(function(){animate();},options.pause);
		});
	};
})(jQuery);




