﻿$(document).ready(function(){
	deplaceFooter();
	/**** hide h2,h3,p presentation ******/
		$("#contentMenu .partBtm .txtPresentation .partTexte").hide();
	/**********/
	showReservation();
	showPresentation();
	showPicVisiteGuidee();
	showListsPic();
	showFormContact();
	effectsMenu();
	initPopins();
});
function showReservation(){
	$("#reservation .linkReservation,#reservation .formulaireReservation .btnClose").unbind();
	$("#reservation .linkReservation,#reservation .formulaireReservation .btnClose").bind('click',function(){
		if($("#reservation .formulaireReservation").css("display")=="none"){
			$("#reservation .formulaireReservation").slideDown("slow");
		}else{
			$("#reservation .formulaireReservation").slideUp("slow");
		}
		
		return false;
	});
}
function showPresentation(){
	$("#contentMenu .partBtm .linkPresentation").unbind();
	$("#contentMenu .partBtm .linkPresentation").bind('click',function(){
			var hauteur=$(".partTexte").innerHeight();
			//alert("-----> "+hauteur);
			hauteur="-"+(hauteur-80)+"px";
		if($(this).next().find(".partTexte").css("display")=="none"){
			
			$(this).next().find(".partTexte").slideDown("slow");
			$(this).parent().parent().find(".visuPresentation").animate({
				top : hauteur
			},{duration:"slow"});
			
		}else{
			$(this).parent().parent().find(".visuPresentation").animate({
				top : "0px"
			}, {duration:"slow"});
			$(this).next().find(".partTexte").slideUp("slow");
			
		}
		
		return false;
	});
}
var indexRight=1;
function showListsPic(){
	$("#contentMenu .partBtm .txtVisiteGuidee ul li a").unbind();
	$("#contentMenu .partBtm .txtVisiteGuidee ul li a").bind('click',function(){
		var indexLi=0;
		$("#contentMenu .partBtm .txtVisiteGuidee ul li").removeClass("select");
		$(this).parent().addClass("select");
		$(".txtVisiteGuidee ul li").each(function(i,obj){
			if($(obj).hasClass("select")){
				indexLi=i;
			}
		});
		$(".partVisuel ul").fadeOut("slow");
		$(".partVisuel ul").css("left","0px");
		$(".partVisuel ul").each(function(j,objj){
			if(indexLi==j){
				$(objj).fadeIn("slow");
				indexRight=1;
			}
		});
		return false;
	});
}
function showPicVisiteGuidee(){
	$("#contentMenu .partVisuel .linkLeft,#contentMenu .partVisuel .linkRight").unbind();
	$("#contentMenu .partVisuel .linkLeft,#contentMenu .partVisuel .linkRight").bind('click',function(){
		//alert("-----> "+$(".txtVisiteGuidee ul li").length);
		var linkNav=0;
		if($(this).hasClass("linkLeft")){
			linkNav=0;
		}else if($(this).hasClass("linkRight")){
			linkNav=1;
		}
		var indexLi=0;
		$(".txtVisiteGuidee ul li").each(function (i,obj) {
			if($(obj).hasClass("select")){
				indexLi=i;
			}
		});
		$(".partVisuel ul").each(function (j,objj) {
			if(indexLi==j){
				if(linkNav==0){
					if(indexRight!=1){
						$(objj).animate({
							"left" : "+=900px"
						},{duration:"slow"});
						indexRight-=1;
					}
					
				}else if(linkNav==1){
					if(indexRight<$(objj).find("li").length){
						$(objj).animate({
								"left" : "-=900px"
						},{duration:"slow"});
						indexRight+=1;
					}
					
				}
			}
		});
		
		return false;
	});
}
function showFormContact(){
	$("#contentMenu .partBtm .linkMasquer,#contentMenu .partBtm .linkAfficher").unbind();
	$("#contentMenu .partBtm .linkMasquer,#contentMenu .partBtm .linkAfficher").bind('click',function(){
		if($(this).hasClass("linkMasquer")){
			$("#contentMenu .partBtm .contact").slideToggle("slow");
			$(this).removeClass();
			$(this).addClass("linkAfficher");
			
		}else if($(this).hasClass("linkAfficher")){
			$("#contentMenu .partBtm .contact").slideToggle("slow");
			$(this).removeClass();
			$(this).addClass("linkMasquer");
		}
		return false;
	});
}
function deplaceFooter(){
	var hHeight=$("#footer").innerHeight();
	var hHeader=$("#header").innerHeight();
	var hContent=$("#content").innerHeight();
	var hBody=$("body").innerHeight();
	//var paddingFooter=hBody-hContent+hHeight-hHeader-24;
	//$("#footer").css("bottom","-"+(hHeader+hContent+hHeight+40)+"px");
	//$("#footer").css("height",(hBody-hContent+hHeader)+"px");
	//alert("--height-> "+hHeight+"---content---> "+hContent+"--body---> "+hBody);
}
function effectsMenu(){
			
			
	//$("#content #menu ul li").prepend("<span></span>"); //Throws an empty span tag right before the a tag
	
	$("#content #menu ul li").each(function() { //For each list item...
		var linkText = $(this).find("a").html(); //Find the text inside of the a tag
		$(this).find("span").show().html(linkText); //Add the text in the span tag
	}); 
		$("#content #menu ul li").hover(function() {	//On hover...
			$(this).find("span").stop().animate({ 
				marginTop: "-20" //Find the span tag and move it up 40 pixels
			}, 200);
		} , function() { //On hover out...
			$(this).find("span").stop().animate({
				marginTop: "0" //Move the span back to its original state (0px)
			}, 200);
		});
			
}



/********************************************************/
// Init des popins
/********************************************************/
function initPopins(){
	var dim = getPageSize();
	$('.linkPopin').bind('click',function(){
		var _this = this;
		loadPopin($(_this).attr('rel'))
		$.ajax({
			type: "GET",
			url: $(_this).attr('href'),
			dataType: "html",
			success: function(data){
				try{
					showPopin(data,$(_this).attr('rel'));
				}catch(e){}
			}
		});
		return false;
	});

}
function resizeOverlayForPopin(){
	$(window).bind('resize',function(){
		resizaOverlay();
	});
	
	$(window).bind('scroll',function(){
		resizaOverlay();
	});	
}	

function resizaOverlay(cond){
	var overlay = $('#popinOverlay');
	var scroll = getPageScroll();
	//alert(hi);
	if(cond){
		overlay.css({
			opacity: 0.7,
			display: 'block'
		}).fadeIn('slow',function(){
			var hi = getHeight() + scroll[1];
			overlay.height(hi);
		});
	}else{
		var hi = getHeight() + scroll[1];
		overlay.height(hi);
	}
}

/********************************************************/
// Affichage des popins
/********************************************************/
function showPopin(data,id){
	//alert("---->"+data);
	$('#popinTemp').html(data);
	$('#popinTemp').removeAttr('style');
	$('#popinTemp').show();
	setPopinPosition(id);
	//setEventSelectLayers();
	
	
	if($("#popinOverlay").length){
		$("#popinOverlay").unbind();
		$("#popinOverlay").bind('click',function(){	
				$("#popinOverlay").fadeOut();
				$("#popinTemp").fadeOut();		 
		 });		
	}
}
function getHeight(){
	if(window.innerHeight || window.innerWidth)
		return window.innerHeight ;
	return document.documentElement.clientHeight ;
}

function refreshPositionPopin(popin){
	

	$(window).bind('resize',function(){
		var dim = getPageSize();
			popin.css({
			left: (dim[2]-popin.width())/2
		})
	});

	
}

function setPopinPosition(id){
	var dim = getPageSize();
	var scroll = getPageScroll();
	var overlay = $('#popinOverlay');
	var popin = $('#'+id);
	resizaOverlay(1);
	popin.css({
		top: scroll[1] + (dim[3]/4),
		left: (dim[2]-$(popin).width())/2,
		opacity: 1,
		display: 'block'
	}).fadeIn();
	refreshPositionPopin(popin);
	
	popin.find('p.close a').bind('click', function(){
		overlay.fadeOut();
		popin.fadeOut();		
		return false;
	});	
}

function loadPopin(id){
	if(!$('#popinOverlay').length)
		$('#main').before('<div id="popinOverlay"></div>');
			
	if(!$('#popinTemp').length)
		$('#main').before('<div id="popinTemp"></div>');
	$('#popinTemp').html("<div id='"+id+"' >Loading..</div>");
	setPopinPosition("popinTemp");			
}

function setWidthForLayers(){
	$('#onglets > li').each(function(){
		var resultshearch = $('.resultSearch > div',$(this))
		nbrItems = $('ul',resultshearch).length;
		var wiFinale = Math.ceil((nbrItems * 135.3) + ( nbrItems * 14.85));
		resultshearch.width(wiFinale);
	});
}

/*************************************************/
// Fonctions générales
/*************************************************/

// Renvoie les dimensions de la page
function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		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
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			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 = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}

