/*
 * 	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 $("#slider").easySlider();
 *	
 * 	<div id="slider">
 *		<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>
 *
 */

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		"prev",
			prevText: 		"Previous",
			nextId: 		"next",
			nextText: 		"Next",
			controlsShow:	true,
			controlsFade:	true,
			speed: 			800,
			auto:			false,
			pause:			2000,
			continuous:		true,
			project:		"project"
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {
			
/* --------------------------------------------------------------------------
	=setup
--------------------------------------------------------------------------- */
			
			var obj = $(this);
			var s = $("li", obj).length;
			var w = $("li", obj).width();
			w = 909; // hardcoded for legacy support
			var h = $("li", obj).height();
			h = 635; // hardcoded for legacy support
			var clickable = true;
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			
			// setup ids for injected elements based on project name
			var counterId = "counter-"+options.project;
			var prevId = "prev-"+options.project;
			var nextId = "next-"+options.project;
			
			// display values for x/y readout
			var countCurrent = 1;
			var countTotal = s;
			
			// setup row of images
			$("ul", obj).css("width",s*w);
			if(options.continuous){
				$("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
				$("ul", obj).append($("ul li:nth-child(2)", obj).clone());
				$("ul", obj).css("width",(s+1)*w);
			};
			$("li", obj).css("float","left");
			
			// setup controls					
			if(options.controlsShow){
				var html;
				html += "<div>";
				html += "<span id=\""+prevId+"\" class=\"control-prev\"><a href=\"javascript:void(0);\">"+ options.prevText +"</a></span>";
				html += "<span id=\""+counterId+"\" class=\"control-counter\"></span>";
				html += "<span id=\""+nextId+"\" class=\"control-next\"><a href=\"javascript:void(0);\">"+ options.nextText +"</a></span>";
				html += "</div>";
				$(obj).append(html);
			};
			
			// add function to anchor image wrapper
			$("img", obj).wrap("<a href=\"javascript:void(0);\"></a>");
			$("li a", obj).click(function(){
				animate("next",true);
			});
			// add function to next/prev buttons
			$("a","#"+nextId).click(function(){		
				animate("next",true);
			});
			$("a","#"+prevId).click(function(){		
				animate("prev",true);
			});
			// add actual value for the counter
			updateCountDisplay();

			
/* --------------------------------------------------------------------------
	=functions
--------------------------------------------------------------------------- */

			function updateCountDisplay(){
				$("#"+counterId).empty();
				$("#"+counterId).append(countCurrent+"/"+countTotal);
			}

			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":
							countCurrent = (countCurrent>=countTotal) ? 1 : countCurrent+1;
							t = (ot>=ts) ? (options.continuous ? t+1 : ts) : t+1;
							break;
						case "prev":
							countCurrent = (countCurrent<=1) ? countTotal : countCurrent-1;
							t = (t<=0) ? (options.continuous ? t-1 : 0) : t-1;
							break;
						default:
							t = dir;
							break;
					};	
					
					// update counter
					updateCountDisplay();
										
					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(!options.continuous && options.controlsFade){					
						if(t==ts){
							$("a","#"+nextId).hide();
						} else {
							$("a","#"+nextId).show();
						};
						if(t==0){
							$("a","#"+prevId).hide();
						} else {
							$("a","#"+prevId).show();
						};					
					};				
					
					if(clicked) clearTimeout(timeout);
					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);
			};		
					
			if(!options.continuous && options.controlsFade){					
				$("a","#"+prevId).hide();
			};				
			
		});
	  
	};

})(jQuery);




