// JavaScript Document
(function($) {
        // définition du plugin jQuery
        $.fn.carousel = function(p) {
                var defaults = {
					t_info : [],
					time : 5000,
					lancement : 2
				};
				
				var images = [] ;
				var imagesCharge = [] ;
				var self = this;
				var indEnCours = 0;
				var compteurImageLoading = 0;
				var flecheG = $("<div class='arrow arrow_left' style='display:none'></div>");
				var flecheD = $("<div class='arrow arrow_right' style='display:none'></div>");
				var timer = null;
				var controlEffect = false;
				var controlDebut = false;
				
				var blockFlechG = true;
				var blockFlechD = true;
				
				
				$(this).append(flecheG);
				$(this).append(flecheD);
				
				var loader = $("<div class='load' ><div class='indicateur'></div></div>");
				$(this).append(loader);
				
				
				
                p = $.extend( defaults, p);
               	if(parseInt(p.lancement) < 2){
					p.lancement = 2;
				}
				
				flecheG.click(prev);
				
				flecheD.click(next);
				
				$(this).mouseover(function(){
					if(!blockFlechG){
						flecheG.css('display','block');
					}
					if(!blockFlechD){
						flecheD.css('display','block');
					}
					
				});
				
				$(this).mouseout(function(){
					flecheG.css('display','none');
					flecheD.css('display','none');					   
				});
				
				for(var i = 0 ; i < p.t_info.length ; i++){
					
					var img = new Image();
					img.name = "image_"+i;
					
					images[img.name] = img;
					
					$(images[img.name]).load(function () {
					  	var t_indice = this.name.split("_");
						var indiceEnCours  = parseInt(t_indice[1]);
						imagesCharge[indiceEnCours] = this ;
						//imagesCharge.push(this)
						if(this.name == "image_0"){
							chargeImage(0);
							$(this).css("paddingTop",0);
							$(this).css("paddingLeft",0);
							$("#loaderCarousel").remove();
						}
					})
					.error(function () {
									 
					})
					.attr('src', p.t_info[i].img);
					
				}
					
					
					
					
				function chargeImage(indice){
					
					if(indice > imagesCharge.length-1){
						indice = 0;
					}
					//alert(imagesCharge["image_"+indice]+" "+indice);
					if(typeof(imagesCharge[indice]) != "undefined"){
						if($('div[rel="image_'+indice+'"]').length == 0){
							var parent;
							if(indice == 0){
								parent = $("<div class='panel' style='display:block;'></div>");
								parent.css("zIndex",501);
							}else{
								parent = $("<div class='panel' style='display:none;'></div>");
								parent.css("zIndex",500);
							}
							
							parent.attr("rel","image_"+indice);
							
							var descr = $("<div class='panel-overlay'>"+p.t_info[indice].descr+"</div>");
							descr.attr("rel","image_descr_"+indice);
							
							parent.html(imagesCharge[indice]);
							parent.append(descr);
						
							$(self).append(parent);
							
							
							window.setTimeout(function(){var ind = indice+1;chargeImage(ind)},3000);
							compteurImageLoading++;
							if(compteurImageLoading == p.t_info.length){
								$(".load").fadeOut();
								blockFlechD = false;
								$(".arrow arrow_right").css("display","block");
								blockFlechG = false;
								$(".arrow arrow_left").css("display","block");
								
							}else{
								var ratio = compteurImageLoading/(p.t_info.length-1);
								//alert($("#load").width());
								$(".indicateur").animate({
                                    width: ratio*$(".load").width() 
                                });
                                    	
							}
							if(p.lancement == indice){
								lanceCarousel(1);
								
							}
						}
						
					}else{
						window.setTimeout(function(){var ind = indice;chargeImage(ind)},1000);
					}
						
						
				}
				
				function lanceCarousel(sens){
					
					var indicePrev = parseInt(indEnCours+1);
					if(indicePrev > imagesCharge.length-1){
							indicePrev = 0;
					}
					
					
					
					//console.log('div[rel="image_'+(indicePrev)+'"]');
					if($('div[rel="image_'+indEnCours+'"]').length > 0 && $('div[rel="image_'+(indEnCours)+'"]').length > 0){
						if(controlEffect == false){
							controlEffect = true;
							
							
							if(sens == 1){	
								$('div[rel="image_'+indEnCours+'"]').fadeOut(p.timeEffect,function(){controlEffect = false;});
								$('div[rel="image_'+(indicePrev)+'"]').fadeIn(p.timeEffect);
								
								$('div[rel="image_descr_'+indEnCours+'"]').fadeOut(p.timeEffect/2);
								$('div[rel="image_descr_'+(indicePrev)+'"]').fadeIn(p.timeEffect/2);
								indEnCours++;
							} 
							if(sens == 2){	
								$('div[rel="image_'+indicePrev+'"]').fadeOut(p.timeEffect,function(){controlEffect = false;});
								$('div[rel="image_'+(indEnCours)+'"]').fadeIn(p.timeEffect);
								
								$('div[rel="image_descr_'+indicePrev+'"]').fadeOut(p.timeEffect/2);
								$('div[rel="image_descr_'+(indEnCours)+'"]').fadeIn(p.timeEffect/2);
								indEnCours--;
							}
							
							
							
							if(indEnCours > imagesCharge.length-1){
								indEnCours = 0;
							}
							
							if(indEnCours < 0){
								indEnCours = imagesCharge.length-1;
							}
							timer =window.setTimeout(function(){lanceCarousel(1)},p.time);
							controlDebut = true;
							return true;
						}
						return false;
					}else{
						timer =window.setTimeout(function(){lanceCarousel()},1000);	
						return true;
					}
					
					
				}
				
				function next(){
					if(controlDebut){
						clearTimeout(timer);
						lanceCarousel(1);
					}
					
				}
				
				function prev(){
					if(controlDebut){
						clearTimeout(timer);
						lanceCarousel(2);
						
					}
				}
				
               
        	return this;
        };
})(jQuery);
