/*
 * 	Easy Slider 1.5 - 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 jQuery("#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>
 *
 */

/*-- def easing --*/
	jQuery.extend(jQuery.easing,
	{
		easeInExpo: function (x, t, b, c, d) {
			return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
		},
		easeOutExpo: function (x, t, b, c, d) {
			return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
		},
		easeInOutExpo: function (x, t, b, c, d) {
			if (t==0) return b;
			if (t==d) return b+c;
			if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
			return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
		},
		easeInCirc: function (x, t, b, c, d) {
			return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
		},
		easeOutCirc: function (x, t, b, c, d) {
			return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
		}
	});
/*-- //def easing --*/


(function(jQuery) {

	jQuery.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'&lt; previous',
			nextId: 		'nextBtn',	
			nextText: 		'next &gt;',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			100,
			auto:			false,
			pause:			2000,
			continuous:		false
		}; 
		
		var options = jQuery.extend(defaults, options);  
				
		this.each(function() {  
			var obj = jQuery(this); 				
			var s = jQuery("ul#test li", obj).length;
			var w = jQuery("ul#test li", obj).width(); 
			var h = jQuery("ul#test li", obj).height(); 
			//obj.width(w); 
			obj.height(h); 
			var ts = s;
			var t = 0;
			jQuery("ul#test", obj).css('width',s*w);			
			jQuery("ul#test", obj).css('height', h);			
			if(!options.vertical) jQuery("ul#test li", obj).css('float','left');
			
			if(options.controlsShow){
				var html = options.controlsBefore;
				if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
				html += ' <div id="cntrl"><div id="'+ options.prevId +'" class="left"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></div>';
				html += ' <div id="'+ options.nextId +'" class="right"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></div></div>';
				if(options.lastShow) html += ' <div id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';
				html += options.controlsAfter;						
				jQuery(obj).after(html);										
			};
	
			jQuery("a","#"+options.nextId).click(function(){		
				animate("next",true);
			});
			jQuery("a","#"+options.prevId).click(function(){		
				animate("prev",true);				
			});	
			jQuery("a","#"+options.firstId).click(function(){		
				animate("first",true);
			});				
			jQuery("a","#"+options.lastId).click(function(){		
				animate("last",true);				
			});		
			
			function animate(dir,clicked){
				var ot = t;				
				switch(dir){
					case "next":
						if(ot<ts-5) { t += 5; }
						break; 
					case "prev":
						if(ot > 0) { t -= 5; }
						break; 
					case "first":
						t = 0;
						break; 
					case "last":
						t = ts;
						break; 
					default:
						break; 
				};	
				
				var diff = Math.abs(ot-t);
				var speed = diff*options.speed;						
				if(!options.vertical) {
					p = (t*w*-1);
					jQuery("ul#test",obj).animate(
						{ marginLeft: p }, 
						speed, "easeInExpo"
					);				
				} else {
					p = (t*h*-1);
					jQuery("ul#test",obj).animate(
						{ marginTop: p }, 
						speed, "easeInExpo"
					);					
				};
				
			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
			
		});
	  
	};

})(jQuery);



