/*--------------------------------------------------
 * Mediastay Object
 * -------------------------------------------------*/
var MDS={};

var lang=new Array();
lang['fr']='Verifiez les champs : ';
lang['en']='Please check :';
var lg_formPleaseCheck=lang['en'];

MDS.regional='fr';
MDS.tmp={}; /* Used to store temporary data */
MDS.profile={
	'style':{
		'ok':'mfc-ok',
		'error':'mfc-error'
	}
};
MDS.attrDefaultValue='dv';
MDS.contactFormCheck=function(/*data*/){
	var check=false;
	MFC.formCheck('contactForm',function(data){
		MDS.tmp.contactFormErrors=data.err;
		MDS.tmp.contactFormErrorsElm=data.errElm;
	});
	var errors=this.tmp.contactFormErrors;
	check=(errors.length==0);
	if(!check){
		var errorsElm=this.tmp.contactFormErrorsElm;
		var jElm,i;
		for(i in errorsElm){
			jElm=$(errorsElm[i]);
			jElm.addClass(this.profile.style.error).removeClass(this.profile.style.ok);
		}
		MFC.formCheckDisplayResult(errors,errorsElm);
	}
	return check;
};

MDS.formFieldCheck=function(elm,css){
	if(typeof(css)=='undefined') css=true;
	if($(elm)[0]!=elm){
		elm=elm[0];
	}
	var jElm=$(elm);
	var validated=MFC.formElementCheck(elm,true);
	if(validated){
		if(css){
			jElm.addClass(this.profile.style.ok).removeClass(this.profile.style.error);
		}
	}else{
		if(css){
			jElm.addClass(this.profile.style.error).removeClass(this.profile.style.ok);
		}
	}
	return validated;
};

/**
* Contact: Init form
*/
MDS.contactFormInit=function(){
	/* */
	$('#contactForm input[type="text"],#contactForm textarea,#contactForm select').each(function(){
		if(MFC.formElementMustBeChecked($(this)[0])){
			$(this).blur(function(){
				MDS.formFieldCheck($(this),true);
			});
		}
		if($(this).val()!='')$('label[for="'+$(this).attr('id')+'"]').hide();
	});
	
	$('#contactForm input[type="text"],#contactForm textarea').each(function(){
		$(this).focusin(function(){
			$('label[for="'+$(this).attr('id')+'"]').hide();
		});
		$(this).focusout(function(){
			$(this).val(MU.trim($(this).val()));
			if($(this).val()=='')$('label[for="'+$(this).attr('id')+'"]').show();
		});
	});
	$('#email').blur(function(){
		if(MU.isEmail($(this).val())){
			$(this).addClass(this.profile.style.error).removeClass(this.profile.style.ok);
		}else{
			$(this).addClass(this.profile.style.ok).removeClass(this.profile.style.error);
		}
	});	
	/* Form listener */
	$('#contactForm').submit(function(){
		return MDS.contactFormCheck();
	});
};

/* Cv formulaire*/
MDS.cvFormCheck=function(/*data*/){
	var check=false;
	MFC.formCheck('cvForm',function(data){
		MDS.tmp.cvFormErrors=data.err;
		MDS.tmp.cvFormErrorsElm=data.errElm;
	});
	var errors=this.tmp.cvFormErrors;
	check=(errors.length==0);
	if(!check){
		var errorsElm=this.tmp.cvFormErrorsElm;
		var jElm,i;
		for(i in errorsElm){
			jElm=$(errorsElm[i]);
			jElm.addClass(this.profile.style.error).removeClass(this.profile.style.ok);
		}
		MFC.formCheckDisplayResult(errors,errorsElm);
	}
	return check;
};

MDS.cvFormInit=function(){
	$('#cvForm input[type="text"],#cvForm input[type="file"],#cvForm select,#cvForm textarea,').each(function(){
		if(MFC.formElementMustBeChecked($(this)[0])){
			$(this).blur(function(){
				MDS.formFieldCheck($(this),true);
			});
		}
		if($(this).val()!='')$('label[for="'+$(this).attr('id')+'"]').hide();
	});
	$('#cvForm input[type="text"],#cvForm textarea').each(function(){
		$(this).focusin(function(){
			$('label[for="'+$(this).attr('id')+'"]').hide();
		});
		$(this).focusout(function(){
			$(this).val(MU.trim($(this).val()));
			if($(this).val()=='')$('label[for="'+$(this).attr('id')+'"]').show();
		});
	});
	$('#email').blur(function(){
		if(MU.isEmail($(this).val())){
			$(this).addClass(this.profile.style.error).removeClass(this.profile.style.ok);
		}else{
			$(this).addClass(this.profile.style.ok).removeClass(this.profile.style.error);
		}
	});	
	/* Form listener */
	$('#cvForm').submit(function(){
		return MDS.cvFormCheck();
	});
}

/**
* Deroule sur la home la liste des sites par payx
*/
function slideDeroul(){
	$("#country ul li ul, #coordonnees ul li ul").each( function () {
		$(this).hide();
	});
	$("#country ul li a.pays, #coordonnees ul li a.pays").click( function () {
		if ($(this).next("#country ul li ul:visible, #coordonnees ul li ul:visible").length != 0) {
			$(this).next("#country ul li ul, #coordonnees ul li ul").slideUp("normal", function () { $(this).prev().removeClass("open") });
		}else {
			$("#country ul li ul, #coordonnees ul li ul").slideUp("normal", function () { $(this).prev().removeClass("open") });
			$(this).next("#country ul li ul, #coordonnees ul li ul").slideDown("normal", function () { $(this).prev().addClass("open") });
		}
		return false;
	});
}

$(document).ready(function(){
	$('a[href=#wrapper]').click(function(){
		$('html').animate({scrollTop:0},'slow');
		return false;
	});
	
	var currentLang=$($('.currentLang')[0]).attr('id').split('-')[1];
	lg_formPleaseCheck=lang[currentLang];
	
	if($.cookie('playVideo')==null){
		if($('#slideHead video').length > 0){
			$('#slideHead video')[0].play();
			$.cookie('playVideo','false');
		}
	}
	
});
