/*
 * 	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 $("#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: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			controlsShow:	true,
			controlsCreate:	false,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			controlsFadeClass:	'inactive',
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			800,
			auto:			false,
			pause:			2000,
			continuous:		false,
			rows:			1,
      firstEl:   0
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this);
			var widths = new Array();
			var poss = new Array();
			var size = 0;
			var li_counter = 0;
			var li_obj = $("li", obj);
			var s = li_obj.length;
			var w = obj.width();//li_obj.width() + parseInt(li_obj.css('paddingLeft')) + parseInt(li_obj.css('paddingRight'));
			var h = li_obj.height() + parseInt(li_obj.css('paddingTop')) + parseInt(li_obj.css('paddingBottom')); 
			obj.width(obj.width()); 
			obj.height(h); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("ul", obj).width(Math.ceil(s*w/options.rows));	
			
			if(!options.vertical) $("li", obj).css('float','left');
			countWidth();
			if(options.controlsShow && options.controlsCreate){
				/*
				var html = options.controlsBefore;
				if(options.firstShow) html += '<span id="'+ options.firstId +'">'+ options.firstText +'</span>';
				html += ' <span id="'+ options.prevId +'">'+ options.prevText +'</span>';
				html += ' <span id="'+ options.nextId +'">'+ options.nextText +'</span>';
				if(options.lastShow) html += ' <span id="'+ options.lastId +'">'+ options.lastText +'</span>';
				html += options.controlsAfter;						
				$(obj).after(html);
				*/										
			};
	
			$("#"+options.nextId).click(function(){
				if (!$("#"+options.nextId).hasClass(options.controlsFadeClass))
					animate("next",true);
			});
			$("#"+options.prevId).click(function(){
				if (!$("#"+options.prevId).hasClass(options.controlsFadeClass))
					animate("prev",true);
			});	
			$("#"+options.firstId).click(function(){
				if (!$("#"+options.firstId).hasClass(options.controlsFadeClass))
					animate("first",true);
			});				
			$("#"+options.lastId).click(function(){
				if (!$("#"+options.lastId).hasClass(options.controlsFadeClass))
					animate("last",true);
			});		
			
			function animate(dir,clicked){
				var ot = t;				
        var is_animate = true;
				switch(dir){
					case "next":
						t = (ot>=ts) ? (options.continuous ? 0 : ts) : t+1;						
						break; 
					case "prev":
						t = (t<=0) ? (options.continuous ? ts : 0) : t-1;
						break; 
					case "first":
						t = 0;
						break; 
					case "last":
						t = ts;
						break; 
          case "to_el":
            t = parseInt(options.firstEl);
            is_animate = false;
            if (poss[t] >= poss[ts]) t = ts;
            break;
					default:
						break; 
				};	
				
				var diff = Math.abs(ot-t);
				var speed = diff*options.speed;						
				if(!options.vertical) {
					p = (poss[t]*-1);
          if (!is_animate)
          {
            $("ul",obj).css({ marginLeft: p });				
          }
          else
          {
            $("ul",obj).animate(
              { marginLeft: p }, 
              speed
            );				
          }
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						speed
					);					
				};
				
				setFadeClass();
				
				if(clicked) clearTimeout(timeout);
				if(options.auto && dir=="next" && !clicked){;
					timeout = setTimeout(function(){
						animate("next",false);
					},diff*options.speed+options.pause);
				};
				
			};
			function setFadeClass() {
				if(!options.continuous && options.controlsFade){					
					if(t==ts || poss[t] == poss[ts]){
						$("#"+options.nextId).addClass(options.controlsFadeClass);
						$("#"+options.lastId).addClass(options.controlsFadeClass);
					} else {
						$("#"+options.nextId).removeClass(options.controlsFadeClass);
						$("#"+options.lastId).removeClass(options.controlsFadeClass);				
					};
					if(t==0 || poss[ts] == 0){
						$("#"+options.prevId).addClass(options.controlsFadeClass);
						$("#"+options.firstId).addClass(options.controlsFadeClass);
					} else {
						$("#"+options.prevId).removeClass(options.controlsFadeClass);
						$("#"+options.firstId).removeClass(options.controlsFadeClass);
					};					
				};
			}
			
			function countWidth() {
				size = 0;
				li_counter = 0;
				$("li", obj).each(function () {
					var in_width = $(this).width() + 
					parseInt($(this).css('paddingLeft')) + 
					parseInt($(this).css('paddingRight')) + 
					parseInt($(this).css('marginLeft')) + 
					parseInt($(this).css('marginRight'));
					in_width = (in_width > 0) ? in_width : w;
					poss[li_counter] = size;		
					widths[li_counter++] = in_width;
					size += in_width;				
				});
				if (s%options.rows == 0)
				{ 
					size=Math.ceil(size/options.rows);
				} 
				else
				{
					var s_longer = s;
					var width_longer = 0;
					do {
						s_longer++;
						width_longer += widths[Math.ceil((s_longer-1)/options.rows)];
					} while (s_longer%options.rows != 0)
					size=Math.ceil((size+width_longer)/options.rows);
				}
				if (size > w && size - widths[ts] <= widths[ts])
				{
					poss[ts] = size - widths[ts];
				}
				else if (size < w)
				{
					for (var key in poss)
					{
						poss[key] = 0;
					}
				}
				$("ul", obj).width(size);
			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
		
			if(!options.continuous && options.controlsFade){					
				$("#"+options.prevId).addClass(options.controlsFadeClass);
				$("#"+options.firstId).addClass(options.controlsFadeClass);
				if (poss[ts] ==poss[0])
				{
					$("#"+options.nextId).addClass(options.controlsFadeClass);
					$("#"+options.lastId).addClass(options.controlsFadeClass);
				}
			};
			// Count widths for Chrome and Safari
			$(window).load(function(){
				countWidth();
				if (size > w)
				{
					for (i = 0; i <= ts; i++)
					{
						if (poss[i] > (size - w))
						{
							poss[i] = size - w;
							ts = i;
						}
					}
				}
				setFadeClass();
        if (parseInt(options.firstEl) > 0)
          animate("to_el", true);
			});
		});
	  
	};

})(jQuery);




