var	$carteflash,
$bouzin, 
$diaporama,
$cartegmap,
/**
 * Premier élément de la liste <li> servant de limite pour le bouton de scroll montant
 */
$firstScroll,
/**
 * Dernier élément de la liste <li> servant de limite pour le bouton de scroll descendant
 */
$maxScroll,
/**
 * variables pour la gestion de la liste d'actus
 */
$actus, $actusHeight, $page;


var positionMiniActuelle = 1;
var valeurScroll = 330;//on crée une autre variable, qui est initialisée comme celle d'avant, mais elle prendre la valeur ensuite des images agrandies
var verrouNav = false;//permet de bloquer les clics successifs tant que l'action n'est pas terminée
var etatLecture = true;//connaitre l'etat de la lecture auto du diaporama

$(window).load(function(){
	projetaction();
	publications();
});

$(document).ready(function(){ 
	$('body').addClass('js');

	// Actualités cliquables accueil
	$('#actus .article').click(function(){
		$joLeLien=$(this).find('.readmore a').attr('href');
		window.location.href=$joLeLien;
	});

	// Projets en action cliquables accueil
	$('#projetaction .proj').click(function(){
		$paulLeLien=$(this).find('.blocTitre h3 a').attr('href');
		window.location.href=$paulLeLien;
	});

	// Duplication du menu contenu dans les pages
	// $('#menu').clone().after('').attr('id', 'menuB');
	$('#fiche').after($('#menu').clone().attr('id', 'menuB'));

	// Super labels
	labelForm("#re");
	labelForm("#nl");
	
	
	initBouzin();
	slideOpenProjets();
	
	listeActualites();

	$("#prevActu").addClass("prevnextbloque");
	$("#prevActu").removeAttr("href");

	unwidow(".chapeau p, h1, .titre");

});


function publications(){
	$publicationaction=$('#publication-content');
	
	$publications=$('#publication-content .public');
	$publicationIndice=0;
	if($publications.length>1){
		$publicationaction.height($publications.filter(':not(:first)').outerHeight());
		$publicationaction.css('overflow','hidden');
		
		publicationChange(0);
		$('#publications .next').click(function(e){
			e.preventDefault();
			publicationNext();
		});
		$('#publications .prev').click(function(e){
			e.preventDefault();
			publicationPrev();
		});
	}
	else{
		$('#publications .next').hide();
		$('#publications .prev').hide();
	}
}

function publicationPrev(){
	if($publicationIndice < $publications.length-1){
		publicationChange(1);
	}
}

function publicationNext(){
	if($publicationIndice > 0){
		publicationChange(-1);
	}
}
function publicationChange(indice){
	$lastP=$($publications[$publicationIndice]);
	$publicationIndice=$publicationIndice + indice;
	$p=$($publications[$publicationIndice]);
	if($publicationIndice>0){
		if(indice >0){
			$publicationaction.animate(
				{
					'scrollTop':$publicationaction.scrollTop()+indice*($lastP.outerHeight()),
					height:$p.height()
				},500);
			$("#prevPublication").removeClass("prevnextbloque");
			$("#prevPublication").attr("href","#");

			if ($publicationIndice==$publications.length-1) {
				$("#nextPublication").addClass("prevnextbloque");
				$("#nextPublication").removeAttr("href");
			}
		}
		else{
			$publicationaction.animate(
				{
					'scrollTop':$publicationaction.scrollTop()+indice*($p.outerHeight()),
					height:$p.height()
				},500);
			$("#nextPublication").removeClass("prevnextbloque");
			$("#nextPublication").attr("href","#");
		}

	}
	else{
		$publicationaction.animate(
			{
				'scrollTop':0,
				height:$p.height()
			},500);
		$("#prevPublication").addClass("prevnextbloque");
		$("#prevPublication").removeAttr("href");
		$("#nextPublication").removeClass("prevnextbloque");
		$("#nextPublication").attr("href","#");
	}
}


function projetaction(){
	$projetaction=$('#projetaction');

	$projets=$('#projets .proj');
	$projetIndice=0;
	if($projets.length>0){
		$projetaction.height($projets.filter(':not(:first)').outerHeight());
		$projetaction.css('overflow','hidden');

		projetactionChange(0)
		$('#projets .next').click(function(e){
			e.preventDefault();
			projetactionNext();
		});
		$('#projets .prev').click(function(e){
			e.preventDefault();
			projetactionPrev();
		});
	}
	if($projets.length<=1){
		$('#projets .next').hide();
		$('#projets .prev').hide();
	}
}

function projetactionPrev(){
	if($projetIndice < $projets.length-1){
		projetactionChange(1);
	}
}

function projetactionNext(){
	if($projetIndice > 0){
		projetactionChange(-1);
	}
}
function projetactionChange(indice){
	$lastP=$($projets[$projetIndice]);
	$projetIndice=$projetIndice + indice;
	$p=$($projets[$projetIndice]);

	if($projetIndice>0){
		if(indice >0){
			$projetaction.animate(
				{
					'scrollTop':$projetaction.scrollTop()+indice*($lastP.outerHeight()),
					height:$p.height()
				},500);

			$("#prevProjetAction").removeClass("prevnextbloque");
			$("#prevProjetAction").attr("href","#");

			if ($projetIndice==$projets.length-1){
				$("#nextProjetAction").addClass("prevnextbloque");
				$("#nextProjetAction").removeAttr("href");
			}
		}
		else{
			$projetaction.animate(
				{
					'scrollTop':$projetaction.scrollTop()+indice*($p.outerHeight()),
					height:$p.height()
				},500);		
			$("#nextProjetAction").removeClass("prevnextbloque");
			$("#nextProjetAction").attr("href","#");
		}
	}
	else{
		$projetaction.animate(
			{
				'scrollTop':0,
				height:$p.height()
			},500);

		$("#prevProjetAction").addClass("prevnextbloque");
		$("#prevProjetAction").removeAttr("href")
		$("#nextProjetAction").removeClass("prevnextbloque");
		$("#nextProjetAction").attr("href","#");
	}

	//  $($projets[page]).show(100);
}

function listeActualites(){
	$actus=$('#autreActus');
	if($actus){
		var $actusListe=$actus.find('ul.liste');
		var nbpp=$actusListe.attr('rel')!=undefined?parseInt($actusListe.attr('rel')):4;
		//console.log('nbpp='+nbpp);
		$actusHeight=36*nbpp;//$actusListe.find('li:first').outerHeight(true)*nbpp;
		//console.log('$actusHeight='+$actusListe.find('li:first').outerHeight(true)+'*'+nbpp+'='+$actusHeight);

		$actus.height($actusHeight+9);//parseInt($actusListe.css('margin-top'))
		$actus.css('overflow','hidden');
		var nbActus=$actus.find('li').length;
		if(nbActus>nbpp){
			$page=0;
			$pageMax=Math.ceil(nbActus/nbpp);

			$('#autreActus+.pager .prev').click(function(event){
				event.preventDefault();
				actuPage($page+1);
			});
			$('#autreActus+.pager .next').click(function(event){
				event.preventDefault();
				actuPage($page-1);
			});
		}
		else{
			$('#autreActus+.pager .prev').hide();
			$('#autreActus+.pager .next').hide();
		}
	}
	// Bloc titre pour les actualités
	if ($('#diaporama').length > 0 ){
		$("#content .blocTitre").addClass("blocTitreDiaporama");
		$("#content .blocTitre").removeClass("blocTitre");		
	}
}

function actuPage(page){
	if(page>-1 && page!=$pageMax){
		$page=page;
		$actus.find('ul').stop(true).animate({marginTop:-$page*$actusHeight},1000);
		$("#prevActu").removeClass("prevnextbloque");
		$("#prevActu").attr("href","#");
		$("#nextActu").removeClass("prevnextbloque");
		$("#nextActu").attr("href","#");
		if($page==0){
			$("#prevActu").addClass("prevnextbloque");
			$("#prevActu").removeAttr("href");
			$("#nextActu").removeClass("prevnextbloque");
			$("#nextActu").attr("href","#");
		}
		else if(page+1==$pageMax){
			$("#prevActu").removeClass("prevnextbloque");
			$("#prevActu").attr("href","#");
			$("#nextActu").addClass("prevnextbloque");
			$("#nextActu").removeAttr("href");
		}
		else{
			$("#prevActu").removeClass("prevnextbloque");
			$("#prevActu").attr("href","#");
			$("#nextActu").removeClass("prevnextbloque");
			$("#nextActu").attr("href","#");
		}
	}
}

//Affichage les label en superposition
//@param champ : Le container du groupe champ+label
function labelForm(champ){
	var $champ=$(champ);
	var $chLabel=$champ.find('label');
	var $chText=$champ.find('input');

	if ($chText.val() != ""){
		$chLabel.hide();
	};
	$chText.keypress(function(){
		$chLabel.hide();
	});

	$chText.focus(function(){
		if ($(this).val()==""){
			$chLabel.fadeTo("fast", 0.3);
		} else{
			$chLabel.hide();
		}
	});

	$chText.blur(function(){
		if ($(this).val()==""){
			$chLabel.fadeTo("fast", 1);
		} else{
			$chLabel.hide();
		}
	});   
}

//SlideUp-Down de la liste des projets
function slideOpenProjets(){
	// Liste des projets
	var openProjet='<div class="open"><a href="#" title="Déployer la liste"><img src="'+absURL+'img/nadir.png" alt="Ouvrir/fermer la liste des projets" /></a></div><!-- .open -->'
	$('#listeProjets .liste').after(openProjet);
	
	var $opener=$('#listeProjets .open a');
	if($opener){
		var $openerIcn=$('#listeProjets .open img');
		var $drawer=$('#listeProjets .liste');
		var maxHeight=$drawer.height(); // Hauteur avant celle appliquée par CSS…
		var cssHeight; // Hauteur appliquée en CSS, hauteur visible au chargement
		$drawer.state="up";
		$drawer.css('height',162);

		$opener.click(function(e){
			e.preventDefault();//arret du lien
			if ($drawer.state=="up"){
				cssHeight='162px'; //$drawer.css('height'); 
				$drawer.animate({height: maxHeight}, '2000');
				$openerIcn.addClass('close');
				$drawer.state="down";
			} else{
				$drawer.animate({height: cssHeight}, '2000');
				$openerIcn.removeClass('close');
				$drawer.state="up";
			}
		});

	}
}







function initBouzin(){
	$bouzin=$('#bouzin');
	$diaporama=$('#diaporama');
	$carteflash=$('#carteflash');
	$cartegmap=$('#cartegmap');
	
	/* menu toolbarBouzin */
	var hash=window.location.hash;
	if(hash!=''){
		if(hash=='#diaporama') $('#btnDiapo').addClass('current');
		if(hash=='#carteflash') $('#btnFlash').addClass('current');
		if(hash=='#cartegmap') $('#btnGMap').addClass('current');
	}
	else $('#toolbarBouzin li a:first').addClass('current');
		
	$('#toolbarBouzin li a').click(function(){
		$('#toolbarBouzin li a').removeClass('current');
		$(this).addClass('current');
	});
	
	
	
	
	if($carteflash.length>0){
		if($diaporama.length==0) afficherCarte(true);
		$('#btnFlash').click(function(){ afficherCarte(); return false; });
		$('#btnGMap').click(function(){ afficherGMap(); return false; });
	}

	if($diaporama.length>0){
		$('#btnDiapo').click(function(){ afficherDiaporama(); return false; });
		//$carteflash.hide(); // on cache le flash
		
		
		$diapimage=$('#diap-image');
		$diapControl=$('#diapmenu-control');

		/* Bouton de retour carte */
		if (!$diaporama.find("#returnmap p").hasClass("lienarticle")){ 
			$diaporama.find('#returnmap a').click(function(){
				afficherCarte();
				return false;
			});
		}

		var diaporamaImage='<div id="diap-grosse-image">\
		<img width="720" id="" src="" alt="" /></div>\
		<div id="diap-tool">\
		<p><span class="legende"></span>'; 
		// 
		if ($("#diaporama li.diapimage").length > 0){
			diaporamaImage+='<span class="diap-next"></span><span class="diap-prev"></span>';
		}
		diaporamaImage+='<span id="barre-nav">&#9660;</span></p></div>';
		$diapimage.append(diaporamaImage);

		$fleche=$('#barre-nav');
		$sizeState=false;
		$lectureFlash=false;

		//  Lecture-pause
		$('#diap-grosse-image img,#diap-tool').live('click',
			function(){
				//tester("video");
				togglePlay(false);
				if($lectureFlash==false){
					var rel=$('#diap-grosse-image img').attr('rel');
					if(rel!=undefined){
						$lectureFlash=true;
						$('#diap-grosse-image').html('<object width="720" height="435"><param name="movie" value="http://www.dailymotion.com/swf/video/'+rel+'?autoPlay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed type="application/x-shockwave-flash" src="http://www.dailymotion.com/swf/video/'+rel+'?autoPlay=1" width="720" height="435" allowfullscreen="true" allowscriptaccess="always"></embed></object>');
						maximizeflash();
					}
					else{
						if(!$sizeState)
							maximizeDiaporama();
						else
							minimizeDiaporama();
					}
				}
			}
		);
		
		// Commande Avance
		$('#diap-tool .diap-prev').click(function(event){
				event.stopPropagation();
				togglePlay(false);
				prevImage();		
		});
		// Commande Recule
		$('#diap-tool .diap-next').click(function(event){
				event.stopPropagation();
				togglePlay(false); 
				nextImage();
		});
		
		$listeImg=$('#diaporama').find('li');

		$maxPage=($("#diap-menu").find("ul li").length)*110;
		$listeImg =$listeImg.find('a');
		var tailleGalerie=$("#diap-menu ul").height();
		$maxScroll= $("#diap-menu ul li:first").offset().height;
		$("#diap-menu ul").height(tailleGalerie+200);

		//Place du premier élément, sert de limite pour le scroll montant
		$firstScroll=$('#diap-menu ul li:last').offset().top;		

		if($("#diaporama").height() > 530){
			$diapControl.append('<span class="prevPage" id="prevPage"></span><span class="numberPage"></span><span class="nextPage" id="nextPage"></span><span class="lecture"></span>');
			$diapControl.find('.prevPage').click(function(event){
				if(!verrouNav)
					prevPage();
			});
			$diapControl.find('.nextPage').click(function(event){
				if(!verrouNav)
					nextPage();
			});
			togglePlay(true,true);
			$diapControl.find('.lecture').click(function(){
				togglePlay();
			});
			$("#prevPage").css("visibility","hidden");
		}
		else{
			if ($("#diaporama li.diapimage").length > 0){
				$diapControl.append('<span class="lecture"></span>');
			}
			togglePlay(true,true);
			$diapControl.find('.lecture').click(function(){
				togglePlay();
			});
		}
		
		
	

		$maxImage=$("#diap-menu ul").children().length;
		if ($("#diaporama li.diapimage").length > 0){
				changeImage($('#diaporama li.diapimage:first a'));//on fixe la premiere image des miniaturescomme etant la premiere image du diaporama
			
		}
	
		$listeImg.click(function(event){//le clic sur une image entraine le changement de l'affichage de l'image principale
			event.preventDefault();
			event.stopPropagation();			
			clicImage(this);
		});
		
		$diapGrossImage=$('#diap-grosse-image img');
		$legende=$('#diap-tool .legende');
		
		$diapGrossImage.bind('load',function(){
			if($sizeState)
				maximizeDiaporama();
			else
				minimizeDiaporama();

		});
		$("#diap-menu").css({height: '400px'});
		
		$("#diap-menu .diapvideo a:first").prepend('<span class="flecheVideo"></span>');
		
		if ($("#diap-menu ul li:last").hasClass("diapvideo")){
			togglePlay(false);
			var href=$("#diap-menu ul li:first a").attr('href');
			if(/^http\:\/\/www\.dailymotion\.com/.test(href)){
				rel=/^http\:\/\/www\.dailymotion\.com\/video\/([a-z0-9]*)/.exec(href);
				if(rel != null){
					$('#diap-grosse-image').html('<object width="720" height="435"><param name="movie" value="http://www.dailymotion.com/swf/video/'+rel[1]+'"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed type="application/x-shockwave-flash" src="http://www.dailymotion.com/swf/video/'+rel[1]+'" width="720" height="435" allowfullscreen="true" allowscriptaccess="always"></embed></object>');
					maximizeflash();
				}
			}
		}
	}
}

/***
Fonction du clic sur une image miniature
2 cas :
	- img dans la fenetre active, simple changement d'image principale 
	- si l'image est dans "coupée", alors changement scroll "NEXT" (avec la verification si contient la derniere image)
**/
function clicImage(clic){
	if($("#diapmenu-control span.pause").length>0)
		togglePlay();
	if(!$(clic).hasClass('current')){
		numeroElem = $(clic).parent().attr("id").substr(5);
		nbVideos = $("#diap-menu ul li.diapvideo").length;
		nombretotalElem = $("#diap-menu ul li").length + nbVideos;
		nombreParAffichage = (valeurScroll/110)*2;
		numElem=numeroElem+nombreParAffichage;//prochain elem à atteindre
		chaine="#elem_"+numElem;
		//on cherche a savoir si ca sera un affichage en page complete ou non
		elementsEntre = nombretotalElem-numElem +1;
		reste =nombretotalElem-numeroElem-nombreParAffichage+1;
		if((positionMiniActuelle + nombreParAffichage)<=numeroElem){
			//doit faire un scroll
			changePage(true);
		}
	changeImage(clic);
	}
}

function masquerSlide(init){
	if(!init) $('#cartegmap div:first').hide();
}
function afficherDiaporama(){
	masquerSlide();
	$("#containbouzin").scrollTo($("#diaporama"),1000);
	//init_camera();
}
function afficherCarte(init){
	masquerSlide(init);
	if($('#diaporama').length>0) minimizeDiaporama();
	if($('object#carteIdn').length==0){
		//tester('Chargement carte Flash');
		swfobject.embedSWF(urlCarteFlash,"carteIdn", "960", "400", "10.0.0","expressInstall.swf", flashVars);
	}
	//$('#carteIdn').hide();
	$("#containbouzin").scrollTo($("#carteflash"),1000,{onAfter: function(){ /*$('#carteIdn',this).show();*/}});
}

function afficherGMap(){
	//$("#containbouzin").scrollTo($("#diaporama"),1000);
	masquerSlide();
	$("#containbouzin").scrollTo($("#cartegmap"),1000,{onAfter: function(){ $('#cartegmap div:first',this).show();}});
	//init_camera();
	//gmap_init();
}

function togglePlay(play,first){
	if($("#diaporama li.diapimage").length > 0){//on verifie qu'il y a des images
		if(play==undefined){
		 	if($diapControl.find('.pause').length>0)
		 		etatLecture=false;
		 	else
		 		etatLecture=true;
		 }		 

		//tester("dans le clic du lecture/pause");		 
		 if(etatLecture){


		 	$diapControl.find('.lecture').addClass('pause');

		 	if(first==undefined){
				//tester("ici");
		 		setTimeout('nextImage()',500);
		 		setTimeout('nextStep()',500);
		clearInterval($timeInterval);
		 	}
		 	else{
				//tester("la");
		 		nextStep();
		 	}
		 }
		 else{
		 	$diapControl.find('.lecture').removeClass('pause');
			clearInterval($timeInterval);
		 }
	}
	 
}

function nextStep(){
	
	$timeInterval=setInterval('nextImage()',5000);
}

/**
 * Clique sur l'ascenceur montant
 */
function nextPage(){
	changePage(true);
}

/**
 * Clique sur l'ascenceur descendant
 */
function prevPage(){
	changePage(false);
}

/*
Fait monter/descendre l'ascenceur pour le choix des images
*/
function changePage(next){
	//tester("######## changePage #######");
	chaine="azeaz";
	verrouNav = true;//on bloque l'action pour ne pas pouvoir cumuler les scrolls sans que l'effet soit fini (et toutes les variables recalculées)
	nbVideos = $("#diap-menu ul li.diapvideo").length;
	nombretotalElem = $("#diap-menu ul li").length + nbVideos;
	nombreParAffichage = (valeurScroll/110)*2;
	////tester('element par affichage' +nombreParAffichage);
	//NEXT
	if (next){
		////tester("changePage - NEXT");
		numElem=positionMiniActuelle+nombreParAffichage;//prochain elem à atteindre
		chaine="#elem_"+numElem;
		//on cherche a savoir si ca sera un affichage en page complete ou non
		elementsEntre = nombretotalElem-numElem +1;
		reste =nombretotalElem-positionMiniActuelle-nombreParAffichage+1;
		if(reste<nombreParAffichage){//pas complet
			numElem = adaptationScrollFin();
			chaine="#elem_"+numElem;
		}
		$("#diap-menu").scrollTo($(chaine),{duration: 500,offset:{top: -10},onAfter: function(){ 
			checkVisible();
			verrouNav = false;
			positionMiniActuelle =numElem;
			}
		});	
	} 
	//PREV
	else{
	//	//tester("changePage - PREV");
		numElem=positionMiniActuelle-nombreParAffichage;
		chaine="#elem_"+numElem;
	//	//tester(numElem);
		if(numElem<1){//pas complet
			numElem = 1;
			chaine="#elem_"+numElem;
		}
		$("#diap-menu").scrollTo($(chaine),{duration: 500,offset:{top: -10},onAfter: function(){ 
			checkVisible();
			verrouNav = false;
			positionMiniActuelle =numElem;
			}
		});
	}
}


//On vérifie si l'on arrive pas aux limites de l'ascenceur pour afficher les boutons de scroll
function checkVisible(){
	$("#nextPage").css("visibility","visible");
	$("#prevPage").css("visibility","visible");
	$dernierElem = $("#diap-menu ul li.diapimage:last");
	positionDernierElement = $dernierElem.position().top;
	if($("#elem_1").offset().top>9){
		$("#prevPage").css("visibility","hidden");
	}
	if(positionDernierElement>=0 && positionDernierElement<valeurScroll){
		$("#nextPage").css("visibility","hidden");
	}	
}

/*
Permet de calculer la valeur de l'animation (scroll) de la liste mes miniatures, en fonction de la taille de l'image principale (mini => 400)
*/
function calculAnimationMax(){
	hauteurImagePrincipale = $('#diap-image').height();
	//on calcule la val maxi
	calcul = Math.floor(hauteurImagePrincipale / 110);
	valeurScroll = 110*calcul; //on refixe la valeur dans la variable globale
	//tester("valeur scroll max "+valeurScroll);
}

//connaitre sur quelle image on doit pointer le scroll pour le dernier mouvement
function adaptationScrollFin(){
	nbVideos = $("#diap-menu ul li.diapvideo").length;
	nombreTotalDiap = $("#diap-menu ul li").length + (nbVideos*2); //une video = 2diaps image
	////tester("il y a  "+nombreTotalDiap+" diapos");
	////tester("valeur scroll "+valeurScroll);
	nbParVue = valeurScroll/110*2;
	////tester("il y a "+nbParVue+" diapos completes par affichage");
	reste = nombreTotalDiap%nbParVue;
	////tester(reste+" images en fin");
	numeroDerniereDiapo = $("#diap-menu ul li:last").attr("id").substr(5);
	////tester("numeroDerniere diapo "+numeroDerniereDiapo);
	if((numeroDerniereDiapo%2)==0){//si l'image est sur la deuxieme rangée, alors il existe forcement une image avant
		numeroDerniereDiapo--;
	}
	diapoSeuil = numeroDerniereDiapo-nbParVue+2;
	////tester("diapo a atteindre : "+ diapoSeuil);
	if($("#elem_"+diapoSeuil).length>0){
		return diapoSeuil;	
	}	
	else{
		return 1;	
	}		
}

function prevImage(){
	//tester("##### PREV IMAGE ######");
	var $current=$('#diap-menu ul a.current'); 
	var prevLi=$current.parent('li').prev('li.diapimage');
	var elem;
	if(prevLi.length>0){
	//	//tester("boucle 1");
		elem=prevLi.children('a');
		changeImage(elem);
		positionElement = prevLi.position().top;
		if(positionElement<0)
			changePage(false);
	}		
	else{
		////tester("boucle 2");
		elem=$('#diap-menu ul li.diapimage:last a');
		changeImage(elem);
		numElem = adaptationScrollFin();
		chaine = "#elem_"+numElem;
			$("#diap-menu").scrollTo($(chaine),{duration: 500,offset:{top: -10},onAfter: function(){ 
				checkVisible();
				verrouNav = false;
				positionMiniActuelle =numElem;
				}
			});
	}	
}

function nextImage(){//OK
 	//tester("##### NEXT IMAGE ######");
	var $current=$('#diap-menu ul a.current'); 
	var nextLi=$current.parent('li').next('li.diapimage');
	var elem;
	if(nextLi.length>0){
	//	//tester("nextImage - premiere boucle");
		elem=nextLi.children('a');
		changeImage(elem);
		//scroll du menu miniature si l'image n'est pas entiere/visible
		positionElement = nextLi.position().top;
		if(positionElement>=valeurScroll)
			changePage(true);
	}		
	else{
	//	//tester("nextImage - deuxieme boucle");
		elem=$('#diap-menu ul li.diapimage:first a');
		changeImage(elem);
		nbVideos = $("#diap-menu ul li.diapvideo").length;
		positionMiniActuelle = 1+2*nbVideos;
		chaine="#elem_"+positionMiniActuelle;
	//	//tester("chainee "+chaine);
		$("#diap-menu").scrollTo($(chaine),{duration: 500,offset:{top: -10},onAfter: function(){ 
				checkVisible();
				verrouNav = false;
				}
		});		
	}
}

/**
 * Permet d'obtenir l'id d'un élément en le transmettant en paramètre.
 * @param elem
 * @return l'id de l'élément
 */
function getIdElem(elem){
	var id=$(elem).parent().attr("id");
	id=id.split("elem_");
	return id[1];
}

function maximizeflash(){
	//tester("MaximizeFlash");
	var $height=458;
	dirFleche('up');
	$diapimage.animate({height: $height}, '2000');
	$bouzin.css('overflow','visible');
	$("#diap-menu").height($height);
}

//quand l'affichage est MAX et que l'on change d'image
function maximizeDiaporama(){
	if($diapimage.find('object').length==0){
		$sizeState=true;

		$bouzin.css('overflow','visible');
		var $height=$diapimage.find('*>*:first').height();
		if($height <= 400){
			// $height=400;
			$fleche.addClass('hidden');
		}
		else{
			$fleche.removeClass('hidden')
		}
		dirFleche('up');

		$diapimage.animate({height: $height}, '2000',function(){
			if($height<400){//on vérifie si l'image est plus petite que le minimum, et si c'est le cas, on retourne à l'affichage classique
					$("#diap-menu").height(400);
			}else{
					$("#diap-menu").height($height);
			}
		
			//Si la taille de l'image déroulée est supérieur à la taille du menu alors on dépli tout le menu 
			if ($("#diaporama").height()>=$("#diaporama ul").height()){
				$("#diap-menu ul").css("margin-top","0px");
				$("#diap-menu").scrollTo($("#elem_1"),{duration: 0,offset:{top: -10}});				
			}
			calculAnimationMax();
			checkVisible();
		});
	}
}

/** minimise le diaporama (affichage de la grande image tronquée) */
function minimizeDiaporama(){
	if($diapimage.find('object').length==0){
		$sizeState=false;
		dirFleche('down');
		var $height=$diapimage.find('img').height();

		if ($height <= 400){
			$fleche.addClass('hidden');
		} else{
			$fleche.removeClass('hidden');
		}
		$diapimage.animate({height: 400}, '2000', function() {
		    	calculAnimationMax();
				checkVisible();
		  });
	}
	$bouzin.css('overflow','hidden');
	$("#diap-menu").height('400px');

}

function changeImage(elemA){
	if($lectureFlash){
		$lectureFlash=false;
		$('#diap-tool').show();
		minimizeDiaporama();
	}
	$listeImg.removeClass('current');
	var $elemA=$(elemA);
	$elemA.addClass('current');
	if(typeof($diapGrossImage)=='undefined')
		$diapGrossImage=$('#diap-grosse-image img');

	// $diapGrossImage.unbind('load');
	$('#diap-tool').show();
	var href=$elemA.attr('href');
	if(/^http\:\/\/www\.dailymotion\.com/.test(href)){
		rel=/^http\:\/\/www\.dailymotion\.com\/video\/([a-z0-9]*)/.exec(href);
		if(rel != null){
			$('#diap-grosse-image').html('<object width="720" height="435"><param name="movie" value="http://www.dailymotion.com/swf/video/'+rel[1]+'?autoPlay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed type="application/x-shockwave-flash" src="http://www.dailymotion.com/swf/video/'+rel[1]+'?autoPlay=1" width="720" height="435" allowfullscreen="true" allowscriptaccess="always"></embed></object>');
			maximizeflash();
		}
		else{
			$('#diap-grosse-image').html('<img src="'+$elemA.attr('href')+'" width="720" alt="" />');
		}
	}
	else{
		$('#diap-grosse-image').html('<img src="'+$elemA.attr('href')+'" width="720" alt="" />');
	}

	$('#diap-grosse-image img').load(function(){
		if($sizeState)
			maximizeDiaporama();
		else
			minimizeDiaporama();
	});

	if(typeof($legende)=='undefined')
		$legende=$('#diap-tool .legende');
	$legende.html($elemA.next().text());
}

function dirFleche(direction){
	// Change la direction de la flèche up ou down pour déployer la zone visible de l’image
	// @param direction : la valeur up affiche la clèche allant vers le haut

	if (direction=="up")
		$fleche.text('▲');
	// $fleche.find('span').text('up');
	else
		$fleche.text('▼');
	// $fleche.find('span').text('down');
}

//Gestion des mots orphelins (typo) : évite d'avoir des mots seuls sur une nouvelle ligne.
function unwidow(selectedItem){
	$(selectedItem).each(function(){

		if($(this).children().length>0){
			$(this).children().each(function(){
				var wordArray=$(this).text().split(" ");
				if(wordArray.length>1){
					wordArray[wordArray.length-2]+="&nbsp;"+wordArray[wordArray.length-1];
					wordArray.pop();
					$(this).html(wordArray.join(" "));
				}
			});
		}
		else{
			var wordArray=$(this).text().split(" ");
			if(wordArray.length>1){
				wordArray[wordArray.length-2]+="&nbsp;"+wordArray[wordArray.length-1];
				wordArray.pop();
				$(this).html(wordArray.join(" "));
			}
		}
	});
}


