function initContactBox(){
	$("#contact_box #cb_content").click(function () {
		if($("#contact_box").hasClass("open")){
			var pos = "-280px"; $("#contact_box").removeClass("open");
		}else{
			var pos = "50px"; $("#contact_box").addClass("open");
		}
		$("#cb_panel").animate({"left": pos }, "slow", "easein"); 
	});
}

function closeContactBox(){
	if($("#contact_box").hasClass("open")){
		var pos = "-280px"; $("#contact_box").removeClass("open");
		$("#cb_panel").animate({"left": pos }, "slow", "easein"); 
	}
}

function slideShow(){
	$('#homejobs .module_content').cycle({ 
		fx:     'scrollDown', 
		easing: 'bounceout' 
	});
}

function lightBox(){
	$("a.lightbox").fancybox({
		'titleShow'     : 	false,
		'transitionIn'	: 	'elastic',
		'transitionOut'	: 	'elastic',
		'easingIn'      : 	'easeOutBack',
		'easingOut'     : 	'easeInBack',
		'onStart'		: 	pauseReference,
		'onClosed'		: 	resumeReference
	});
}

function initReferences(){
	$('#references .cnav').prepend('<div id="nav">') ;
	$('#references .module_content').cycle({ 
		fx:     'fade', 
		speed:  1200,
		timeout: 4000, 
		pager:  '#nav'
	});
	$("#nav a").each(function (i) {	
		$(this).click(
			function(){
				$('#references .module_content').cycle('pause');
			}
		);
	});
}

function pauseReference(){
	$('#references .module_content').cycle('pause');
}
function resumeReference(){
	$('#references .module_content').cycle('resume');
}

function layout(){
	if($(window).height() > ($('#container').height() + ($('#header').height()))){
		$('#container').height(($(window).height() - $('#header').height()));
	}else{
		if( ($('#content').height() + parseInt(($('#content').css('paddingTop')).replace('px','')) + parseInt(($('#content').css('paddingBottom')).replace('px','')) + $('#header').height()) < $(window).height()){
			$('#container').height($(window).height() - $('#header').height() );
		}else{
			$('#container').height($('#content').height() +  parseInt(($('#content').css('paddingTop')).replace('px','')) + parseInt(($('#content').css('paddingBottom')).replace('px','')) );
		}
	}
}

/*function initGMap(){
	if ($("#google_maps").length > 0){
		$("#google_maps").append('<iframe style="visibility: none;" width="460" height="303" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.fr/maps/ms?hl=fr&amp;ie=UTF8&amp;msa=0&amp;msid=101935956628868122949.00045678b9fbcc40986bd&amp;ll=49.601922,6.099172&amp;spn=0.038938,0.072956&amp;z=13&amp;output=embed"></iframe>');
		window.setTimeout(function() { $("google_maps_inner").show(); }, 1000);
	}
}*/
function initForm(){
	if ($("#table_contact_form").length > 0) {
		$('#table_contact_form').validate();
		$('.fieldvalue').clearField();
	}
}

function initStaff(){
	$('div.person').each(function (i) {	
		var id = $(this).attr('id');
		id = id.replace('p_','i_');
		id_parent = id.replace('i_','p_');
		$(this).mouseover(function () {  
			$('#'+id).fadeIn("fast"); 
		});
		$(this).mouseleave(function () { 
			$('#'+id).fadeOut("fast"); 
		});
	})
}

function updateCounter() {
		var creation = new Date(2006, 11, 15);
		var today = new Date();			
		var diff = Math.abs(today.getTime() - creation.getTime());
		var years = Math.floor(diff / (1000 * 60 * 60 * 24 * 365));
		diff -= years * (1000 * 60 * 60 * 24 * 365);
		var months = Math.floor(diff / (1000 * 60 * 60 * 24 * 30));
		diff -= months * (1000 * 60 * 60 * 24 * 30);
		var days = Math.floor(diff / (1000 * 60 * 60 * 24)); 
		diff -= days * (1000 * 60 * 60 * 24);			
		var hours = Math.floor(diff / (1000 * 60 * 60)); 
		diff -= hours * (1000 * 60 * 60);			
		var minutes = Math.floor(diff / (1000 * 60)); 
		diff -= minutes * (1000 * 60);		
		var seconds = Math.floor(diff / (1000)); 
		diff -= seconds * (1000);				
		var diff_sexy = years + " an" + ((years>1)?"s":"");
		if (months>0) diff_sexy = diff_sexy + " " + months + " mois";
		if (days>0) diff_sexy = diff_sexy + " "+ days + " jour" + ((days>1)?"s":"");
		if (hours>0) diff_sexy = diff_sexy + " "+ hours + " heure" + ((hours>1)?"s":"");
		if (minutes>0) diff_sexy = diff_sexy + " "+ minutes + " minute" + ((minutes>1)?"s":"");
		if (seconds>0) diff_sexy = diff_sexy + " "+ seconds + " seconde" + ((seconds>1)?"s":"");
		$("span#creation_date").html(diff_sexy);
		window.setTimeout(updateCounter, 1000);
}

function isLoaded(){
 	if($("body#contact").length > 0) $('#link_gallery').fadeIn('slow', function() { });
 	$("a.js_blank").attr("target" , "_blank");	
}

/**************************************************************************************************************************************************************/
window.onload = function() {
	layout();
	initContactBox();
	if ($("body#book").length > 0) initReferences();
	if ($("body#staff").length > 0) initStaff();
	if ($("body#home").length > 0){
		updateCounter();
		setTimeout("closeContactBox()", 2000);
	}
	if ($("body#contact").length > 0){
		initGMap();
		initForm();
	}else{
		slideShow();
		lightBox();
	}	
	isLoaded();
}


window.onresize = function() {
	layout();
}
