// JavaScript Document

$(function(){

	init_modal();	
	
	
	
	init_slider_accueil();
	equipe_accueil();
	init_menu_principale();
	
	
	
	
	validate_form_footer();
	validate_form_contact();
	
	
	$("a.a_thumb").fancybox();
	
	
	$('.z3_element a').hover(function(){
		$("#" + this.id + " span.z3_highlight").stop().animate({
		color: '#28afe6'		
		},200);
	},function(){
		$("#" + this.id + " span.z3_highlight").stop().animate({
		color: '#707070'		
		},200);	
	});
	
	$("input").placeholder();
	

	$(".fade").hover(function(){
	
		$(this).stop().fadeTo('fast', 0.5);
		
				
	},function(){
				
		$(this).stop().fadeTo('fast', 1);
				
	});
	
	
	
	init_ajax_tweets();
	
	
	
});


function init_menu_principale(){

	$("#liste-menu-principale li").hover(function(){		
		if(this.id){			
			$("#" + this.id + "-dropdown").fadeIn("fast");		
		}	
	},function(){
		
		if(this.id){			
			$("#" + this.id + "-dropdown").fadeOut("fast");		
		}	
	
	});


}



function init_ajax_tweets(){
	
	
	
	if($(".drop_actif a").text() == ""){
	$url_ajax_tweet= "accueil/find_tweet/";
	}
	else{
	$url_ajax_tweet= "find_tweet/";
	}
	

	
	
	$.ajax({
	  url: $url_ajax_tweet,
	  type: "POST",
	  success: function(reponse){
		  
		$("#container_tweets_ajax").fadeOut("fast", function(){
			
			$("#container_tweets_ajax").empty();
			
			$("#container_tweets_ajax").append(reponse).fadeIn('fast');
		
		});
	  }  
	  
	});
}



/*
Init de l'effet roll over de l'équipe à 
la page d'accueil
*/
function equipe_accueil()
{
	
	$('#z3_third_a,#z3_second_a, #social_icones li a').hover(function() {				
		var fadeOut = $(this).children(".img_off");
		fadeOut.stop().animate({opacity: "0"}, 250);
	  }, function () {
		var fadeIn 	= $(this).children(".img_off");
	  	fadeIn.stop().animate({opacity: "1"}, 250);
	  });
} 





/*
Init du slider à l'accueil
*/
function init_slider_accueil(){

	$('#slider').cycle({
				fx:'fade',
				timeout:9000,
				speed:1000,
				delay: 1000,
				pager:  '#nav_slider',
				cleartypeNoBg : true,
				pause: 1
	});


}




/*
Init du modal pour la visualisation
des galeries images
*/
function init_modal(){
	
	

	$(".modal_galerie, .nom_projet a").overlay({	
		target: '#modal_projets',
		top: 40,
		
		// some expose tweaks suitable for facebox-looking dialogs 
		expose: { 
			// you might also consider a "transparent" color for the mask 
			color: '#000', 
			// load mask a little faster 
			loadSpeed: 200, 
			// highly transparent 
			opacity: 0.5 ,
			maskId : 'exposeMask'
		},
		
		api: true,
		fixed: false,
		onBeforeLoad: function(){
		
			$("#modal_projets #gallerie_projets").empty();
		
		},
		onLoad: function() { 	
			
			
			$base_url = $("#a_logo").attr("href");
			
			$aHref = this.getTrigger().attr("href");
			
			
			$("#modal_projets #gallerie_projets").append("<div id='loading_gif_div'><img id='loading_gif' src='"+$base_url+"/medias/icones/loading_black.gif' /></div>").fadeIn("fast",function(){
				
				$.post($aHref,function(contenu){
					
					
					
					$("#modal_projets #gallerie_projets").fadeOut("fast",function(){
																			  
						$("#modal_projets #gallerie_projets").empty();
						$("#modal_projets #gallerie_projets").append(contenu);
						
						
						
						$("#container_galerie_projets").cycle({
							fx:       'scrollHorz', //fade
							next:     '#next2', 
							prev:     '#prev2',
							nowrap:      1,
							speed:    200,
							timeout:  0,
							after: function (curr, next, opts) {
								var index = opts.currSlide;
								
								$('#prev2')[index == 0 ? 'hide' : 'show']();
								$('#next2')[index == opts.slideCount - 1 ? 'hide' : 'show']();
							}
						});
						
						
							
						$("#modal_projets #gallerie_projets").fadeIn(500);
																  
					});
					
				});																				  
																												  
			});
						
		}
		
	});

	$(".modal_galerie").click(function(){
		return false;
	});
	
	




}


/****************
Initalisation de la validation client du formulaire
dans la page Contact
******************/	
function validate_form_contact(){
	
	$("#form_contact").validate({
	
	
	errorPlacement: function(error, element) {
     error.appendTo( element.next(".container_error") );
	 element.next(".container_error").hide();
	 afficher_erreur(element.attr("id"));
   },
	
	errorElement:"p",
	submitHandler: function() {
	envoie_ajax_form_contact();	
	}
	
	
	
	});

}
/*
Envoie des données du form CONTACT via ajax
*/
function envoie_ajax_form_contact(){
	
	
	$("#form_contact_reponse").fadeOut("fast",function(){	
		
		$("#contact_loading").fadeIn("fast",function(){
		
				
			$url = $("#form_contact").attr("action") + "_ajax";
			$nom_personne = $("#nom_personne").val();
			$courriel_personne = $("#courriel_personne").val();
			$message_personne = $("#message_personne").val();
			$compagnie_personne = $("#compagnie_personne").val();
			
			
			$.ajax({
			  url: $url,
			  type: "POST",
			  data: { nom_personne: $nom_personne, courriel_personne: $courriel_personne, message_personne: $message_personne, compagnie_personne: $compagnie_personne  },
			  success: function(reponse){
				  
				$("#contact_loading").fadeOut("fast",function(){
					
					$("#form_contact_reponse").empty();
				
					if(reponse == "true"){
						$msg = '<p class="confirmation_contact">Merci! Nous avons bien re&ccedil;u votre message!</p>';
					
					}else{
						$msg = '<p class="erreur_contact">Oups ... un probl&egrave;me est survenu. S.V.P., r&eacute;essayez plus tard!</p>';
					
					}
					
					$(':input','#form_contact').not(':button, :submit, :reset, :hidden').val('');

					$("#form_contact_reponse").append($msg).fadeIn("fast");
				
				
				});
			  }  
			  
			});//Fin $.ajax();
	
		});//$("#loading_foot").fadeIn();
		
	
	});

}



/****************
Initalisation de la validation client du formulaire
dans le footer
******************/	
function validate_form_footer(){	
	
	
	$("#form_footer").validate({
	
	
	errorPlacement: function(error, element) {
     error.appendTo( element.next(".container_error") );
	 element.next(".container_error").hide();
	 afficher_erreur(element.attr("id"));
   },
	
	errorElement:"p",
	submitHandler: function() {
	envoie_ajax_form_footer();	
	}
	
	
	});

}



/*
Envoie des données du form via ajax
*/
function envoie_ajax_form_footer(){
	
	$("#form_footer_container").fadeOut("fast",function(){
		
		
		$("#loading_foot").fadeIn("fast",function(){
		
				
			$url = $("#form_footer").attr("action") + "_ajax";
			$courriel = $("#info_courriel").val();
			
			
			
			$.ajax({
			  url: $url,
			  type: "POST",
			  data: {info_courriel: $courriel },
			  success: function(reponse){
				$("#loading_foot").fadeOut("fast",function(){
					$("#form_footer_container").empty();
				
					if(reponse == "true"){
						$msg = '<p class="confirmation_footer">Merci! Courriel re&ccedil;u.</p>';
					
					}else{
						$msg = '<p class="confirmation_footer">Oups ... un probl&egrave;me est survenu. S.V.P., r&eacute;essayez plus tard!</p>';
					
					}				
					
					$("#form_footer_container").append($msg).fadeIn("fast");
				
				
				});
			  }  
			  
			});//Fin $.ajax();
			
	
		});//$("#loading_foot").fadeIn();
		
	});//Fin $("#form_footer_container").fadeOut()
	

}




function afficher_erreur(unId){
	var monWidth = $("#"+unId).width() + 10;
	
	
	$("#"+unId).mouseenter(function(){									
		$(this).next(".container_error").fadeIn(100);
		});
	$("#"+unId).mouseleave(function(){
		$(this).next(".container_error").fadeOut(100);
		});
	
	}



