// JavaScript Document
$(function(){
	Cufon.replace('#nav-top>ul>li>a,#subnav-top,#nav-bottom,#front-news h3,#front-news h5,#front-page h1,#front-page h2,.intern #main h1, .intern #main h2:not(#indus_blisks,#indus_blades,#indus_others,#expert_blending,#expert_profile,#expert_surface,#expert_edge,#expert_2d,#expert_3d),aside.date-list h3,#equipe #content h3,#news article.post h3', {fontFamily:'Advert-Light',hover:true});
	Cufon.replace('#front-page h1 span.special,#front-page h2 span.special,.intern #main h1 span.special,.intern #main h2 span.special, aside span.special,#equipe #content h3 span.special', {fontFamily:'Arial',hover:true});
	
	$(window).resize(function(){
			resizeWin();
	})
	resizeWin();
	
	$("#nav-top>ul>li").hover(
		function(){
			$(this).addClass("over")
		},
		function(){
			$(this).removeClass("over");
			if($(this+":not(.over)")){
					Cufon.replace($(this).children("a").first(),{color:'#ffffff',fontFamily:'Advert-Light',hover:{color:'#16395d'}})
					
					if($(this).children("a").hasClass("active")){
						Cufon.replace($(this).children("a.active"),{color:'#16395d',fontFamily:'Advert-Light',hover:{color:'#16395d'}})
					}
			}
		}
	)
	
	$("#main h1,#main h2,aside.date-list h3,#equipe #content h3").not("#onglets h2").each(function(){
		$(this).html($(this).html().replace(/\&amp;/g,"<span class=\"special\">&amp;</span>") ); 
	})
	//$("#main h1").wrap("<span style='color:red'/>");
	
	$("#avr-pieces").bxSlider({
		ticker:true,
		tickerSpeed:2500,
		pager:false
	})
	
	$("#news-window").bxSlider({
		mode:'vertical',
		pager:false,
		controls:false,
		auto:true,
		autoHover:true,
		displaySlideQty:2,
		pause:5000
	})
	
	$("#onglets").append("<div class=\"s_contenu\">");
	$("#onglets section").children().not("h2").hide();
	$("#onglets section").addClass("hide");
	var ancre = window.location.hash;
	if(ancre!="" && $(ancre).length>0 && $("#onglets").length>0){
		changeContent($(ancre));
	}else if($("#onglets").length>0){
		changeContent("#onglets section h2:eq(0)")
	}
	
	
	$("#onglets section h2").click(function(){
		changeContent($(this))
	});
	
	
	$("#form-salesforce div.boutons button").click(function(e){
		e.preventDefault();
		
		if(validation("form-salesforce")){
			$("#form-salesforce").submit();
		}
	})
	
});

function resizeWin(){
	var heightdiff=0
	$("#f-wrap").css({'height':'auto'});
	realHeight=$(document).height()-5;
	if(realHeight > $(window).height()){
		var height1 = $(document).height();
		flag=false;
	}else{
		var height1 = $(window).height();
		flag=true;
	}
	var height2 = $("#box").height()-($("#f-wrap").height());
	heightdiff = height1 - height2 + 0;
	$("#f-wrap").height(heightdiff);
}

function changeContent(elem){
	$("#onglets section h2").removeClass("over");
	$(elem).addClass("over");
		
	var contenu = $(elem).parent().html().replace('<h2 class="'+$(elem).attr("class")+'" id="'+$(elem).attr("id")+'">'+$(elem).html()+'</h2>','')
	//console.log(contenu)
	$("div.s_contenu").html(contenu).children().show();
	$("div.s_contenu h2").hide()
}






function validation(form){
	var valid = false;
	$("#"+form+" div, #"+form+" fieldset div, #"+form+" fieldset div table tbody tr td").children().each(function() {
		if($(this).hasClass('testresult')){
			if($('input[name="password"]').hasClass('required')){
				if($(this).hasClass('shortPass')){
					valid = false;
					$('input[name="password"]').addClass('erreur');
					afficheMsg('blockquote.message', 'invalid_password')
					return false;
				}else{
					$(this).removeClass('erreur');
					valid = true;
				}
			}
		}
		
		
		if($(this).hasClass('required')){
			if($(this).val()==''){
				$(this).addClass('erreur');
				//$(this).focus();
				valid = false;
				if($(this).attr('name')=='texte_fr' || $(this).attr('name')=='texte_en' || $(this).attr('name')=='text_fr_text' || $(this).attr('name')=='text_en_text'){
					$("#"+$(this).attr('id')+'_ifr').addClass('erreur');
				}
				if($(this).hasClass('uploadthis') && ($(this).prev("label").children("a").length < 1)){
				//genName=$(this).attr('name').substr(0,($(this).attr('name')).length-4);
					genName=$(this).attr('id');
					$("#"+genName+'Uploader').addClass('erreur');
				}
				
				afficheMsg('blockquote.message', 'empty_all')
				return false;
				
			}else if($(this).hasClass('email')){
				var regexp = new RegExp(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/)
				
				if(!$(this).val().match(regexp)){
					valid = false;
					$(this).addClass('erreur');
					afficheMsg('blockquote.message', 'invalid_email')
					return false;
				}else{
					$(this).removeClass('erreur');
					valid = true;
				}
			}
			else if($(this).hasClass('passwordrepeat')){
				if($(this).val()!=$('input[name="password"]').val()){
					valid = false;
					$(this).addClass('erreur');
					afficheMsg('blockquote.message', 'invalid_password_combination')
					return false;
				}else{
					$(this).removeClass('erreur');
					valid = true;
				}
			
			}else{
				$(this).removeClass('erreur');
				
				if($(this).attr('name')=='texte_fr' || $(this).attr('name')=='texte_en' || $(this).attr('name')=='text_fr_text' || $(this).attr('name')=='text_en_text'){
					$("#"+$(this).attr('id')+'_ifr').removeClass('erreur');
				}
				
				if($(this).hasClass('uploadthis') && ($(this).prev("label").children("a").length < 1)){
				//genName=$(this).attr('name').substr(0,($(this).attr('name')).length-4);
					genName=$(this).attr('id');
					$("#"+genName+'Uploader').removeClass('erreur');
				}
				$(this).removeClass('erreur');
				valid = true;
			}	
		}else{
			$(this).removeClass('erreur');
			valid = true;
		}
	});
	if(valid == true){
		return true;
	}
}


function afficheMsg(element, code){
	//alert($("#form-salesforce input[name=lang]").val())
	$.ajax({
		data: 'error-code='+code,
		url: '/includes/messages_'+$("#form-salesforce input[name=lang]").val()+'.php',
		type: 'get',
		dataType : 'html',
		success : function(obj){
			if($("#form-salesforce").children(element).length<1){
				$("#form-salesforce").prepend(obj);
			}else{
				$(element).replaceWith(obj)
			}
			//$(element).replaceWith(obj)
			
			//$(obj).show();
		},
		error : function(obj, mess){alert(mess)}
	});
	
	//$("section div.boutons button").attr("disabled","disabled")
	effaceMsg(element,'');
}

function effaceMsg(element,type){
	setTimeout(function(){
		$(element).slideUp('slow',function(){
			$(this).removeClass(type);
			//$("section div.boutons button").attr("disabled","")
		})
		}, 3000);
}
