/*
 * 	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>
 *			<.new><img src="images/01.jpg" alt="" /></.new>
 *			<.new><img src="images/02.jpg" alt="" /></.new>
 *			<.new><img src="images/03.jpg" alt="" /></.new>
 *			<.new><img src="images/04.jpg" alt="" /></.new>
 *			<.new><img src="images/05.jpg" alt="" /></.new>
 *		</ul>
 *	</div>
 *
 */

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			orientation:	'', //  'vertical' is optional;
			speed: 			1500			
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			obj = $(this); 				
			var s = $(".new", obj).length;
			z = Math.ceil(s/2.);
			var w = obj.width(); 
			var h = obj.height(); 
			var ts = z-1;
			var t = 0;
			var vertical = (options.orientation == 'vertical');
			/* $("ul", obj).css('width',s*w);			 */
			if(!vertical) $(".new", obj).css('float','left');
			$(obj).after('<span id="'+ options.nextId +'"><a href=\"javascript:void(0);\"><img src="templates/images/Actualite_down.png" alt="flecheDown" style="margin-top:10px;margin-left:20px;" /></a></span><span id="'+ options.prevId +'"><a href=\"javascript:void(0);\"><img src="templates/images/Actualite_up.png" alt="flecheUp" style="margin-top:10px;margin-left:10px;" /></a></span> ');		
			$("a","#"+options.prevId).hide();
			$("a","#"+options.nextId).hide();
			
			
			var click_button= 0;
			/* reglade du temps d'attente pour le défilement */
			var time = 6000;

			/* fonction manuelle news -----------------------------------------*/				
			$("a","#"+options.nextId).click(			
			function next(){
				click_button=1;
				animate("next");
				if (t>=ts) $(this).fadeOut();
				$("a","#"+options.prevId).fadeIn();
			}
			);
			
			$("a","#"+options.prevId).click(			
			function prev (){
				click_button=1;
				animate("prev");
				if (t<=0) $(this).fadeOut();
				$("a","#"+options.nextId).fadeIn();
			}
			);	
			
			/* fonction auto news -----------------------------------------*/			
		
			
			function next(){				
				animate("next");
				if (t>=ts) $(this).fadeOut();
				$("a","#"+options.prevId).fadeIn();
			}
			
			function prev (){						
				animate("prev");
				if (t<=0) $(this).fadeOut();
				$("a","#"+options.nextId).fadeIn();
			}
			
			 for (var i=0; i<100; i++)
			{ 
				/* Next */			
				for( var g=1; g<z ; g++){		
				var time_auto_next = setTimeout(next,time*g+time*z*i*2); // 3000x1=3000 3000x2=6000 3000x3=9000  // 3000x18000=21000 24000 28000 //3000x3000x3x2x2=39000
				}		
				/* Prev */			
				for( var l=1; l<z ; l++){			
				var time_auto_prev = setTimeout(prev,time*z+time*l+time*z*i*2); //3000x3+3000=12000 15000 18000 31000 // 3000x3+3000+3000x3x1x2=30000 33000 36000
				}
			}

				
			function animate(dir){
				if(dir == "next"){
					t = (t>=ts) ? ts : t+1;	
				} else {
					t = (t<=0) ? 0 : t-1;
				};								
				if(!vertical) {
					p = (t*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p }, 
						options.speed
					);				
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
			};
			if(z>1) $("a","#"+options.nextId).fadeIn();	
		});
	  
	};

})(jQuery);
