// JavaScript Document
$(document).ready(function(){
	$(".zoomzoom .pop .outilszoom a.ferme").click(function(event){
       	griser(false);
	$(this).parent().parent().parent().parent().hide();
       	return false;
    }
	);
	$(".zoomzoom a.show").click(function(event){ 
		griser(true);
                if($(this).parent().parent().parent().attr('class')=="zoomzoom")
		$(this).parent().parent().parent().find('.pop').show();
                else
                  $(this).parent().parent().find('.pop').show();  
		//
       var largeur_pop = 0;          
        if($(this).parent().parent().find('.pop .image img').html() == ""){         
            largeur_pop = $(this).parent().parent().find('.pop .image img').width()+20;
         }else{
              largeur_pop = $(this).parent().parent().parent().find('.pop .image img').width()+20;
              }
         // alert("largeur_pop "+ largeur_pop );
		var largeur_fenetre = $("body").width();
		var hauteur_fenetre = $("body").height();
		var gauche = (largeur_fenetre - largeur_pop)/2;
		$(this).parent().parent().parent().find('.pop').css({ width:largeur_pop+"px", left:gauche+"px" });
    }
	);
	$(".zoomzoom a.show, .zoomzoom .popbulle").hover(function(event){
       	//
		$(this).parent().parent().parent().find('.popbulle').show();
    },
	function(event){
       	//
		$(this).parent().parent().parent().find('.popbulle').hide();
    }
	);

	$(".navdiapo .precedente a").click(function(event){
		var temoin_fin = false;
		var temoin_debut = false;
       $(this).parent().parent().parent().find('.vignettes .vignette').each(function(n){
			if($(this).is(":hidden")){
				if($(this).next().is(":visible")){
					if (temoin_debut != true){
						//alert(n);
						//s'il en reste au moins un caché au début ok on peut deplacer
						$(this).show();
						temoin_debut = true;
						$(this).nextAll().each(function(n){
							if($(this).is(":visible")){
								if (temoin_fin != true){
									if($(this).next().is(":hidden")){
										$(this).hide();
										temoin_fin = true;
									}
								}
							}
						})
						if (temoin_fin != true){
							$(this).parent().parent().find('.vignette:last').hide();
						}
					}
				}
			}

		})
       	return false;
    }
	);
	$(".navdiapo .suivante a").click(function(event){
		var temoin_fin = false;
		var temoin_debut = false;
       	$(this).parent().parent().parent().find('.vignettes .vignette').each(function(n){
			if($(this).is(":hidden")){
				if($(this).prev().is(":visible")){
					if (temoin_fin != true){
						//alert(n);
						//s'il en reste au moins un caché à la fin ok on peut deplacer
						$(this).show();
						temoin_fin = true;
						$(this).prevAll().each(function(n){
							if($(this).is(":visible")){
								if($(this).prev().is(":hidden")){
									if (temoin_debut != true){
										$(this).hide();
										temoin_debut = true;
									}
								}
							}
						})
						if (temoin_debut != true){
							$(this).parent().find('.vignette:first').hide();
						}
					}
				}
			}
		})

       	return false;
    }
	);



	$(".diaporamadossier .sommaires .sommaire .entete a").click(function(event){
		if(! $(this).parent().parent().parent().hasClass("ouvert") ){
			$(this).parent().parent().parent().parent().find(".sommaire").each(function(n){
				if( $(this).hasClass("ouvert") ){
					$(this).find(".dedans").hide();
					$(this).removeClass("ouvert");
				}
			})
			$(this).parent().parent().parent().find(".dedans").show();
			$(this).parent().parent().parent().addClass("ouvert");
			$(this).parent().parent().parent().find(".dedans .navdiapo .vignettes .vignette a").each(function(n){
				var num_vign = $(this).attr('class').substr(2,1);
				if( num_vign=="0" || num_vign=="1"){
					$(this).parent().parent().show();
				}else{
					$(this).parent().parent().hide();
				}
			})
		}
       	return false;
    }
	);
	$(".diaporamadossier .navdiapo .vignettes .vignette a").click(function(event){
		var chapitre = Number($(this).attr('class').substr(0,1));
		var numero = Number($(this).attr('class').substr(2,1));
		remplirDiaporamaChapitre(numero,chapitre);

       	return false;
    }
	);
	$(".diaporamadossier .zoom .gauche .navzoom .precedente a").click(function(event){
		var numero_ancien;
		var chapitre_ancien;
		$('.zoom .zoomzooms .chapitre').each(function(n){
			if($(this).is(":visible")){
				chapitre_ancien = n;
				$(this).find('.zoomzoom').each(function(n){
					if($(this).is(":visible")){
						numero_ancien = n;
					}
				})
			}
		})
		if(numero_ancien>0){
			var numero = numero_ancien-1;
			var chapitre = chapitre_ancien;
		}else{
			if(chapitre_ancien>0){
				var chapitre = chapitre_ancien-1;
			}else{
				var nombre_chapitres = $(".diaporamadossier .sommaires").children(".sommaire").length;
				var chapitre = nombre_chapitres-1;
			}
			var nombre_vignettes;
			//ouvrir le bon chapitre et fermer les autres
			$(".diaporamadossier .sommaires .sommaire").each(function(n){
				if($(this).hasClass(chapitre)){
					nombre_vignettes = 	$(this).find(".vignettes").children(".vignette").length;
					$(this).find(".dedans").show();
				}else{
					$(this).find(".dedans").hide();
				}
			})
			var numero = nombre_vignettes-1;
		}
		remplirDiaporamaChapitre(numero,chapitre);
       	return false;
    }
	);
	$(".diaporamadossier .zoom .gauche .navzoom .suivante a").click(function(event){
		var numero_ancien;
		var chapitre_ancien;
		$('.zoom .zoomzooms .chapitre').each(function(n){
			if($(this).is(":visible")){
				chapitre_ancien = n;
				$(this).find('.zoomzoom').each(function(n){
					if($(this).is(":visible")){
						numero_ancien = n;
					}
				})
			}
		})
		var nombre_chapitres = $(".diaporamadossier .sommaires").children(".sommaire").length;
		var nombre_vignettes_ancien_chapitre;
		$(".diaporamadossier .sommaires .sommaire").each(function(n){
			if($(this).hasClass(chapitre_ancien)){
				nombre_vignettes_ancien_chapitre = 	$(this).find(".vignettes").children(".vignette").length;
			}
		})
		if(numero_ancien<nombre_vignettes_ancien_chapitre-1){
			var numero = numero_ancien+1;
			var chapitre = chapitre_ancien;
		}else{
			if(chapitre_ancien<nombre_chapitres-1){
				var chapitre = chapitre_ancien+1;
			}else{
				var chapitre = 0;
			}
			var nombre_vignettes;
			//ouvrir le bon chapitre et fermer les autres
			$(".diaporamadossier .sommaires .sommaire").each(function(n){
				if($(this).hasClass(chapitre)){
					nombre_vignettes = 	$(this).find(".vignettes").children(".vignette").length;
					$(this).find(".dedans").show();
				}else{
					$(this).find(".dedans").hide();
				}
			})
			var numero = 0;
		}
		remplirDiaporamaChapitre(numero,chapitre);
       	return false;
    }
	);

	function remplirDiaporamaChapitre(num,chap){
		// utiliser num et chap pour aller chercher les infos sur la bonne image
		$('.zoom .zoomzooms .chapitre').each(function(n){
			if(n==chap){
				$(this).show();
				$(this).find('.zoomzoom').each(function(n){
					if(n==num){
						$(this).show();
					}else{
						$(this).hide();
					}
				})
			}else{
				$(this).hide();
			}
		})
		$('.zoom .gauche .infos .chapitre').each(function(n){
			if(n==chap){
				$(this).show();
				$(this).find('.info').each(function(n){
					if(n==num){
						$(this).show();
					}else{
						$(this).hide();
					}
				})
			}else{
				$(this).hide();
			}
		})
		$('.zoom .legendes .chapitre').each(function(n){
			if(n==chap){
				$(this).show();
				$(this).find('.legende').each(function(n){
					if(n==num){
						$(this).show();
					}else{
						$(this).hide();
					}
				})
			}else{
				$(this).hide();
			}
		})

		//met la vignette de l'image affichée en première place
		$(".diaporamadossier .sommaires .sommaire").each(function(n){
			if($(this).hasClass(chap)){
				$(this).find('.vignette a').each(function(n){
					var num_vign = Number($(this).attr('class').substr(2,1));
					var nombre_vign =$(this).parent().parent().parent().children(".vignette").length;
					//
					if(num_vign<num){
						if( num_vign < nombre_vign-2){
							$(this).parent().parent().hide();
						}else{
							$(this).parent().parent().show();
						}
					}else if(num_vign == num  || num_vign == (num+1)){
						$(this).parent().parent().show();
					}else{
						$(this).parent().parent().hide();
					}
				})
			}
		})
	}
	$(".simple .navdiapo .vignettes .vignette a").click(function(event){
		var numero = $(this).attr('class');
		remplirDiaporama(numero);
       	return false;
    }
	);
	$(".simple .zoom .gauche .navzoom .precedente a").click(function(event){
		var nombre_vignettes =  $(".navdiapo .vignettes").children(".vignette").length ;
		$('.zoom .zoomzooms .zoomzoom').each(function(n){
			if($(this).is(":visible")){
				numero_ancien = n;
			}
		})
		if(numero_ancien>0){
			var numero = numero_ancien-1;
		}else{
			var numero = nombre_vignettes-1;
		}
		remplirDiaporama(numero);
       	return false;
    }
	);
	$(".simple .zoom .gauche .navzoom .suivante a").click(function(event){
		var nombre_vignettes =  $(".navdiapo .vignettes").children(".vignette").length ;
		$('.zoom .zoomzooms .zoomzoom').each(function(n){
			if($(this).is(":visible")){
				numero_ancien = n;
			}
		})
		if(numero_ancien<(nombre_vignettes-1)){
			var numero = numero_ancien+1;
		}else{
			var numero = 0;
		}
		remplirDiaporama(numero);
       	return false;
    }
	);
	function remplirDiaporama(num){
		// utiliser num pour aller chercher les infos sur la bonne image

		$('.zoom .zoomzooms .zoomzoom').each(function(n){
			if(n==num){
				$(this).show();
			}else{
				$(this).hide();
			}
		})
		$('.zoom .gauche .infos .info').each(function(n){
			if(n==num){
				$(this).show();
			}else{
				$(this).hide();
			}
		})
		$('.zoom .legendes .legende').each(function(n){
			if(n==num){
				$(this).show();
			}else{
				$(this).hide();
			}
		})

	}

});

