/*
 * 	Jaslide 0.1.1 - jQuery plugin
 *	written by Julien Appert
 *	http://julienappert.com
 *
 *	Copyright (c) 2010 Julien Appert (http://julienappert.com)
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	markup example for $("#slider").jaslide();
 *	
 * 	<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>
 *		</ul>
 *	</div>
 *
 */
 (function($) {

	$.fn.jaslide = function(options){
		var defaults = {
			pause:2000,
			speed:200,
			auto:false,
			width:300,
			height:200,
			prevId:'',
			nextId:'',
			posId:'',
			style:'default',
			cacheNav:false
		};
		var options = $.extend(defaults, options);  
		
		this.each(function() {  
			var obj = $(this); 		
			obj.addClass('js_active');
			var ul = obj.children('ul');
			var li = ul.children('li');
			var i = 0;
			var length = li.length - 1;
			switch(options.style){
				case 'around':
					ul.css({position:'relative',overflow:'hidden'});
					var k = 0;
					li.each(function(){
						if($(this).hasClass('current')) i = k;
						if(k == 0){
							var width = options.width;
							var height = options.height;						
							var top = (ul.height() - height)/2;
							var left = (ul.width() - width)/2;
							var zindex = 20;
							var opacity = 1;	
						}
						else if(k == 1){
							var width = options.width * 0.8;
							var height = options.height * 0.8;						
							var top = (ul.height() - height)/2;
							var left =  ul.width() - width;	
							var zindex = 10;
							//var opacity = 0.5;	
							var opacity = 0;	
						}
						else if(k == length){
							var width = options.width * 0.8;
							var height = options.height * 0.8;						
							var top = (ul.height() - height)/2;
							var left =  0;	
							var zindex = 10;
							//var opacity = 0.5;	
							var opacity = 0;	
						}
						else{
							var width = options.width * 0.6;
							var height = options.height * 0.6;						
							var top = (ul.height() - height)/2;
							var left = (ul.width() - width)/2;	
							var zindex = 5;
							var opacity = 0;	
						}
						
						$(this).css({position:'absolute', top:top+'px',left:left+'px',width:width+'px',height:height+'px',zIndex:zindex});
						$(this).find('img.normal').css({opacity:opacity});
						if(opacity == 1){
							$(this).find('img.flou').css({display:'none'});
						}
						k++;
					});					
				break;
				
				case 'show':
					obj.css({position:'relative',overflow:'hidden',width:options.width,height:options.height});
					var k = 0;
					li.css({position:'absolute',top:'0',left:'0',zIndex:1});
					li.fadeOut();
					li.each(function(){
						if(k == 0) {
							$(this).css({zIndex:10});
							$(this).fadeIn();
							$(this).addClass('current');
						}
						
						k++;
					});
				break;
				
				case 'default':
				default:
					obj.css({position:'relative',overflow:'hidden',width:options.width,height:options.height});
					var k = 0;
					var hasClass = false;
					li.each(function(){
						if($(this).hasClass('current')){
							i = k;
							hasClass = true;
						}
						$(this).css({float:'left'});
						k++;
					});
					if(hasClass == false){
						var l = 0;
						li.each(function(){
							if(l == 0) $(this).addClass('current');
							l++;
						});
					}
					var left = '-'+ i*options.width;					
					ul.css({width:li.length * options.width,height:options.height,position:'absolute',top:'0px',left:left+'px'});					
				break;
			}
			if(options.prevId.length > 0){
				$('#'+options.prevId).click(function(){	action('prev',0); clearTimeout(timeout);	});
			}
			if(options.nextId.length > 0){
				$('#'+options.nextId).click(function(){	action('next',0); clearTimeout(timeout);	});
			}
			if(options.posId.length > 0){
				var poscont = $('#'+options.posId);
				$('.jaslide-pos', poscont).each(function(index){
					if(index == 0) $(this).addClass('current');
					$(this).click(function(event){	event.preventDefault();	action('pos',index); clearTimeout(timeout);});
				});
			}	
			if(options.auto){
				var timeout = setTimeout(function(){
					action("next",0);
				},options.pause);
				obj.mouseover(function(){
					clearTimeout(timeout);
				});
				obj.mouseout(function(){
					timeout = setTimeout(function(){
						action("next",0);
					},options.pause);				
				});
			}
			if(options.cacheNav && i == 0){
				$('#'+options.prevId).addClass('cache');
			}
			
			function action(dir,index){
				var oi = i;
				switch(dir){
					case 'prev':			
						 if(i > 0)	i--; else i = length;
						if(options.cacheNav){
							$('#'+options.nextId).removeClass('cache');
							if(i == 0){
								$('#'+options.prevId).addClass('cache');
							}
						}							 
					break;
					case 'next':	
						if(options.cacheNav){
							$('#'+options.prevId).removeClass('cache');
							if(i == length - 1){
								$('#'+options.nextId).addClass('cache');
							}	
						}
						if(i < length) i++; else i = 0;
					break;
					case 'pos':
						i = index;
					break;
				};
				/*var diff = Math.abs(oi-i);
				var speed = diff * options.speed;*/					
				var speed = options.speed;

				switch(options.style){
					case 'around':
						var k = 0;
						li.each(function(){
							switch(dir){
								case 'next':
									//centre passe à gauche
									if(k == oi){
										var width = options.width * 0.8;
										var height = options.height * 0.8;						
										var top = (ul.height() - height)/2;
										var left =  0;	
										var zindex = 10;	
										//var opacity = 0.5;								
										var opacity = 0;								
									}				
									//droite passe au centre
									else if(k == i){
										var height = options.height;
										var width = options.width;
										var top = (ul.height() - height)/2;
										var left = (ul.width() - width)/2;
										var zindex = 20;
										var opacity = 1;
									}
									//arrière passe à droite
									else if( (k == i+1 && i+1 <= length) || (k == 0 && i+1 > length) ){
										var width = options.width * 0.8;
										var height = options.height * 0.8;						
										var top = (ul.height() - height)/2;
										var left =  ul.width() - width;	
										var zindex = 10;	
									//	var opacity = 0.5;								
										var opacity = 0;								
									
									}
									else{
										var width = options.width * 0.6;
										var height = options.height * 0.6;						
										var top = (ul.height() - height)/2;
										var left = (ul.width() - width)/2;	
										var zindex = 5;
										var opacity = 0;								
									}
								break;
								
								case 'prev':
									//centre passe à droite
									if(k == oi){
										var width = options.width * 0.8;
										var height = options.height * 0.8;						
										var top = (ul.height() - height)/2;
										var left =  ul.width() - width;	
										var zindex = 10;	
										//var opacity = 0.5;								
										var opacity = 0;								
									}				
									//droite passe au centre
									else if(k == i){
										var height = options.height;
										var width = options.width;
										var top = (ul.height() - height)/2;
										var left = (ul.width() - width)/2;
										var zindex = 20;
										var opacity = 1;
									}
									//arrière passe à gauche
									else if( (k == i-1 && i-1 >= 0) || (k == length && i-1 < 0) ){
										var width = options.width * 0.8;
										var height = options.height * 0.8;						
										var top = (ul.height() - height)/2;
										var left =  0;	
										var zindex = 10;	
										//var opacity = 0.5;							
										var opacity = 0;							
									
									}
									else{
										var width = options.width * 0.6;
										var height = options.height * 0.6;						
										var top = (ul.height() - height)/2;
										var left = (ul.width() - width)/2;	
										var zindex = 5;
										var opacity = 0;								
									}								
								break;
							}
							//$(this).animate({top:top+'px',left:left+'px',width:width+'px',height:height+'px',opacity:opacity},speed);
							if(opacity == 0){
								$(this).find('img.flou').css({display:'block'});
							}							
							$(this).animate({top:top+'px',left:left+'px',width:width+'px',height:height+'px'},speed);
							$(this).find('img.normal').animate({opacity:opacity},speed,function(){
								//alert($(this).css('opacity'));
								if($(this).css('opacity') == 1){
									$(this).parent().find('img.flou').css({display:'none'});
								}		
							});						
							$(this).css({zIndex:zindex});
							k++;
						});						
					break;
					
					case 'show':
						var k = 0;
						li.each(function(){
							if(k == oi){
								$(this).fadeOut(speed,function(){
									$(this).css({zIndex:1});
									if($(this).hasClass('current')) $(this).removeClass('current');
									var l = 0;
									li.each(function(){	
										if(l == i){
											$(this).css({zIndex:10});
											$(this).addClass('current');
										}
										l++;
									});
								});
							}
							if(k==i){
								$(this).fadeIn();
								$(this).css({zIndex:2});
							}
							k++;
						});
					break;
					
					case 'default':
					default:
						ul.animate({left:'-'+i*options.width},speed,function(){
							var div = $(this).parent('div');
							var slider = div.children('#slider');
							slider.children('.legende').removeClass('show');
							slider.children('.legende').addClass('hide');
							slider.children('#legende-slide-'+i).removeClass('hide');
							slider.children('#legende-slide-'+i).addClass('show');
							var m = 0;
							li.each(function(){
								if($(this).hasClass('current')) $(this).removeClass('current');
								if(m == i) $(this).addClass('current');
								m++;
							});							
						});					
					break;
				}
				
				if(options.auto){
					clearTimeout(timeout);
					timeout = setTimeout(function(){
						action("next",0);
					}, speed + options.pause);
				};	
				if(options.posId.length > 0){
					var poscont = $('#'+options.posId);
					$('.jaslide-pos', poscont).each(function(index){
						if(index == i)
							$(this).addClass('current');
						else
							$(this).removeClass('current');	
					});
				}
			}		
			
		});
	};

})(jQuery);
