(function($) {

    $.fn.pbge_slider = function(options) { 
		
		 // définition des paramètres par défaut
		 var defaults = {
			 			  effect: 'Fade',
						  transition : 0.5,
						  timeFrame : 5,
						  description : 0,
						  controls : 1,
						  current : 0,
						  autostart : 1,
						  background : 1
						}; 
		
		// mélange des paramètres fournis et des paramètres par défaut
		var opts = $.extend(defaults, options);
		
		// récupération des images du slider
		var tabImages =  $('#wrapper_images_pbge_slider li');

		//Image affichée
		var current = opts.current;
		
		//Etat de la transition : 0 : sans transition - 1 : en cours
		var transitionState = 0;
		
		//compteur pour l'autoplay
		var autoplay;
		
		//description cachée
		var hideDesc = 0;
		
		//taille du slider
		var hSlider = $('#wrapper_images_pbge_slider').height();
		
		var tabVideosPlayer = new Array();
		
		
		function next(){
			
			if (!transitionState){
				
				transitionState = 1;
				
				var previousImage = $(tabImages[current]);
				
				if(current+1 < tabImages.length){
					var nextImage = $(tabImages[++current]);
				}else{
					var nextImage = $(tabImages[0]);
					current = 0;
				}
				
				//changement de la classe du controle
				changeControl(current);
				
				if(opts.effect == 'Fade'){
					
					nextImage.css({'display':'none','z-index':1});
					//changement du zindex de l'image courante
					previousImage.css({'z-index':0});
					
					nextImage.fadeIn(opts.transition*1000,function(){
						previousImage.css({'z-index':-1});		
						transitionState = 0;
						if(opts.autostart){
							playSlider();	
						}
					});	
				}
				
				if(opts.effect == 'Slide'){
					
					//changement du zindex de l'image courante
					previousImage.css({'z-index':0});
					
					
					//récupération de la taille de la nouvelle image
					var w = nextImage.width();
					var wp = -previousImage.width();				
				
					//placement de la nouvelle image en prévision du scroll
					nextImage.css({'z-index':1,left:w+'px'});
					
					//scroll
					previousImage.animate({left:wp},opts.transition*1000);
					nextImage.animate({left:0},opts.transition*1000,function(){
						//on passe l'image précédente a z-index -1
						previousImage.css({'z-index':-1});		
						transitionState = 0;
						if(opts.autostart){
							playSlider();	
						}
					});
				}
				
				if(opts.background){
					changeFond();
				}
				
				moveCursor();
				
			}			
		}
		
		function prev(){
			if (!transitionState){
				
				transitionState = 1;
				
				var previousImage = $(tabImages[current]);
				
				if(current-1 > 0){
					var nextImage = $(tabImages[--current]);
				}else{
					var nextImage = $(tabImages[tabImages.length-1]);
					current = tabImages.length-1;
				}
				
				//changement de la classe du controle
				changeControl(current);
				
				if(opts.effect == 'Fade'){
					
					nextImage.css({'display':'none','z-index':1});
					//changement du zindex de l'image courante
					previousImage.css({'z-index':0});
					
					nextImage.fadeIn(opts.transition*1000,function(){
						previousImage.css({'z-index':-1});		
						transitionState = 0;
						if(opts.autostart){
							playSlider();	
						}
					});	
				}
				
				if(opts.effect == 'Slide'){
					
					//changement du zindex de l'image courante
					previousImage.css({'z-index':0});
								
					//récupération de la taille de la nouvelle image
					var w = -nextImage.width();
					var wp = previousImage.width();
				
					//placement de la nouvelle image en prévision du scroll
					nextImage.css({'z-index':1,left:w+'px'});
					
					//scroll
					previousImage.animate({left:wp},opts.transition*1000);
					nextImage.animate({left:0},opts.transition*1000,function(){
						//on passe l'image précédente a z-index -1
						previousImage.css({'z-index':-1});		
						transitionState = 0;
						if(opts.autostart){
							playSlider();	
						}
					});
				}
				
				if(opts.background){
					changeFond();
				}
				
				moveCursor();
				
			}	
		}
		
		function changeImage(nbImage){
			if (!transitionState && nbImage != current){
				
				transitionState = 1;
			
				var previousImage = $(tabImages[current]);
				
				var nextImage = $(tabImages[nbImage]);				
				
				//changement de la classe du controle
				changeControl(nbImage);
				
				if(opts.effect == 'Fade'){
					
					nextImage.css({'display':'none','z-index':1});
					//changement du zindex de l'image courante
					previousImage.css({'z-index':0});
					
					nextImage.fadeIn(opts.transition*1000,function(){
						previousImage.css({'z-index':-1});		
						transitionState = 0;
						if(opts.autostart){
							playSlider();	
						}
					});	
				}
				
				if(opts.effect == 'Slide'){
					
					//changement du zindex de l'image courante
					previousImage.css({'z-index':0});
				
					//récupération de la taille de la nouvelle image
					if(nbImage < current){
						var w = -nextImage.width();
						var wp = previousImage.width();
					}else{
						var w = nextImage.width();
						var wp = -previousImage.width();
					}
				
				
					//placement de la nouvelle image en prévision du scroll
					nextImage.css({'z-index':1,left:w+'px'});
					
					//scroll
					previousImage.animate({left:wp},opts.transition*1000);
					nextImage.animate({left:0},opts.transition*1000,function(){
						//on passe l'image précédente a z-index -1
						previousImage.css({'z-index':-1});		
						transitionState = 0;
						if(opts.autostart){
							playSlider();	
						}
					});	
				}
				
				current = nbImage;			
				
				if(opts.background){
					changeFond();
				}
				
				moveCursor();
				
			}
		}
		
		function changeFond(){
			document.body.style.backgroundImage="url(/media/img-cabi/cover/fond_"+current+".jpg)";
		}
		
		function moveCursor(){
			$('#cursor_pbge_slider').stop().animate({'left':current*$('#cursor_pbge_slider').width()},250);
		}
		
		function playSlider(){
			window.clearTimeout(autoplay);
			autoplay = window.setInterval(function(){next();play=1},opts.timeFrame*1000);
		}
		
		function changeControl(num){
			$('#wrapper_controls_pbge_slider div').each(function(el){
				$(this).removeClass('item_control_pbge_slider_selected').addClass('item_control_pbge_slider');
				
				if(el == num){
					$(this).removeClass('item_control_pbge_slider').addClass('item_control_pbge_slider_selected');
				}
			});
		}
		
		function deactivateVideos(){

			try{
				for ( var id in players ) {
					document.getElementById(players[id]).sendEvent('STOP');
				}
			}catch(err){}
			
			$('#wrapper_images_pbge_slider').animate({'height': hSlider+'px'});
			$('#pbge_slider').animate({'height': hSlider+'px'});
			
			$('.pbge_slider_video_player').each(function(){
				var h = $(this).height();									 
				$(this).animate({'height': hSlider},500,function(){
					$(this).css({'height':h,'display':'none'});									   
				});
			});			
			transitionState = 0;
			
			$('#wrapper_controls_pbge_slider').show();
			$('#wrapper_cursor_pbge_slider').show();
		}
		
		function activateVideos(){
			$('.pbge_slider_video').each(function(el){
				
				$(this).css({'cursor':'pointer'});
				
				$(this).click(function(){
					transitionState = 1;
					
					$('#wrapper_controls_pbge_slider').hide();
					$('#wrapper_cursor_pbge_slider').hide();
					
					var divVideo = $(this).prev();
					
					var h = divVideo.height();
					
					divVideo.show();
					
					if(h > hSlider){
						$('#wrapper_images_pbge_slider').animate({'height': h+'px'});
						$('#pbge_slider').animate({'height': h+'px'});
					}
					
					$('#pbge_slider_video_fermeture').remove();	
					
					var divFermeture = document.createElement('div');
					
					$(divFermeture).attr('id','pbge_slider_video_fermeture').click(function(){
																					deactivateVideos();
																				 });
					divVideo.append(divFermeture);
					
				});									  
			});
		}
		
		function init(){
			
			
			
			//affichage des boutons de controle (un par image)
			if(opts.controls){
				
				var divWrapperCursor = document.createElement('div');
				$(divWrapperCursor).attr({'id':'wrapper_cursor_pbge_slider'});
				var divCursor = document.createElement('div');
				$(divCursor).attr({'id':'cursor_pbge_slider'});
				
				var divControls = document.createElement('div');
				$(divControls).attr({'id':'wrapper_controls_pbge_slider'});
				
				//ajout de la class pbge_slider_first au premier selectionné
				$(tabImages[opts.current]).addClass('pbge_slider_first');
				
				var i =0
				$(tabImages).each(function(el){
					var n = i;
					var div = 	document.createElement('div');
					$(div).addClass('item_control_pbge_slider').append('&nbsp;');
					$(divControls).append(div);
					$(div).click(function(){changeImage(n)});
					i++;
				});	
				$(divWrapperCursor).append($(divCursor));
				$('#wrapper_images_pbge_slider').append($(divWrapperCursor));
				$('#wrapper_images_pbge_slider').append($(divControls));
				
				changeControl(current);				
			}
			
			//lancement de l'autoplay si demandé
			if(opts.autostart){
				playSlider();	
			}
			
			$('#wrapper_images_pbge_slider').mouseover(function(){
																
				//on arret le timer
				window.clearTimeout(autoplay);
				
				//affichage de la description
				$('.wrapper_description').each(function(){
					$(this).stop().animate({'height':'60px'});
				});
				
			}).mouseout(function(){
				
				//relance du timer si nécessaire
				if(opts.autostart){
					playSlider();	
				}
				
				//on cache la description
				$('.wrapper_description').each(function(){
					$(this).stop().animate({'height':'0px'});
				});
			});
			
			//on bouge le curseur sur le premier
			moveCursor();	
			
			//on change le fond
			if(opts.background){
				changeFond();
			}
			
			//récupération des videos
			activateVideos();		
			
		}
		
		init();
		
		return this;
	}
})(jQuery);

function playerReady(obj) {
	var id = obj['id'];
	players.push(id);
}
