function contact() {
	if ($("#contactForm").is(":hidden")){
		$("#contactForm").slideDown("slow");
		$("#backgroundPopup").css({"opacity": "0.7"});
		$("#backgroundPopup").fadeIn("slow"); 
	}
	else{
		$("#contactForm").slideUp("slow");
		$("#backgroundPopup").fadeOut("slow");  
	}
}

function makeList() {
	// Prendre les dimensions et si besoin créer une deuxième liste
	var isGalerie = $('#gallery');
	if (isGalerie.length == 0) return;
	
	var viewPortSize = $('.ad-thumbs').width();
	viewPortSize = 500;
	var limitList = viewPortSize + 100;
	var sizeList = 0;
	var idListImageOver = new Array();
	$('.ad-thumb-list li a img').each(function(img) {
		//3 border de l'image
		
		sizeList+=$(this).attr('width')+3;
		if (sizeList >=limitList) {
			// on créer une autre liste => 1/ on repère l'image courante qui a fait déborder
			idListImageOver.push($(this).attr('id'));
		}
	
	});
	if (idListImageOver.length == 0) return ; // Rien à faire
	
	// Créer une nouvelle liste
	$('<ul class="ad-thumb-list-2"></ul>').insertAfter('.ad-thumb-list');
	
	idListImageOver.forEach(function(img) {
		var pasteHtml = $('#'+img).parent().parent().html();
		pasteHtml='<li>'+pasteHtml+'</li>';
		
		$('#'+img).parent().parent().remove();
		
		$(pasteHtml).prependTo('ul.ad-thumb-list-2');
		
	});
	
	
	
}

$(document).ready(function() {
	if (!Array.prototype.forEach)
	{
	  Array.prototype.forEach = function(fun /*, thisp*/)
	  {
	    var len = this.length;
	    if (typeof fun != "function")
	      throw new TypeError(); 

	    var thisp = arguments[1];
	    for (var i = 0; i < len; i++)
	    {
	      if (i in this)
	        fun.call(thisp, this[i], i, this);
	    }
	  };
	}

	// Initi de la gallerie
	makeList();
	var isGalerie = $('#gallery');
	var galleries;

	if (isGalerie.length != 0) { // Galerie Trouvée
		
		galleries = $('.ad-gallery').adGallery({width:500,height:false});
	    galleries[0].addAnimation('wild',
	      function(img_container, direction, desc) {
	        var current_left = parseInt(img_container.css('left'), 10);
	        var current_top = parseInt(img_container.css('top'), 10);
	        if(direction == 'left') {
	          var old_image_left = '-'+ this.image_wrapper_width +'px';
	          img_container.css('left',this.image_wrapper_width +'px');
	          var old_image_top = '-'+ this.image_wrapper_height +'px';
	          img_container.css('top', this.image_wrapper_height +'px');
	        } else {
	          var old_image_left = this.image_wrapper_width +'px';
	          img_container.css('left','-'+ this.image_wrapper_width +'px');
	          var old_image_top = this.image_wrapper_height +'px';
	          img_container.css('top', '-'+ this.image_wrapper_height +'px');
	        };
	        if(desc) {
	          desc.css('bottom', '-'+ desc[0].offsetHeight +'px');
	          desc.animate({bottom: 0}, this.settings.animation_speed * 2);
	        };
	        img_container.css('opacity', 0);
	        return {old_image: {left: old_image_left, top: old_image_top, opacity: 0},
	                new_image: {left: current_left, top: current_top, opacity: 1},
	                easing: 'easeInBounce',
	                speed: 2500};
	      }
		);

	
	}
	
	$(".contact").click(function(){contact();});
	$('#myForm').submit(function(e) {
		e.preventDefault();
		$('#response').empty();
		$('#myForm #loader').css('display','inline');
		var nom = $(this).find("input[name=nom]").val();
		var entreprise = $(this).find('input[name=entreprise]').val();
		var email = $(this).find('input[name=email]').val();
		var message = $(this).find('textarea[name=message]').val();
		if (nom =='') {
			$(this).find("input[name=nom]").next('.error-message').css('display','inline');
			$('#myForm #loader').css('display','none');
		}
		else if (entreprise=="") {
			$(this).find("input[name=entreprise]").next('.error-message').css('display','inline');
			$('#myForm #loader').css('display','none');		
		}
		else if (email=="") {
			$(this).find("input[name=email]").next('.error-message').css('display','inline');
			$('#myForm #loader').css('display','none');		
		}
		else if (message=="") {
			$(this).find("textarea[name=message]").next('.error-message').css('display','inline');
			$('#myForm #loader').css('display','none');		
		}
		else {
			$('#myForm .error-message').hide();
			$.post("http://www.construction-qualite.fr/devis",{nom:nom,entreprise:entreprise,email:email, message:message},
				function(data) {
					$('#myForm #loader').css('display','none');
					$('#response').prepend(data).fadeIn(2000);			
			});
			
		}
		return false;
	});

	
	$('#sidebar .img-hover').css('opacity',0);	
	$('#mAccueil').hover(function() {
		$("#mAccueil .img-hover").stop().animate({"opacity":1},1200) ;
	},
	function() {
		$("#mAccueil .img-hover").stop().animate({"opacity":0},1200) ;
	});
	$('#mRealisations').hover(function() {
			$("#mRealisations .img-hover").stop().animate({"opacity":1},1200) ;
		},
		function() {
			$("#mRealisations .img-hover").stop().animate({"opacity":0},1200) ;
	});
	$('#mPartenaires').hover(function() {
			$("#mPartenaires .img-hover").stop().animate({"opacity":1},1200) ;
		},
		function() {
			$("#mPartenaires .img-hover").stop().animate({"opacity":0},1200) ;
	});
	/* Gestion des choix du slider */
	
	var present = $('#references').length;
	
	if (present) {
		var mySlider = $(".slider").easySlider();

		/* Par défaut le 1er Slider est actif au chargement de la page */
		$('#slider-command-choix1').css('display','block');
		
		$('#slider-choice a').click(function(e){
			e.preventDefault();
			var oldChoix = $('#slider-choice a.jq-checked').parent().attr("id");;
			$('#slider-choice a.jq-checked').removeClass('jq-checked');
			$(this).addClass('jq-checked');
			var newChoix= $(this).parent().attr("id");
			$('#references').animate({'opacity':0},1000, function() {
				// Clean up 
				$('#slider-command-'+oldChoix).css('display','none');
				$('#slider-command-'+newChoix).css('display','block');
				$('.slider.selected').removeClass('selected');
				$('.slider.'+newChoix).addClass('selected');
				$('#references').animate({'opacity':1},1000);
			});
		});
		
	
	}
	
});
