	
	var inAction = false;
	
	
	var timer = 0;
	var timer_buffer = 0;
	
	var tab_visibles = new Array();
	
	var lastClickedLink = "";
	
		
	var tab_cachees = new Array();
		
	var objets_caches = new Array();
	var tuiles = new Array();
	
	var setFlash = true;
	
	var random_entrante = "",last_random_entrante = "",random_sortante = "",last_random_sortante = "";
	
	// table des actions en cours
	var tab_actions = new Array();
	
	// Savoir si le timer est en roulement
	var timerRunning = false;
	
	function addTabAction() {
		inAction = true;
		tab_actions.push("1");
	}
	
	function removeTabAction() {
		tab_actions.shift();
		if (tab_actions.length == 0) {
			inAction = false;
			
			if (lastClickedLink) lastClickedLink.click();
		}
	}
	
	function checkActions() {
		if (!inAction) {
			clearInterval(timer_buffer);
			timerRunning = true;
			tuiles_switch();
			timer = setInterval('tuiles_switch()', 1400);
		}
	}
	
	function tuiles_switch(algo) {
		
		if (!inAction) {
		
			if (!algo) {
				// Mécanique de switch
				tuiles = algo_switch();
			}
			
			// Effet visuel
			a_sortant = $("#mosaique li").children("a#tuile_" + tuiles["sortante"].getId());
			
			objets_caches["tuile_" + tuiles["sortante"].getId()] = new Array();
			objets_caches["tuile_" + tuiles["sortante"].getId()]["obj_cache"] = $("#tuile_" + tuiles["entrante"].getId()).parent("li");
			objets_caches["tuile_" + tuiles["sortante"].getId()]["a_sortant"] = a_sortant;
			
			objet_cache = $("#tuile_" + tuiles["entrante"].getId()).parent("li");
			html_entrant = objet_cache.html();
			
			inAction = true;
			
			objet_cache.empty();
			
			// Ramène les 2 liens dans le même li
			$(a_sortant).parent("li").html($(a_sortant).parent("li").html() + html_entrant);
			
			addTabAction();
			$("#mosaique li").children("a#tuile_" + tuiles["entrante"].getId()).fadeIn(1200,function(){
				removeTabAction();
			});
			
			addTabAction();
			$("#mosaique li").children("a#tuile_" + tuiles["sortante"].getId()).fadeOut(1200,function() {
				var id = $(this).attr("id");
				$(this).remove();
				objets_caches[id]["obj_cache"].html(objets_caches[id]["a_sortant"]);
				objets_caches[id]["obj_cache"].children("a").css("display","none");
				removeTabAction();
			});
		
		} else if (timerRunning) {
			clearInterval(timer);
			timer_buffer = setInterval("checkActions()",300);
		}
		
		return false;
	}
	
	function algo_switch() {
		
		// Choisir une tuile sortante
		nb_visibles = tab_visibles.length;
		if (random_sortante) {
			// On sort un random différent des deux dernires images fades
			while((tab_visibles[random_sortante].getId() == last_random_sortante) || (tab_visibles[random_sortante].getId() == last_random_entrante)) {
				random_sortante = Math.round(Math.random() * (nb_visibles -1));
			}
		} else {
			random_sortante = Math.round(Math.random() * (nb_visibles -1));
		}
		
		tuile_sortante = tab_visibles[random_sortante];
		
		// Choisir une tuile entrante
		nb_cachees = tab_cachees.length;
		
		if (random_entrante) {
			// On sort un random différent des deux dernires images fades
			while((tab_cachees[random_entrante].getId() == last_random_sortante) || (tab_cachees[random_entrante].getId() == last_random_entrante)) {
				random_entrante = Math.round(Math.random() * (nb_cachees-1));
			} 
		} else {
			random_entrante = Math.round(Math.random() * (nb_cachees-1));
		}
		
		// Pour ne pas utiliser les deux dernieres images fades, on les met en variable
		last_random_entrante = tab_cachees[random_entrante].getId();
		last_random_sortante = tab_visibles[random_sortante].getId();
		
		tuile_entrante = tab_cachees[random_entrante];
		
		// Switch de la sortante et de l'entrante dans l'array des visibles
		tab_visibles.splice(random_sortante,1,tuile_entrante);
		
		// Enlever l'entrante de l'array des cachées
		tab_cachees.splice(random_entrante,1);
		
		// Ajouter la sortante dans le buffer
		tab_cachees.push(tuile_sortante);
		
		tuiles["sortante"] = tuile_sortante;
		tuiles["entrante"] = tuile_entrante;
		
		return tuiles;
	}
		
	var timer_document_ready = 0;
	
	$(document).ready(function() {
		if (!$.browser.safari) document_ready();
	});
	
	function document_ready() {
		
				inAction = true;
				
		// Sur le click d'une categorie, slideUp des blocs ouverts et slideDown de celui clique
		$("dl.equipe_categorie dt a").click(function(){
			
			// Cache le bloc texte
			$(".equipe_navigation .texte").not(".hidden").fadeOut("fast").addClass("hidden");
			
			// slideUp seulement s'il n'est pas deja ouvert
			if (!$(this).is(".selected")) {
				$("dl.equipe_categorie dd.ouvert").slideUp("normal").removeClass("ouvert").prev().children("a").removeClass("selected");
				$(this).addClass("selected").parent().next("dd").slideDown("normal").addClass("ouvert").html();
			}
			
			return false;
		});
		
				
		// Sur le click d'une categorie/departement
		$("dl.equipe_categorie dd ul li a").not('.employes').click(function(){
			
			// Si on est pas deja dans un crossfade
			if (!inAction) {
				lastClickedLink = null;
				inAction = true;
				
				// Cache le bloc texte
				$(".equipe_navigation .texte").not(".hidden").fadeOut("fast").addClass("hidden");
				
				if (setFlash) {
					$.ajax({
						type: "POST",
						url: "/fr/equipe/setCategorie",
						data: 'id='+this.id,
						success: function(resultat){

						}
					});
				} else {
					setFlash = true;
				}
				
				// Stop l'animation de la matrice
				if (timerRunning) {
					clearInterval(timer);
					timerRunning = false;
				}
				
				// Deselectionne les categories/departements
				$(".equipe_categorie dd ul li a").removeClass("selected");
				
				// Categorie/Departement
				var obj = $(this);
				obj.addClass("selected");
				
				
				var tab_disponibles = new Array();
				
				// Loop sur les personnes visibles
				for(t in tab_visibles) {
					// Si il n'est pas dans la categorie/departement en cours, on peut switcher cette photo la
					if (!tab_visibles[t].inGroup(obj.attr("id"))) {
						tab_disponibles.push(t);
					}
				}
				
				
				// Loop sur les personnes invisibles
				for(t in tab_cachees) {
					inAction = false;
					// Si il n'est pas dans la categorie/departement en cours, on le fadeOut
					if (tab_cachees[t].inGroup(obj.attr("id"))) {
						if (tab_disponibles.length > 0) {
							
							// on le switch avec spot disponible
							tuiles["entrante"] = tab_cachees[t];
							
							// Choisir une tuile sortante
							nb_visibles = tab_disponibles.length;
							random_sortante = Math.round(Math.random() * (nb_visibles -1));
							tuiles["sortante"] = tab_visibles[tab_disponibles[random_sortante]];
							
							// Enleve la photo dans table visible et invisible
							var temp = tab_visibles[tab_disponibles[random_sortante]];
							tab_visibles.splice(tab_disponibles[random_sortante],1,tab_cachees[t]);
							tab_cachees.splice(t,1,temp);
							tab_disponibles.splice(random_sortante,1);
							
							tuiles_switch(true);
						}
					}
					inAction = true;
				}
				
				// Loop sur les personnes visibles
				for(t in tab_visibles) {
					
					// Si il n'est pas dans la categorie/departement en cours, on le fadeOut
					if (!tab_visibles[t].inGroup(obj.attr("id"))) {
						// Si il n'est pas deja fadeOut
						if (!$("#tuile_" + tab_visibles[t].getId()).is(".selected")) {
						
							$("#tuile_" + tab_visibles[t].getId()).addClass("selected");
							
							addTabAction();
							
							$("#tuile_" + tab_visibles[t].getId()).children(".over").fadeIn(1200,function(){
								removeTabAction();
								
							});
							
							addTabAction();
							
							$("#tuile_" + tab_visibles[t].getId()).children(".notOver").fadeOut(1200,function(){
								removeTabAction();
								
							});
						}
					// Si il est fadeOut
					} else if ($("#tuile_" + tab_visibles[t].getId()).is(".selected")) {
						addTabAction();
						
						$("#tuile_" + tab_visibles[t].getId()).children(".notOver").fadeIn(1200,function(){
							$(this).parent().removeClass("selected");
							removeTabAction();
							
						});
						addTabAction();
						
						$("#tuile_" + tab_visibles[t].getId()).children(".over").fadeOut(1200,function(){
							removeTabAction();
							
						});
					}
				}
				
				if (tab_actions.length == 0) inAction = false;
				
			// Si deja dans un crossFade, on set l'action dans une variable et on execute a la fin du crossFade
			} else {
				lastClickedLink = $(this);
			}
			return false;
		});
		
				
	}
	
	// Une fois que les images sont loades, on start l'animation
	$(window).load(function(){
		
		if ($.browser.safari) {
			document_ready();
		}
		
				
		inAction = false;
		
				
		timerRunning = true;
		
			// Debut de l'animation
			// Animation active seulement s'il y a plus de 28 employés
						
				
			});