preload = function(o, img_src, w, h){
  var img = new Image ();
  img.onload = function(){
    o.onload = null;
    if(w){o.width  = w;}
    if(h){o.height = h;}
    o.src = img.src;

	jQuery(o).fadeIn("slow");
  };
  img.src = img_src;

}

jQuery(document).ready(function () {
	jQuery("#quickAccess").css("display","none");
	
	if(navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i)) { 
		var viPhone = confirm('Souhaitez-vous être redirigé sur la version iPhone ?');
		if(viPhone){
			window.parent.location.replace("http://iphone.chaufourier.fr");
		}
	}

	$('.bt_top a').click(function () {
     $('html, body').animate({
         scrollTop: '0px'
     },
     1500);
     return false;
  });
	
	if(jQuery(".load").size() > 0){
		$(".load").each(function(){
		  	var image = $(this).attr("src");
			$(this).attr("src","img/presentation-load.jpg");
			preload(this, image);
		});
	}
	
	if(jQuery(".blank").size() > 0){
		jQuery(".blank").attr("target","_blank");
	}
	
	//realisation
	if($("#developpeur-web-realisation").size() > 0)
	{
		$("#parent1").wslide({
			width: 420,
			height: 300
		});
	
		$(".flipBT").click(function(){
			$(".grande_image span").empty();
			var chemin = $(this).find(".chemin").attr("alt");
			var description = $(this).attr("title");
				
			$("#flipBox").flip({
				direction: 'tb',
				color: 'white',
				speed: 300,
				content: $('.grande_image img').attr({ src: chemin })
			});
				
			$('.grande_image span').html("" + description + "");
			return false;
		});
			
	   $('.miniatures img').animate({"opacity": .5 });
	   $('.miniatures img').hover(function() {
	   $(this).stop().animate({ "opacity": 1 });
	    }, function() {
	    	$(this).stop().animate({ "opacity": .5 });
	   });
	}
	
	//contact
	if($("#developpeur-web-contact").size() > 0)
	{
		$("form").submit(function() {
			var error = false;
	
			if($("#nom").val() == "" && $("#societe").val() == ""){
				$("#nom").addClass("bgColorError");
				$("#societe").addClass("bgColorError");
				error = true;
			}
		
			var reg= /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
			var email = $("#mail").val();
			if(reg.test(email)==false){
				$("#mail").addClass("bgColorError");
				error = true;
			}
		
			if($("#sujet").val() == ""){
				$("#sujet").addClass("bgColorError");
				error = true;
			}
		
			if($("#message").val() == ""){
				$("#message").addClass("bgColorError");
				error = true;
			}
		
			if(error == true) return false;
		});
	
		$("input,textarea").keypress(function() {
			$(this).removeClass("bgColorError");	
		});
	
		$("#nom,#societe").keypress(function() {
			$("#nom").removeClass("bgColorError");
			$("#societe").removeClass("bgColorError");
		});
	}
});
