var current_rel = "";

$(document).ready(function() {
	if (jQuery.browser.mozilla) {
		$("a.museo").mouseout(function() {
			Cufon.refresh("a.museo");
		});
	}
	activerAccordeonMenu();
	activerCollapse();
	
	Cufon.replace('h1,h2,h3,ol,#menu ul li ul li',{fontFamily: 'Museo-500', hover: true});
	Cufon.replace('a.museo',{fontFamily: 'Museo', hover: true});
});

function activerCollapse(){
	$(".collapse").each(function(){
		$(this).css({display:"none"});
		$(this).prev("h2, h3").addClass("click");
		$(this).prev("h2, h3").append("<span></span>");
		$(this).prev("h2, h3").click(function(){
			$(this).next(".collapse").slideToggle("slow");
			$(this).toggleClass("actif");
		});
	});
}

function activerAccordeonMenu(){
	$("#menu ul ul").not($("#menu ul .sectionmenuselect ul, .menu-tertiaire")).css("display", "none");
	
	$("#accueil #menu").css("height", $("#menu").height());
	$("#accueil #menu>ul").css({position:"absolute", left:-($("#menu").width()*2)});
	$("#accueil #menu>ul").animate({left:0}, 800, function(){
		$("#accueil #menu").css("height", "auto");
		$("#accueil #menu>ul").css({position:"relative", left:"auto", height:"auto"});
	});
	
	/*$("#menu>ul>li:has(ul)>a").not($("#menu>ul>li.sectionmenuselect>a")).each(function(){
		$(this).click(function(){
			$(this).parent("li").find("ul").slideToggle("slow");
			$(this).parent("li").toggleClass("sectionmenuselect");
			$(this).blur();
			return false;
		});
	});*/
	
	$("#menu>ul>li:has(ul)>a").each(function(){
		$(this).click(function(){
			if($("#menu>ul>li.sectionmenuselect ul").css("display") == "block"){
				$("#menu>ul>li.sectionmenuselect>ul").slideUp("slow");
				$("#menu>ul>li.sectionmenuselect").removeClass("sectionmenuselect");
			}
			
			if($(this).parent("li").find("ul").css("display") == "block"){
				$(this).parent("li").find(">ul").slideUp("slow");
				$(this).parent("li").removeClass("sectionmenuselect");
			} else {
				$(this).parent("li").find(">ul").slideDown("slow");
				$(this).parent("li").addClass("sectionmenuselect");
			}
			Cufon.refresh("a.museo");
			$(this).blur();
			return false;
		});
	});
	
	$("#menu>ul>li:has(ul).sectionmenuselect>a").each(function(){
		$(this).click(function(){
			$(this).blur();
			return false;
		});
	});
}

function activerTopLayer() {
	$("a.pop-photo").click(function(){
		afficherOverlay();
		initTopLayer($(this));
		$(this).blur();
		return false;
	});
}

function initTopLayer(__el) {
	$("body").append("<div id=\"lightbox\"><p id=\"bt-fermer-lightbox\"><a href=\"#\"></a></p><div id='lightbox-contenu'></div></div>");
	var type = __el.attr("class");
	var image = __el.attr('href');
	current_rel = __el.attr("rel");
	
	switch (type) {
		case "pop-photo":
			$("#lightbox").addClass("images");
			if(current_rel != ""){
				$("#lightbox").append("<p class=\"bt-precedent\"><a href=\"#\"></a></p><p class=\"bt-suivant\"><a href=\"#\"></a></p>");
				$("#lightbox .bt-suivant a").click(function(){
					chargerNextMedia(__el, 1);
					$(this).blur();
					return false;
				});
				$("#lightbox .bt-precedent a").click(function(){
					chargerNextMedia(__el, -1);
					$(this).blur();
					return false;
				});
			}
			
			if(type == "pop-photo"){
				$("#lightbox-contenu").append("<p><img src='"+image+"' alt='' id='lightbox-photo' /></p>");
				if(__el.attr("title") != ""){
					$("#lightbox-contenu").append("<span class='legende'></span>");
				}
				//$("#lightbox").css({display:"block"});
				positionTopLayer();
				/*
				$("#lightbox-photo").load(function(){
					$("#lightbox-contenu .legende").html(__el.attr("title"));
					positionTopLayer();
					$("#lightbox").css("display", "block");
					$("#loading").css("display", "none");
				});
				*/
				chargerPhoto($("a[rel='" + current_rel + "']:eq(" + $("a[rel='" + current_rel + "']").index(__el) + ")"));
			}
			break;
			
		default: 
			// rien
			break;
	}
	
	$("#bt-fermer-lightbox").click(function(){
		$("#overlay").click();
		$(this).blur();
		return false;
	});
	
	$(document).keyup(function(event){
		if (event.keyCode == 27) {
			$("#overlay").click();
		}
	});
}

function chargerNextMedia(__el, __dir) {
	
	var next_media = current_media + __dir;
	var type = $("a[rel='" + current_rel + "']:eq(" + next_media + ")").attr("class");
	if(type == "pop-photo"){
		chargerPhoto($("a[rel='" + current_rel + "']:eq(" + next_media + ")"));
	}
	if(type == "pop-video"){
		chargerVideo($("a[rel='" + current_rel + "']:eq(" + next_media + ")"));
	}
	
	if(type == "pop-concours"){
		chargerConcours($("a[rel='" + current_rel + "']:eq(" + next_media + ")"));
	}
}

/* TOP LAYER PHOTO */
function chargerPhoto(__el) {	
	if(($("a[rel='" + current_rel + "']").index(__el) >= 0)&&($("a[rel='" + current_rel + "']").index(__el) <= $("a[rel='" + current_rel + "']").length-1)) {
		$("#lightbox").css("display", "none");
		$("#loading").css("display", "block");
		$("#lightbox-contenu").empty();
		$("#lightbox-contenu").append("<img src='' alt='' id='lightbox-photo' />");
		if(__el.attr("title") != ""){
			$("#lightbox-contenu").append("<span class='legende'></span>");
		}
		
		var fichier = __el.attr("href");
		current_media = $("a[rel='" + current_rel + "']").index(__el);
		checkFleches();
		
		$("#lightbox-photo").attr("src", fichier);
		$("#lightbox-photo").load(function(){
			$("#lightbox-contenu .legende").html(__el.attr("title"));
			positionTopLayer();
			$("#lightbox").css("display", "block");
			$("#loading").css("display", "none");
		});
	}
}

function checkFleches() {
	if(current_media <= 0){
		$("#lightbox .bt-precedent").css({display:"none"});
	} else {
		$("#lightbox .bt-precedent").css({display:"block"});
	}
		
	if(current_media >= $("a[rel='" + current_rel + "']").length-1){
		$("#lightbox .bt-suivant").css({display:"none"});
	} else {
		$("#lightbox .bt-suivant").css({display:"block"});
	}
}

function positionTopLayer() {
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var largeurLightbox = $("#lightbox").width();
	var hauteurLightbox = $("#lightbox").height();
	var marginTop = arrayPageScroll[1] + -(hauteurLightbox/2);
	//marginTop = arrayPageScroll[1] + 20;
	var marginLeft = -(largeurLightbox/2);
	
	$("#lightbox").css("margin-top", marginTop);
	$("#lightbox").css("margin-left", marginLeft);
}

function cacherTopLayer() {
	$(document).unbind("keyup");
	$("#lightbox a").unbind("click");
	$("#lightbox").remove();
}


/*--------------------------------------------------------------
OVERLAY (Bloque l'écran)
--------------------------------------------------------------*/
function afficherOverlay() {
	$("body").append("<div id=\"overlay\"></div><div id=\"loading\"></div>");
	var arrayPageSize = getPageSize();
	var hauteurOverlay = arrayPageSize[1];
	
	$("#overlay").css("height", hauteurOverlay);
	$("#overlay").css("opacity", "0.7");
	$("#overlay").css("display", "block");
	$("#loading").css("display", "block");
	
	$("#overlay").click(function(){
		cacherTopLayer();
		cacherOverlay();
		$(this).blur();
		return false;
	});
	
	if(($.browser.msie)&&($.browser.version < 7)) {
		visibilitySelects("hidden");
	}
}

function cacherOverlay() {
	$("#overlay").remove();
	$("#loading").remove();
	
	if(($.browser.msie)&&($.browser.version < 7)) {
		visibilitySelects("visible");
	}
}

function visibilitySelects(visibility){
	$("select").each(function(i){
		$(this).css("visibility", visibility);
	});
}


/**********************************************************************
DIMENSIONS DE L'ÉCRAN
**********************************************************************/
function getPageScroll(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
