$(document).ready(function() {

	var nomDivHover = "main";
	if($("#gcontent").length != 0) nomDivHover = "gcontent";

	$("#"+nomDivHover).hoverIntent({    
		sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
		interval: 50, // number = milliseconds for onMouseOver polling interval    
		over: function() {
			$("#paupiere-bottom[rel!='fixe']").slideDown();
		}, 
		timeout: 100, // number = milliseconds delay before onMouseOut
		out: function() {
			$("#paupiere-bottom[rel!='fixe']").slideUp("normal");
			
		}
	});
		
	$(".main-lien").live("click", function() {
		var href = $(this).attr("href");
		if(href) window.location = href;
	});
	
	// Fonctionnement des photos / diaporama
	$("#paupiere-bottom .pagination a.photo").click(function() {
		changePhoto($(this).attr("rel"), true);
		return false;
	});
	$("#paupiere-bottom .pagination .diaporama").click(function() {
		if(IS_DIAPORAMA) {
			stopDiaporama();
		} else {
			startDiaporama();
		}
		return false;
	});
	
	// Fonctionnement du menu
	$("#menu > ul > li").each(function() {
		if($(this).attr("rel") == _RUBRIQUE) {
			$(this).find("> a").addClass("section-courante");
			if(_CATEGORIE) {
				$(this).find("> ul > li").each(function() {
					if($(this).attr("rel") == _CATEGORIE) {
						$(this).addClass("courant");
						$(this).find("> ul").show();
						$(this).find(".feuille").each(function() {
							if($(this).attr("rel") == _ITEM) {
								$(this).addClass("courant");
								$(this).find("> a").addClass("courant");
							}
						});
					} else {
						// On le replie
						$(this).find("> ul").hide();
					}
				});
			
			} else if(_ITEM) {
				// Pas de catégorie, des items directement
				$(this).find(".feuille").each(function() {
					if($(this).attr("rel") == _ITEM) {
						$(this).addClass("courant");
						$(this).find("> a").addClass("courant");
					}
				});
			}
		
		} else {
			// On le replie
			$(this).find("> ul").hide();
		}
	});
	$("#menu li.section > a").click(function() {
		$isSousListe = $(this).parent().find("> ul").length;
		if($isSousListe) {
			$(this).parent().siblings().find("> ul").slideUp("fast");
			$(this).parent().find("> ul").slideDown("fast");
			return false;
		}
	});
	
	// -------------------------
	// Au lancement de la page :
	if(_DIAPORAMA) {
		var NO_PHOTO = 1;
		changePhoto(NO_PHOTO, false);
		var IS_DIAPORAMA = false;
		startDiaporama();	
	}
	// -------------------------
	
	function changePhoto(nophoto, isManuel) {
		NO_PHOTO = nophoto;
		if(isManuel && IS_DIAPORAMA) stopDiaporama();
		
		$("#blanc").fadeIn("fast", function() {
			var photo = $("#paupiere-bottom .pagination li a.photo[rel='"+NO_PHOTO+"']").attr("href");
			if(!photo) {
				if(IS_DIAPORAMA) {
					// Cas "out of bounds"
					NO_PHOTO = 1;
					photo = $("#paupiere-bottom .pagination li a.photo[rel='1']").attr("href");
					if(!photo) {
						// Cas aucune photo
						stopDiaporama();
						return;
					}
				} else {
					// Cas impossible
					return;
				}
			}
			
			$("#main").css("background", "url("+photo+") no-repeat #000000 center center");
			$("#blanc").fadeOut("fast");
			$("#paupiere-bottom .pagination li a.photo").removeClass("courant");
			$("#paupiere-bottom .pagination li a.photo[href='"+photo+"']").addClass("courant");
			
			$("#paupiere-bottom .legende span.legende-item").hide();
			var legende = $("#paupiere-bottom .legende span.legende-item[rel='"+NO_PHOTO+"']");
			if(legende.length && legende.html().length) legende.show();
			
			if($(".paupiere-accueil .accroche").length) {
				// Gestion du cas de l'accueil (pas de pagination visible, mais un détail de la photo courante, éventuellement)
				$(".paupiere-accueil .accroche").hide();
				var legendeAccueil = $(".paupiere-accueil .accroche[rel='"+NO_PHOTO+"']");
				if(legendeAccueil.html().length != 0) {
					legendeAccueil.show();
					$("#paupiere-bottom").show();
				} else {
					$("#paupiere-bottom").hide();
				}
				var hrefLegendeAccueil = $(".paupiere-accueil .accroche[rel='"+NO_PHOTO+"']").attr("href");
				if(hrefLegendeAccueil) {
					$("#main").addClass("main-lien");
					$("#main").attr("href", hrefLegendeAccueil);
				} else {
					$("#main").removeClass("main-lien");
				}
			}
			
			var photoSuivante = $("#paupiere-bottom .pagination li a.photo[rel='"+(NO_PHOTO+1)+"']").attr("href");
			if(!photoSuivante) photoSuivante = $("#paupiere-bottom .pagination li a.photo[rel='1']").attr("href");
			if(photoSuivante) {
				$.preload([photoSuivante]);
			}
		});
	}
	
	function startDiaporama() {
		if($("#paupiere-bottom .pagination li a.photo").length < 2) {
			stopDiaporama();
			$("#paupiere-bottom .pagination").hide();
			$("#paupiere-bottom .pagination-nom").show();
		} else {
			IS_DIAPORAMA = true;
			$("#paupiere-bottom .pagination .diaporama").css("background", "url(images/pause.png) no-repeat");
			$(document).everyTime(4000, function(i) {
				NO_PHOTO++;
				changePhoto(NO_PHOTO);
			});
		}
	}
	
	function stopDiaporama() {
		$(document).stopTime();
		$("#blanc").fadeOut("fast");
		IS_DIAPORAMA = false;
		$("#paupiere-bottom .pagination .diaporama").css("background", "url(images/play.png) no-repeat");
	}
	
});