/*
 * 	Easy Slider - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/3783/jquery-plugin-easy-image-or-content-slider
 *
 *	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 $("#images").easySlider();
 *	
 * 	<div id="images">
 *		<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: 		'#prevId',
			prevText: 		'Previous',
			nextId: 		'#nextId',	
			nextText: 		'Next',
			orientation:	'', //  'vertical' is optional;
			speed: 			1100,
			time:			5000,
			timeStartAgain:	30000
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			obj = $(this); 				
			var s = $("li", obj).length;
			var w = obj.width(); 
			var h = obj.height(); 
			var ts = s-1;
			var t = 0;
			var tNew = 0;
			var timer;
			var vertical = (options.orientation == 'vertical');

			$(options.nextId).click(function(){		
				stopTimer();
				animate("next");
				$(options.prevId).fadeIn();
			});
			$(options.prevId).click(function(){		
				stopTimer();
				animate("prev");
				$(options.nextId).fadeIn();
			});
			
		
			
			$("li", obj).each(function(i) {
				if (i > 0)
					$(this).hide(1);
			});		
			
			function stopTimer(){
				clearTimeout(timer);
				timer = setTimeout(function(){
					setTimerAction(options.time);
				},options.timeStartAgain);
			};
			
			function setTimerAction(time){
				animate('next');
				timer = setTimeout(function(){
					setTimerAction(time);
				},time);
			};
			timer = setTimeout(function(){
				setTimerAction(options.time);
			},options.time);
			
			function animate(dir){
				var tOld = t;
				if (dir == "next"){
					t = (t>=ts) ? 0 : t+1;	
				} else if(dir == "open"){
					t = tNew;
				} else{
					t = (t<=0) ? ts : t-1;
				};				
				
				$("ul li",obj).eq(tOld).fadeOut(options.speed);
				$("ul li",obj).eq(t).fadeIn(options.speed);
				
			};
		});
	  
	};

})(jQuery);

