/**
 * simpleTabs ( http://supercanard.phpnet.org/jquery-test/simpleTabs/ )
 * plugin jQuery pour afficher des bôites d'onglet.
 * 
 * Version 1.0
 *
 * Auteur : Jonathan Coulet ( j.coulet@gmail.com )
 * 
 **/
(function($){
	$.fn.simpleTabs = function(option){
		// Param plugin
		var param = jQuery.extend({
			fadeSpeed: "low", // @param : low, medium, fast
			defautContent: 1, // @param : number ( simpleTabs-nav-number)
			autoNav: "false", // @param : true or false
			closeTabs : "true" // @param : true or false;
		}, option);
		
		$(this).each(function() {
			// Initialisation
			var $this = this;
			var $thisId = "#"+this.id;
			var nbTab = $($thisId+" > div").size();
			//autoNav();
			//showCloseTabs();
			//hideAll();
			//changeContent(param.defautContent);
			
			
			/*MODIFS DAVID*/
			
			//Get size of the image, how many images there are, then determin the size of the image reel.
			
			$(".simpleTabs-nav li").each(function(i){
				$(this).animate({'opacity':'1'},500*i,"linear");
			}); 
			
			
			
			
			var imageWidth = $(".image_reel").width();
			
			var imageSum = $(".image_reel img").size();
			var imageReelWidth = imageWidth * imageSum;

			//Adjust the image reel to its new size
			$(".image_reel").css({'width' : imageReelWidth});
			//$(".image_reel").css({'width' : '3375px'});
			
			
			
			$('.simpleTabs-nav li:first').addClass('actif');
			//Paging  and Slider Function
			rotate = function(){
			    var triggerID = $active.attr("rel") - 1; //Get number of times to slide
			    //var triggerID = 4; //Get number of times to slide
			    var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
			    
			    $(".simpleTabs-nav li").removeClass('actif');//Remove all active class
			    $active.addClass('actif'); //Add active class (the $active is declared in the rotateSwitch function)

			    //Slider Animation
			    $(".image_reel").animate({
			        left: -image_reelPosition
			    }, 500);

			}; 

			var play;
			//Rotation  and Timing Event
			rotateSwitch = function(){
			    play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
			        $active = $('.simpleTabs-nav li.actif').next(); //Move to the next paging 
			        
			        if ( $active.length === 0) { //If paging reaches the end...
			            $active = $('.simpleTabs-nav li:first'); //go back to first
			        }
			        rotate(); //Trigger the paging and slider function
			    }, 5000); //Timer speed in milliseconds (7 seconds)
			};

			////////////////////////////////////////////
			//fonction à activer pour lancer la rotation
			rotateSwitch(); //Run function on launch
			////////////////////////////////////////////
			
			//on Hover sur la Grande Image
			$(".image_reel a").hover(function() {
				if(play)
			    clearInterval(play); //Stop the rotation
			}, function() {
			    rotateSwitch(); //Resume rotation timer
			});
			
			//On Hover sur les vignettes
			$(".simpleTabs-nav li").hover(function() {
			    $active = $(this); //Activate the clicked paging
			    //Reset Timer
				if(play){
			    clearInterval(play); //Stop the rotation
				}
			    rotate(); //Trigger rotation immediately
			    //rotateSwitch(); // Resume rotation timer
			    return false; //Prevent browser jump to link anchor
			});
			//On Click sur les vignettes
			$(".simpleTabs-nav li").each(function(i) {
				var imgTab = new Array();
				$(".image_reel a").each(function(i) {
					imgTab[i] = $(this).attr("href");
				});
				$(this).click(function(){
					window.open(imgTab[i],'_self');
					return false;
				});
			});
			
			
			
			
			/*-----------------------------------------------*/
			
			
			
			
			// Fonctions
			function hideAll(){
				// Masque tous les content
				$($thisId+" .simpleTabs-content").hide();
			}
			
			
			
			
			function changeContent(indice){
				// Masque tous les content - Supprime la classe actif de tous les onglets 
				// Ajoute la classe actif à l'onglet cliqué - Affiche le content ciblé - Execute showCloseTabs
				hideAll();
				
				$($thisId+" .simpleTabs-nav li").removeClass("actif");
				if (!$($thisId+" #simpleTabs-content-"+indice).hasClass("video")) {
				
					$($thisId+" #simpleTabs-nav-"+indice).addClass("actif");
				}else{
					$($thisId+" #simpleTabs-content-"+indice).css({'position':'absolute','z-index':'2'});
				}
				$($thisId+" #simpleTabs-content-"+indice).fadeIn(param.fadeSpeed);
				//$($thisId+" #simpleTabs-content-"+indice).show();
				
				showCloseTabs();
			}
			function autoNav(){
				// Génère les onglets automatiquement
				if(param.autoNav == "true"){
					var listeNav = '';
					for(i=1; i!=nbTab; i++){
						listeNav = listeNav+'<li id="simpleTabs-nav-'+i+'">'+i+'</li>';
					}
					$($thisId+" .simpleTabs-nav").append('<ul>'+listeNav+'</ul>');
				}
			}
			function showCloseTabs(){
				// Génére un bouton de fermeture générale des content
				if(param.closeTabs == "true"){
					if($($thisId+" .simpleTabs-nav li.close").size() == 0){
						$($thisId+" .simpleTabs-nav ul").append("<li title=\"Fermer tous les onglets\" class=\"close\">x</li>");
					}
				}
			}
			// Exec
			$($thisId+" .simpleTabs-nav li").click(function(){
				var numContent = this.id.substr(this.id.length-1,this.id.length);
				changeContent(numContent);
				num = parseInt(numContent)+1;
			});
			
			var num = 2;
			function change(){
					changeContent(num);
					num++;
					if(num>5){
						num = 1;
					}
			}
			//setInterval(change,7000);
			
			
			// test function closeTabs
			$($thisId+" .simpleTabs-nav li.close").click(function(){
				hideAll();
				$($thisId+" .simpleTabs-nav li").removeClass("actif");
				$($thisId+" .simpleTabs-nav li.close").remove();
				//alert($($thisId+" .simpleTabs-nav li.close").size());
			});
		});
	}
})(jQuery);

