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

/*--- GREYBOX ---*/
//Config générale récurente
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.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}$("#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)}$("#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(b)};greyBoxDisplay(e,a.opacity,a.top);break;case"get":var e=function(){$("#greyBoxContent").load(b)};greyBoxDisplay(e,a.opacity,a.top);break;case"post":var e=function(){$.ajax({type:"POST",url:b,data:a.data,success:function(f){$("#greyBoxContent").html(f)}})};greyBoxDisplay(e,a.opacity,a.top);break;case"iframe":var e=function(){var f=400;if(typeof(a.height)!="undefined"){f=a.height}$("#greyBoxContent").html('<iframe src="'+b+'" border="0" frameborder="0" width="100%" height="'+f+'" allowTransparency="true"></iframe>')};greyBoxDisplay(e,a.opacity,a.top);break;case"youtube":var e=function(){var f=400;if(typeof(a.height)!="undefined"){f=a.height}$("#greyBoxContent").html('<object width="100%" height="'+f+'"><param name="movie" value="http://www.youtube.com/v/'+b+'&hl=fr_FR&fs=1&rel=0&color1=0x006699&color2=0x54abd6"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+b+'&hl=fr_FR&fs=1&rel=0&color1=0x006699&color2=0x54abd6" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="100%" height="'+f+'"></embed></object>')};greyBoxDisplay(e,a.opacity,a.top);break;case"dailymotion":var e=function(){var f=400;if(typeof(a.height)!="undefined"){f=a.height}$("#greyBoxContent").html('<object width="100%" height="'+f+'"><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);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();$("#greyBoxImage").fadeIn("medium");$("#greyBox").css("width","auto").css("left","-"+(Math.ceil($("#greyBoxContent").outerWidth()/2))+"px")})};greyBoxDisplay(e,a.opacity,a.top);break;default:break}}else{greyBoxDisplay(a.customFunction,a.opacity,a.top)}return false}function greyBoxDisplay(c,a,b){if($("#greyBox").attr("status")!="running"){$("#greyBox").attr("status","running");$("#greyBox").css("top",($(window).scrollTop()+b)+"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,a,function(){$("#greyBox").fadeIn(greyBoxEffectDuration,function(){$("#greyBox").attr("status","done");c()})})}}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")})})}};


/*-------- Debug IE 6 --------*/
function ie6Fixes(){
	/** pour les éléments qui disparaissent **/
	$('#header').wrap('<div />');
	/** pour les PNG **/
	$('.specialLink, #shadow').addClass('png_bg');
	DD_belatedPNG.fix('.png_bg');
}

/*--- SYSTEME ONGLETS ---*/
function initializeTabs(){
	$('.tab').hide();
	$($('.tabs li.active a').attr('href')).show();
	limitation_img();

	$('.tabs li').each(function(){	
		var divID = $(this).find('a').attr('href');
		if(divID != '#' && $(divID).length){
			$(this).find('a').click(function(){				
				return showTab(this, divID);
			});
		}
		else{
			$(this).find('a').click(function(){
				$('#realisations .more').hide();
				return false;
			});
		}	
	});
}

function showTab(trigger, target){
	$('.tabs li').removeClass('active');
	$(trigger).parent().addClass('active');
	$(target).fadeIn();
	$('.tab:not('+target+')').hide();	
	limitation_img();
	
	return false;
}

function limitation_img()
{
	if($('.tabs li.active a').attr('href')=='#toutes')
	{
		$('#realisations .more').show();		
		$('#toutes img').hide();

		for(i=0;i<9;i++)
		{
			$('#toutes img').eq(i).show();
		}
	}
	else
	{
		$('#realisations .more').hide();
	}
}

function more_realisations()
{
	$('#toutes').find('img').fadeIn();
	return false;
}

/*---- hauteur RIGHTCOL ----*/

jQuery(window).load(function(){

var height  = $('.article').height();
$('.repositionnement').height(height+42);

});

function fieldFocus(element){
	if(element.value == element.defaultValue){
		element.value = '';
	}
}
function fieldBlur(element){
	if(element.value == ''){
		element.value = element.defaultValue;
	}
}

/*--Function check Form--*/
function checkForm(form, config){

	//Vérification de la config
	if(typeof(config.offset) == 'undefined'){
		config.offset = {
			left: 0,
			top: 10
		};
	}
	else{
		if(typeof(config.offset.left) == 'undefined'){config.offset.left = 0;}
		if(typeof(config.offset.top) == 'undefined'){config.offset.top = 10;}
	}

	//Initialisation de la validation
	if(typeof($(form).attr('status')) == 'undefined' && $(form).attr('status') != 'initialized'){
		$(form).attr('status', 'initialized');
	
		//On stocke les champs uniques dans un tableau
		var uniqueElements = new Array();
		//Si on a des champs requis
		if(typeof(config.required) != 'undefined'){
			for(var i=0; i<config.required.elements.length; i++){
				if(jQuery.inArray(config.required.elements[i], uniqueElements) == -1){
					uniqueElements.push(config.required.elements[i]);
				}
			}
		}
		//Si on a des champs à comparer
		if(typeof(config.compare) != 'undefined'){
			for(var i=0; i<config.compare.elements.length; i++){
				if(jQuery.inArray(config.compare.elements[i][1], uniqueElements) == -1){
					uniqueElements.push(config.compare.elements[i][1]);
				}
			}
		}
		//Si on a des champs radio
		if(typeof(config.radio) != 'undefined'){
			for(var i=0; i<config.radio.elements.length; i++){
				if(jQuery.inArray(config.radio.elements[i], uniqueElements) == -1){
					uniqueElements.push(config.radio.elements[i]);
				}
			}
		}
		//Si on a des champs email à vérifier
		if(typeof(config.email) != 'undefined'){
			for(var i=0; i<config.email.elements.length; i++){
				if(jQuery.inArray(config.email.elements[i], uniqueElements) == -1){
					uniqueElements.push(config.email.elements[i]);
				}
			}
		}
		//Si on a des dates à vérifier
		if(typeof(config.date) != 'undefined'){
			for(var i=0; i<config.date.elements.length; i++){
				if(jQuery.inArray(config.date.elements[i], uniqueElements) == -1){
					uniqueElements.push(config.date.elements[i]);
				}
			}
		}
		//Si on a des champs customisés
		if(typeof(config.custom) != 'undefined'){
			for(var i=0; i<config.custom.length; i++){
				if(jQuery.inArray(config.custom[i].element, uniqueElements) == -1){
					uniqueElements.push(config.custom[i].element);
				}
			}
		}
		//On crée les infos en fonction des champs uniques
		for(var i=0; i<uniqueElements.length; i++){
			var target = $('#'+uniqueElements[i]);
			checkFormFeedback(target, {
				id: i,
				offset: config.offset,
				message: '',
				position: target.offset()
			});
		}
	}
	
	//Statut
	var errors = 0;
	/*** Checker les adresses email ***/
	if(typeof(config.email) != 'undefined'){
		for(var i=0; i<config.email.elements.length; i++){
			var target = $('#'+config.email.elements[i]);
			updateFeedback(target, config.offset, config.email.message);
			
			var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/;
			if(reg.exec(target.val()) == null){
				$(target).parent().addClass('error');
				errors++;
			}
		}
	}
	/*** Vérifier une date ***/
	if(typeof(config.date) != 'undefined'){
		for(var i=0; i<config.date.elements.length; i++){
			var target = $('#'+config.date.elements[i]);
			updateFeedback(target, config.offset, config.date.message);
			
			var reg = /^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$/;
			if(reg.exec(target.val()) == null){
				$(target).parent().addClass('error');
				errors++;
			}
		}
	}
	/*** Checker la correspondance de 2 champs ***/
	if(typeof(config.compare) != 'undefined'){
		for(var i=0; i<config.compare.elements.length; i++){
			var target1 = $('#'+config.compare.elements[i][0]);
			var target2 = $('#'+config.compare.elements[i][1]);
			updateFeedback(target2, config.offset.top, config.compare.message);
			
			if(target2.val() != target1.val()){
				$(target2).addClass('error');
				errors++;
			}
		}
	}
	/*** Checker la sélection d'au moins 1 bouton radio ***/
	if(typeof(config.radio) != 'undefined'){
		for(var i=0; i<config.radio.elements.length; i++){
			var target = $('#'+config.radio.elements[i]);
			var ctp = 0;
			$('input[name="'+(target.attr('name'))+'"]').each(function(){
				if($(this).is(':checked')){
					ctp++;
				}
			});
			if(!ctp){
				target.parent().addClass('error');
				updateFeedback(target, config.offset, config.radio.message);
				errors++;
			}
		}
	}
	/*** Checking custom ***/
	if(typeof(config.custom) != 'undefined'){
		for(var i=0; i<config.custom.length; i++){
			var target = $('#'+config.custom[i].element);
			updateFeedback(target, config.offset, config.custom[i].message);
			
			if(config.custom[i].reg.exec(target.val()) == null){
				$(target).parent().addClass('error');
				errors++;
			}
		}
	}
	/*** Checking required ***/
	if(typeof(config.required) != 'undefined'){
		for(var i=0; i<config.required.elements.length; i++){
			
			var target = $('#'+config.required.elements[i]);
			var defaultValue = document.getElementById(config.required.elements[i]).defaultValue;
			if(target.is('input')){
				if(target.attr('type') == 'text' || target.attr('type') == 'password'){
					if(target.val() == ''){
						target.parent().addClass('error');
						updateFeedback(target, config.offset, config.required.message);
						errors++;
					}
				}
				else if(target.attr('type') == 'checkbox' || target.attr('type') == 'radio'){
					if(!target.is(':checked')){
						target.parent().addClass('error');
						updateFeedback(target, config.offset, config.required.message);
						errors++;
					}
				}
			}
			else if(target.is('select')){
				if($('option:selected', target).val() == ''){
					target.parent().addClass('error');
					updateFeedback(target, config.offset, config.required.message);
					errors++;
				}
			}
			else if(target.is('textarea')){
				if(target.val() == ''){
					target.parent().addClass('error');
					updateFeedback(target, config.offset, config.required.message);
					errors++;
				}
			}
			
		}
	}
	
	if(!errors){
		return true;
	}
	else{
		return false;
	}
	
};
function updateFeedback(target, offset, message){
	$('#'+target.attr('related_to')+' .message').html(message);
	if(target.attr('type') != 'checkbox' && target.attr('type') != 'radio'){
		if($('#'+target.attr('related_to')).outerWidth() > target.outerWidth()){
			var width = target.outerWidth() - parseFloat($('#'+target.attr('related_to')).css('paddingLeft')) - parseFloat($('#'+target.attr('related_to')).css('paddingRight'));
			$('#'+target.attr('related_to')).css('width', width+'px');
		}
		else{
			$('#'+target.attr('related_to')).css('width', 'auto');
		}
	}
	//Offset top
	var top =  Math.round(target.offset().top) - $('#'+target.attr('related_to')).outerHeight() + offset.top;
	$('#'+target.attr('related_to')).css('top', top+'px');
	//Offset left
	var left =  Math.round(target.offset().left) + offset.left;
	$('#'+target.attr('related_to')).css('left', left+'px');
}
function checkFormFeedback(target, data){
	//Plugged
	target.attr('related_to', 'checkFormFeedback_'+data.id);
	
	//Insertion de l'élément
	$('body').append('<div class="errorFeedback" id="checkFormFeedback_'+data.id+'" style="display:none;"><span class="message">'+data.message+'</span><span class="appendice"></span></div>');
	$('#checkFormFeedback_'+data.id).attr('form_element', target.attr('id'));
	
	//Offset top
	var top =  Math.round(data.position.top) - $('#checkFormFeedback_'+data.id).outerHeight() + data.offset.top;
	$('#checkFormFeedback_'+data.id).css('top', top+'px');
	//Offset left
	var left =  Math.round(data.position.left) + data.offset.left;
	$('#checkFormFeedback_'+data.id).css('left', left+'px');
	
	/*** Déclaration des évènements ***/
	//Si c'est un champ INPUT -> Checkbox ou Radio
	if(target.is('input') && (target.attr('type') == 'checkbox' || target.attr('type') == 'radio')){
		target.parent().find('label').hover(
			function(){
				if($(this).parent().hasClass('error')){
					$('#checkFormFeedback_'+data.id).fadeIn('fast');
				}
			},
			function(){
				if($(this).parent().hasClass('error')){
					$('#checkFormFeedback_'+data.id).fadeOut('fast');
				}
			}
		);
	}
	else{
		target.focus(function(){
			if($(this).parent().hasClass('error')){
				$('#checkFormFeedback_'+data.id).fadeIn('fast');
			}
		});
		target.blur(function(){
			if($(this).parent().hasClass('error')){
				$('#checkFormFeedback_'+data.id).fadeOut('fast');
			}
		});
	}
	
	//Si c'est un bouton radio
	if(target.is('input') && target.attr('type') == 'radio'){
		$('input[name="'+target.attr('name')+'"]').change(function(){
			if($(this).parent().hasClass('error')){
				$('#checkFormFeedback_'+data.id).fadeOut('fast', function(){
					var target = $(this).attr('form_element');
					$('#'+target).parent().removeClass('error');
				});
			}
		});
	}
	else{
		target.change(function(){
			if($(this).parent().hasClass('error')){
				$('#checkFormFeedback_'+data.id).fadeOut('fast', function(){
					var target = $(this).attr('form_element');
					$('#'+target).parent().removeClass('error');
				});
			}
		});
	}
}

/*--- PASSER AUTOMATIQUEMENT D'UN CHAMPS A L'AUTRE  ---*/


function nextField(id)
{
	if($('.CP_box input[type="text"]').eq(id).val()!='')
	{
		var reg =/[0-9]+/;
			
		if(reg.test($('.CP_box input[type="text"]').eq(id).val())==false)
		{		
			$('.CP_box input[type="text"]').eq(id).focus().select();
			$('.CP_box input[type="text"]').eq(id).val('');
		}
		else
		{
			id++;
			$('.CP_box input[type="text"]').eq(id).focus().select();
		}
	}
}

/*--- PASSER AUTOMATIQUEMENT D'UN CHAMPS A L'AUTRE  ---*/

/*--- FORMULAIRE 2 ETAPES (PAGE ARTISAN) ---*/

$(document).ready(function(){		
	
	$('#form_artisan').hide();
	$('#form_artisan').find('fieldset').hide();
	
	$('#first_step').click(function(){
	
		$('#form_artisan').show();			
		
		$('#form_artisan').find('fieldset').eq(0).fadeIn();
		return false;
	});
	
	$('#second_step').click(function(){
		$('#form_artisan').show();
		
		$('#form_artisan').find('fieldset').eq(1).fadeIn();
		$('#form_artisan').find('fieldset').eq(1).before('<hr class="nomargin clear"/>');
		$(this).unbind("click");
		return false;
	});
	
	/*------- POSITIONNEMENT BOUTON SUBMIT PAGE MAGAZINE ------*/
	var height = $('#form_magazine fieldset').eq(0).height();
	$('#form_magazine fieldset').eq(1).height(height);
});

/*--- DEPLIER REPLIER LE TEXTE DU FOOTER ---*/

var cpt=0;
function en_savoir_plus(param)
{
	if(cpt==0)
	{
		$('#content_footer').slideDown('slow',function()
		{
			$('#pushes .more').html('Replier');
		});
		cpt=1;
	}
	else
	{
		$('#content_footer').slideUp('slow',function()
		{
			$('#pushes .more').html('En savoir plus');						
		});	
		cpt=0;
	}
}
