/*--FUNCTIONS.JS--*/
/*Développements spécifique au site*/

/** PLUGIN JQUERY MOUSE WHEEL **/
(function(c){var a=["DOMMouseScroll","mousewheel"];c.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var d=a.length;d;){this.addEventListener(a[--d],b,false)}}else{this.onmousewheel=b}},teardown:function(){if(this.removeEventListener){for(var d=a.length;d;){this.removeEventListener(a[--d],b,false)}}else{this.onmousewheel=null}}};c.fn.extend({mousewheel:function(d){return d?this.bind("mousewheel",d):this.trigger("mousewheel")},unmousewheel:function(d){return this.unbind("mousewheel",d)}});function b(f){var d=[].slice.call(arguments,1),g=0,e=true;f=c.event.fix(f||window.event);f.type="mousewheel";if(f.wheelDelta){g=f.wheelDelta/120}if(f.detail){g=-f.detail/3}d.unshift(f,g);return c.event.handle.apply(this,d)}})(jQuery);

/** CUSTOM SCROLLING **/
function customScrolling(b,a){if(typeof(a.id)=="undefined"){a.id=""}if(typeof(a.height)=="undefined"){a.height=300}if(typeof(a.bar)=="undefined"){a.bar=new Object()}if(typeof(a.bar.width)=="undefined"){a.bar.width=10}if(typeof(a.bar.margin)=="undefined"){a.bar.margin=new Object()}if(typeof(a.bar.margin.top)=="undefined"){a.bar.margin.top=0}if(typeof(a.bar.margin.right)=="undefined"){a.bar.margin.right=0}if(typeof(a.bar.margin.bottom)=="undefined"){a.bar.margin.bottom=0}if(typeof(a.bar.margin.left)=="undefined"){a.bar.margin.left=0}if(typeof(a.cursor)=="undefined"){a.cursor=new Object()}if(typeof(a.cursor.width)=="undefined"){a.cursor.width=10}if(typeof(a.cursor.height)=="undefined"){a.cursor.height=10}if(typeof(a.cursor.margin)=="undefined"){a.cursor.margin=new Object()}if(typeof(a.cursor.margin.top)=="undefined"){a.cursor.margin.top=0}if(typeof(a.cursor.margin.right)=="undefined"){a.cursor.margin.right=0}if(typeof(a.cursor.margin.bottom)=="undefined"){a.cursor.margin.bottom=0}if(typeof(a.cursor.margin.left)=="undefined"){a.cursor.margin.left=0}$(b).each(function(){if($(this).outerHeight(true)>a.height){$(this).css("position","relative");var f=$(this).outerWidth(true);$(this).wrap('<div id="'+a.id+'" class="customScrolling" style="position:relative;overflow:hidden;width:'+f+"px;height:"+a.height+'px;" />');var c=$(this).parent();var e=a.height-a.bar.margin.top-a.bar.margin.bottom;var d='<div class="customScrollingBar" style="position:absolute;right:'+a.bar.margin.right+"px;top:"+a.bar.margin.top+"px;width:"+a.bar.width+"px;height:"+e+'px;">';d+='<div class="customScrollingBarContainment" style="position:absolute;top:0;left:'+a.cursor.margin.left+"px;width:"+a.cursor.width+"px;height:"+(e-a.cursor.margin.top-a.cursor.margin.bottom)+"px;margin-top:"+(a.cursor.margin.top)+'px;cursor:pointer;">';d+='<div class="customScrollingBarCursor" style="position:absolute;top:0;left:0px;width:'+a.cursor.width+"px;height:"+a.cursor.height+'px;cursor:pointer;">';d+="</div></div>";c.append(d);var g=$(this).outerHeight(true);var h=$(this);c.find(".customScrollingBarCursor:first").draggable({axis:"y",scroll:false,containment:"parent",drag:function(i,j){customScrollingDiv(j.helper,j.position.top)}});$(this).mousewheel(function(j,m){var l=c.find(".customScrollingBarCursor:first");var i=l.parent().innerHeight()-l.innerHeight();var k=l.position().top+(-m*30);if(k<0){k=0}if(k>i){k=i}l.css("top",k+"px");customScrollingDiv(l,k)});c.find(".customScrollingBarContainment:first").click(function(k){var l=$(this).children("div:first");var i=l.parent().innerHeight()-l.innerHeight();var j=k.pageY-$(this).offset().top-Math.floor(l.innerHeight()/2);if(j<0){j=0}if(j>i){j=i}l.animate({top:j+"px"},{duration:300,step:function(m){customScrollingDiv($(this),m)}})})}})}function customScrollingDiv(f,b){var a=f.parent().innerHeight()-f.innerHeight();var c=f.parents("div.customScrolling");var e=c.children("div:first");var d=Math.ceil(((e.outerHeight()-c.innerHeight())*b)/a);e.css("top","-"+d+"px")};

/** GREYBOX **/
var greyBoxDefaultConfig = {
	/*--Valeurs ci-dessous requises--*/
	top: 50,						//=> Décalage en PX du haut du document
	duration: 0.6,					//=> Durée totale de l'affichage
	opacity: 0.6					//=> Opacité du fond Overlay
	/*--Options supplémentaires--*/
	//height: 500,					//=> Fixer la hauteur
	//closeText: 'Retour',			//=> Ajouter un texte au bouton "fermer"
	//background: 'green',			//=> Modifier la couleur du fond de l'Overlay
	//style: 'background:red',		//=> Ajouter des styles CSS directement à la greyBox
	//className: 'customize',		//=> Ajouter une Classe CSS
	//data: {id: 12},				//=> Envoyer des données en POST
	/*customFunction: function(){	//=> Exécuter un fonction personnelle à l'affichage de la greyBox
		alert('test');
	}*/
};
//Variable définissant la durée de la transition
var greyBoxEffectDuration = Math.round((greyBoxDefaultConfig.duration*1000)/2);
//Fonctions GREYBOX
function greyBoxShow(b,c,d,a){if(!$("#overlay").length){$("body").append('<div id="overlay" onclick="greyBoxHide();"></div>');if(jQuery.browser.msie&&jQuery.browser.version=="6.0"){$(window).scroll(function(){$("#overlay").css("top",$(window).scrollTop()+"px")})}}if(!$("#greyBox").length){$("body").append('<div id="greyBox"><div class="content" id="greyBoxContent"></div><div onclick="greyBoxHide();" class="close"></div></div>')}if(typeof(a)!="undefined"){if(typeof(a.duration)=="undefined"){greyBoxEffectDuration=Math.round((greyBoxDefaultConfig.duration*1000)/2)}else{greyBoxEffectDuration=Math.round((a.duration*1000)/2)}if(typeof(a.top)=="undefined"){a.top=greyBoxDefaultConfig.top}if(typeof(a.opacity)=="undefined"){a.opacity=greyBoxDefaultConfig.opacity}if(typeof(a.height)=="undefined"&&typeof(greyBoxDefaultConfig.height)!="undefined"){a.height=greyBoxDefaultConfig.height}if(typeof(a.closeText)=="undefined"&&typeof(greyBoxDefaultConfig.closeText)!="undefined"){a.closeText=greyBoxDefaultConfig.closeText}if(typeof(a.title)=="undefined"&&typeof(greyBoxDefaultConfig.title)!="undefined"){a.title='<h1 class="title greybox">'+greyBoxDefaultConfig.title+"</h1>"}else{if(typeof(a.title)=="undefined"&&typeof(greyBoxDefaultConfig.title)=="undefined"){a.title=""}else{a.title='<h1 class="title greybox">'+a.title+"</h1>"}}if(typeof(a.error)=="undefined"&&typeof(greyBoxDefaultConfig.error)!="undefined"){a.error=greyBoxDefaultConfig.error}if(typeof(a.background)=="undefined"&&typeof(greyBoxDefaultConfig.background)!="undefined"){a.background=greyBoxDefaultConfig.background}if(typeof(a.style)=="undefined"&&typeof(greyBoxDefaultConfig.style)!="undefined"){a.style=greyBoxDefaultConfig.style}if(typeof(a.className)=="undefined"&&typeof(greyBoxDefaultConfig.className)!="undefined"){a.className=greyBoxDefaultConfig.className}if(typeof(a.loading)=="undefined"&&typeof(greyBoxDefaultConfig.loading)!="undefined"){a.loading=greyBoxDefaultConfig.loading}if(typeof(a.data)=="undefined"&&typeof(greyBoxDefaultConfig.data)!="undefined"){a.data=greyBoxDefaultConfig.data}if(typeof(a.customFunction)=="undefined"&&typeof(greyBoxDefaultConfig.customFunction)!="undefined"){a.customFunction=greyBoxDefaultConfig.customFunction}}else{greyBoxEffectDuration=Math.round((greyBoxDefaultConfig.duration*1000)/2);a=greyBoxDefaultConfig;a.title=""}$("#overlay").removeAttr("style");$("#greyBox .close").html("");$("#greyBox").removeAttr("class");$("#greyBoxContent").removeAttr("style");if(typeof(a.height)!="undefined"&&d!="iframe"&&d!="youtube"&&d!="dailymotion"){$("#greyBoxContent").css("overflow","auto").css("height",a.height+"px")}if(typeof(a.closeText)!="undefined"){$("#greyBox .close").html(a.closeText)}if(typeof(a.background)!="undefined"){$("#overlay").css("background",a.background)}if(typeof(a.style)!="undefined"){$("#greyBoxContent").attr("style",a.style)}if(typeof(a.className)!="undefined"){$("#greyBox").addClass(a.className)}if(typeof(a.onComplete)=="undefined"){a.onComplete=function(){}}$("#greyBoxContent").html('<div id="greyBoxLoader"></div>');if(typeof(a.loading)!="undefined"){$("#greyBoxLoader").html(a.loading)}$("#greyBox").css("width",c+"px").css("left","-"+(Math.ceil(c/2))+"px");if(typeof(a.customFunction)=="undefined"){switch(d){case"html":var e=function(){$("#greyBoxContent").html(a.title+b)};greyBoxDisplay(e,a.opacity,a.top,a.onComplete);break;case"get":var e=function(){$.ajax({type:"GET",url:b,success:function(f){$("#greyBoxContent").html(a.title+f)},error:function(){$("#greyBoxContent").html(a.title+a.error)}})};greyBoxDisplay(e,a.opacity,a.top,a.onComplete);break;case"post":var e=function(){$.ajax({type:"POST",url:b,data:a.data,success:function(f){$("#greyBoxContent").html(a.title+f)},error:function(){$("#greyBoxContent").html(a.title+a.error)}})};greyBoxDisplay(e,a.opacity,a.top,a.onComplete);break;case"iframe":var e=function(){var f=400;if(typeof(a.height)!="undefined"){f=a.height}$("#greyBoxContent").html(a.title+'<iframe src="'+b+'" border="0" frameborder="0" width="100%" height="'+f+'" allowTransparency="true"></iframe>')};greyBoxDisplay(e,a.opacity,a.top,a.onComplete);break;case"youtube":var e=function(){var f="";if(typeof(a.height)!="undefined"){f=' height="'+a.height+'"'}$("#greyBoxContent").html(a.title+'<iframe class="youtube" title="YouTube video player" style="width:100%;display:block;"'+f+' src="http://www.youtube.com/embed/'+b+'?wmode=transparent" frameborder="0" allowfullscreen></iframe>')};greyBoxDisplay(e,a.opacity,a.top,a.onComplete);break;case"dailymotion":var e=function(){var f=400;if(typeof(a.height)!="undefined"){f=a.height}$("#greyBoxContent").html(a.title+'<object width="100%" height="'+f+'" style="display:block;"><param name="movie" value="http://www.dailymotion.com/swf/video/'+b+'"></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/'+b+'" width="100%" height="'+f+'" allowfullscreen="true" allowscriptaccess="always"></embed></object>')};greyBoxDisplay(e,a.opacity,a.top,a.onComplete);break;case"image":var e=function(){$("#greyBoxContent").html($("#greyBoxContent").html()+'<img src="'+b+'" alt="" style="display:none;" id="greyBoxImage" />');$("#greyBoxImage").load(function(){$("#greyBoxImage").fadeOut(0);$("#greyBoxLoader").remove();$("#greyBoxContent").prepend(a.title);$("#greyBoxImage").fadeIn("medium");$("#greyBoxContent").css("width",Math.floor($("#greyBoxImage").outerWidth())+"px");$("#greyBox").css("width","auto").css("left","-"+(Math.ceil($("#greyBoxContent").outerWidth()/2))+"px")})};greyBoxDisplay(e,a.opacity,a.top,a.onComplete);break;default:break}}else{greyBoxDisplay(a.customFunction,a.opacity,a.top,a.onComplete)}return false}function greyBoxDisplay(d,b,c,a){if($("#greyBox").attr("status")!="running"){$("#greyBox").attr("status","running");$("#greyBox").css("top",($(window).scrollTop()+c)+"px");if(jQuery.browser.msie&&jQuery.browser.version=="6.0"){$("#overlay").css("position","absolute").css("height",$(window).height()+"px").css("top",$(window).scrollTop()+"px");$("#greyBoxLoader").css("height","200px")}$("object:not(.keep-visible), embed").css("visibility","hidden");if(jQuery.browser.msie&&jQuery.browser.version=="6.0"){$("select").css("visibility","hidden")}$("#overlay").fadeTo(greyBoxEffectDuration,b,function(){$("#greyBox").fadeIn(greyBoxEffectDuration,function(){$("#greyBox").attr("status","done");d();a()})})}}function greyBoxHide(){if($("#greyBox").attr("status")!="running"){$("#greyBox").attr("status","running");$("#greyBox").fadeOut(greyBoxEffectDuration,function(){$("#greyBox").hide();$("#overlay").fadeOut(greyBoxEffectDuration,function(){$("object:not(.keep-visible), embed").css("visibility","visible");if(jQuery.browser.msie&&jQuery.browser.version=="6.0"){$("select").css("visibility","visible")}$("#greyBox").attr("status","done");$("#greyBox").remove()})})}};

function buildSite(){
	
	if(typeof(mobile) == 'undefined'){
		
		site.loading = true;
		
		//Calage générique
		$(window).resize(function(){
			adjustSite();
		});
		
		/** FONCTION A EXECUTER APRES CHARGEMENT GREYBOX **/
		var complete = function(){
		
			//On empèche la fermeture par le fond
			$('#overlay').attr('onclick', '');
			
			//On vide de contenu de la page
			$('#viewport').html('');
			
		}
		
		greyBoxShow('_loader.php', 300, 'get', {top:0, opacity:1, background:'#ff5a44', onComplete:complete, className:'loading', duration:0});
		
	}
}

function buildSection(id, html){
	var image = '';
	if(typeof(site.pages[id].image) != 'undefined'){
		image = ' style="background-image:url('+site.pages[id].image+');"';
	}
	$('#viewport').append('<div class="section"><div class="customScrollArea"><div class="background"'+image+'><div class="container">'+html+'</div></div></div></div>');
}

function buildContent(){
	/** LECTURE DE L'ARBO **/
	//On parcours la NAV
	$.each(site.pages, function(i, nav){
		setTimeout(function(){
			$.ajax(nav.url, {
				async: false,
				type: 'POST',
				data: {mode:'ajax'},
				complete: function(data){
				
					buildSection(i, data.responseText);
					/** PROGRESSION **/
					displayProgress(i);
					
				}
			});
		}, 1);
	});
}

function displayProgress(id){
	var ratio = Math.ceil((99*(id+1))/site.pages.length);
	if($('#loader').length){
		$('#loader').text(ratio);
	}
	/**SITE CHARGÉ**/
	if(id == (site.pages.length-1)){
		//On renseigne les hauteurs des sections
		var height = 0;
		var i = 0;
		$('#viewport > .section').each(function(){
			site.pages[i].top = height;
			$(this).css('height', site.viewport.height+'px').attr('top', height);
			height += site.viewport.height;
			i++;
		});
		//On remonte en tête de page
		setTimeout(function(){
			$(window).scrollTop(0);
		}, 1);
		//On applique le scroll personnalisé
		var ctp = 0;
		$('#viewport > .section > .customScrollArea').each(function(){
			$(this).attr('id', 'customScrollArea_'+ctp);
			var config = {
				id: 'customScrolling_'+ctp,
				height: site.viewport.height,
				bar: {
					width: 25
				},
				cursor: {
					height: 150,
					width: 15
				}
			}
			customScrolling('#customScrollArea_'+ctp, config);
			ctp++;
		});
		//On spécifie que le chargement du site est complet
		site.loading = false;
		//On masque le loader
		$('#loader').animate({
			top:'60%',
			opacity:0
		},{
			duration:(site.loader.hidePercentage*1000),
			complete:function(){
				$('#insign').fadeIn(site.loader.displayBrand*1000, function(){
					$('#go').show();
					setTimeout(function(){
						closeLoader();
					}, site.loader.hideTimeout*1000);
				});
			}
		});
	}
}

function closeLoader(){
	if($('#overlay').length && $('#greyBox').length && !site.scrolling && $('#greyBox').hasClass('loading')){
		site.scrolling = true;
		$('#go').hide();
		$('#overlay, #greyBox').animate({
			top:'-100%'
		},{
			duration:(site.scrollSpeed*1000),
			complete:function(){
				greyBoxHide();
				site.scrolling = false;
				$('#mainMenu a[onclick^="return reachContent"]:eq('+site.current+')').click();
			}
		});
	}
}

function internalLink(trigger){
	var href = $(trigger).attr('href');
	var rel = $('#mainMenu a[href="'+href+'"]');
	if(rel.length){
		rel.click();
		return false;
	}
}

//GESTION DU REDIMENSIONNEMENT DU SITE
function adjustSite(){
	
	//On re-spécifie les variables du VIEWPORT
	site.viewport.width = $(window).width();
	site.viewport.height = $(window).height()-80;
	
	var height = 0;
	var i = 0;
	$('#viewport > .section').each(function(){
		site.pages[i].top = height;
		$(this).css('height', site.viewport.height+'px').attr('top', height);
		$(this).find('> .customScrolling').css('height', site.viewport.height+'px');
		height += site.viewport.height;
		i++;
	});
	
	setTimeout(function(){
		$(window).scrollTop(site.pages[site.current].top);
	}, 1);
	
}

function expandSubLevel(trigger){
	if(!$(trigger).parent().hasClass('current')){
		$(trigger).parent().addClass('current').siblings('.current').removeClass('current');
	}
	return false;
}

function reachContent(trigger){
	if(typeof(mobile) == 'undefined'){
		if(!$(trigger).parent().hasClass('current') || $(trigger).next().find('li.current').length){
			var ctp = 0;
			var id = null;
			$('#mainMenu a[onclick^="return reachContent"]').each(function(){
				if($(this)[0] == trigger){
					id = ctp;
				}
				ctp++;
			});
			if(id != null){
				scrollContent(trigger, id);
			}
		}
		return false;
	}
}

function scrollContent(trigger, id){
	if(typeof(site.loader.after) != 'undefined' && !site.loader.after.executed){
		site.loader.after.executed = true;
		site.loader.after.callback();
	}
	if(!site.scrolling && (!$(trigger).parent().hasClass('current') || $(trigger).next().find('li.current').length)){
		site.scrolling = true;
		
		if($(trigger).parents('ul').length > 1){
			if(!$(trigger).parents('li:eq(1)').hasClass('current')) $(trigger).parents('li:eq(1)').addClass('current').siblings('.current').removeClass('current');
			$(trigger).parent().siblings('.current').removeClass('current');
			$('#mainMenu .logo.current').removeClass('current');
		}
		else{
			$('#mainMenu .current').removeClass('current');
		}
		$(trigger).parent().addClass('current');
		
		site.current = id;
		document.location.href = '#!/'+site.pages[id].id;
		
		$('body').attr('class', site.pages[id].bg);
		
		$('html,body').animate({scrollTop: site.pages[id].top}, {
			duration:(site.scrollSpeed*1000),
			complete: function(){
				site.scrolling = false;
			}
		});
	}
}

function reachPrev(){
	var ctp = 0;
	var id = null;
	$('#viewport > .section').each(function(){
		if(parseInt($(this).attr('top')) < $(window).scrollTop()){
			target = $(this);
			id = ctp;
		}
		ctp++;
	});
	if(id != null){
		reachContent($('#mainMenu a[onclick^="return reachContent"]:eq('+id+')')[0]);
		$('#toolbar .pager > li.prev').addClass('focus');
		setTimeout(function(){
			$('#toolbar .pager > li.prev').removeClass('focus');
		}, site.scrollSpeed*1000);
	}
}
function reachNext(){
	var switchOut = false;
	var ctp = 0;
	var id = null;
	$('#viewport > .section').each(function(){
		if(!switchOut){
			if(parseInt($(this).attr('top')) > $(window).scrollTop()){
				switchOut = true;
				target = $(this);
				id = ctp;
			}
		}
		ctp++;
	});
	if(id != null){
		reachContent($('#mainMenu a[onclick^="return reachContent"]:eq('+id+')')[0]);
		$('#toolbar .pager > li.next').addClass('focus');
		setTimeout(function(){
			$('#toolbar .pager > li.next').removeClass('focus');
		}, site.scrollSpeed*1000);
	}
}

//NAVIGATION AU CLAVIER
function keyboardNav(keyCode){
	switch(keyCode){
		//Backspace
		case 8:
			$('#mainMenu li.current').removeClass('current');
			break;
		//Flèche UP
		case 38:
			reachPrev();
			break;
		//Flèche DOWN
		case 40:
			reachNext();
			break;
		//AUTRES
		default:
			break;
	}
}

if(typeof(mobile) == 'undefined'){
	$(document).keyup(function(event){
		keyboardNav(event.keyCode);
	});
	/*$(document).scroll(function(event){
		if(!$('body').hasClass('bgWhite')){
			if($(window).scrollTop() > parseInt($('#viewport > .section:eq(1)').attr('top'))){
				$('body').attr('class', 'bgWhite');
			}
		}
	});*/
	/*$(document).mousewheel(function(){
		var switchOut = false;
		var ctp = 0;
		var id = null;
		$('#viewport > .section').each(function(){
			if(!switchOut){
				if(parseInt($(this).attr('top')) <= ($(window).scrollTop() + Math.ceil(site.viewport.height*25*0.01))){
					target = $(this);
					id = ctp;
				}
				else{
					switchOut = true;
				}
			}
			ctp++;
		});
		if(id != null){
			$('body').attr('class', site.pages[id].bg);
			var trigger = $('#mainMenu a[onclick^="return reachContent"]:eq('+id+')')[0];
			if($(trigger).parents('ul').length > 1){
				if(!$(trigger).parents('li:eq(1)').hasClass('current')) $(trigger).parents('li:eq(1)').addClass('current').siblings('.current').removeClass('current');
				$(trigger).parent().siblings('.current').removeClass('current');
				$('#mainMenu .logo.current').removeClass('current');
			}
			else{
				$('#mainMenu .current').removeClass('current');
			}
			$(trigger).parent().addClass('current');
		}
	});*/
}
