/*	based on:
 * 	easySlider - 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.
 */

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'slider-prevBtn',
			prevText: 		'Previous',
			nextId: 		'slider-nextBtn',
			nextText: 		'Next',
			speed: 			800,
			auto:			false,
			pause:			4000,
			continuous:		true
		}; 
		
		var options = $.extend(defaults, options);
				
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			//var w = $("li", obj).width();
			var w = 210;
			var h = $("li", obj).height(); 
			var clickable = true;
			obj.width(w*3);
			//obj.height(h);
			//obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("ul", obj).css('width',s*w);
			
			
			
			$("ul", obj)
				.prepend($("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"))
				.append($("ul li:nth-child(2)", obj).clone(), $("ul li:nth-child(3)", obj).clone(), $("ul li:nth-child(4)", obj).clone())		
				.css('width',(s+3)*w);
			
				$("a","#"+options.nextId).click(function(e){
					animate("next",true);
					e.preventDefault();
				});
				$("a","#"+options.prevId).click(function(e){
					animate("prev",true);
					e.preventDefault();
				});	
							
			function adjust(){
				if(t>ts) t=0;		
				if(t<0) t=ts;	
					$("ul",obj).css("margin-left",(t*w*-1));
				clickable = true;

			};
			
			function animate(dir,clicked){
				if (clickable){
					clickable = false;
					var ot = t;				
					switch(dir){
						case "next":
							t = (ot>=ts) ? (options.continuous ? t+1 : ts) : t+1;						
							break; 
						case "prev":
							t = (t<=0) ? (options.continuous ? t-1 : 0) : t-1;
							break; 
						case "first":
							t = 0;
							break; 
						case "last":
							t = ts;
							break; 
						default:
							t = dir;
							break; 
					};	
					var diff = Math.abs(ot-t);
					var speed = diff*options.speed;						
						p = (t*w*-1);
						$("ul",obj).animate(
							{ marginLeft: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);				
					
					
					//if(clicked) clearTimeout(timeout);
					if(clicked){
						clearTimeout(timeout);
						if(options.auto){
							timeout = setTimeout(function(){
								animate("next",false);
							},diff*options.speed+options.pause);
						};
					};
					
					if(options.auto && dir=="next" && !clicked){
						timeout = setTimeout(function(){
							animate("next",false);
						},diff*options.speed+options.pause);
					};
			
				};
				
			};
			// init
			var timeout;
			if(options.auto){
				timeout = setTimeout(function(){
					animate("next",false)
				},options.pause);
			};		
			
		});
	  
	};

})(jQuery);
