var cmapub = {
	init: function(){
		cmapub.devis();
		cmapub.contact();
		cmapub.swapValues();
		cmapub.pictures();
	},
	devis: function(){
		$(".rub7 .devisForm input:image").click(function(){
			var error = false;
			
			$(".rub7 form .star, .rub7 form textarea").each(function(){
				if( $(this).val() == "" )
					error = true;
			});
			
			mail = $(".rub7 form #email").val();
			
			if(mail != "" && mail.search(/^[\w\-+]+(\.[\w\-+]+)*@[\w\-]+(\.[\w\-]+)*\.[\w\-]{2,}$/) == -1)
				error = true;
			
			if( error == true )
				$(".rub7 .error").html("Veuillez rempir correctement tous les champs.");
			
			if( error == false ) {
				$.ajax({
					url: "devis_ajax.php",
					type: "POST",
					data: $(".rub7 form input:text, .rub7 form textarea, #subscribe_newsletter").serialize(),
					success: function(backDatas){
						if( backDatas == "OK" ){
							$(".rub7 form").hide();
							$(".rub7 .confirm").fadeIn();
						} else if( backDatas == "NOTOK" ){
							$(".rub7 .error").html("Une erreur est survenue, merci de remplir le formulaire une nouvelle fois.");
						}
					}
				});
			}
			
			return false;
		});
		
		$("#rub4 .textarea span a").click(function(){
			$("#rub4 form input:text, #rub4 form textarea").val("");
			return false;
		});
	},
	contact: function(){
		$(".contact #center input:image").click(function(){
			var error = false;
			
			$(".contact #center .star").each(function(){
				if( $(this).val() == "" )
					error = true;
			});
			
			mail = $(".contact #center #email").val();
			
			if(mail != "" && mail.search(/^[\w\-+]+(\.[\w\-+]+)*@[\w\-]+(\.[\w\-]+)*\.[\w\-]{2,}$/) == -1)
				error = true;
			
			if( error == true )
				$(".contact #center .error").html("Veuillez rempir correctement tous les champs.").show();
			
			var checkbox = "";
			
			$(".contact #center .domaines input:checkbox").each(function(){
				if( $(this)[0].checked )
					checkbox += $(this).val() + ", ";
			});
			
			if( error == false ) {
				$.ajax({
					url: "contact_ajax.php",
					type: "POST",
					data: $(".contact #center input:text, .contact #center textarea").serialize() + "&domaine=" + checkbox,
					success: function(backDatas){
						if( backDatas == "OK" ){
							$(".contact #center form").hide();
							$(".contact #center .confirm").fadeIn();
						} else if( backDatas == "NOTOK" ){
							$(".contact #center .error").html("Une erreur est survenue, merci de remplir le formulaire une nouvelle fois.").show();
						}
					}
				});
			}
			
			return false;
		});
	},
	swapValues: function(){
		swapValues=[];$(".swap_value").each(function(i){swapValues[i]=$(this).val();$(this).focus(function(){if($(this).val()==swapValues[i]){$(this).val("")}}).blur(function(){if($.trim($(this).val())==""){$(this).val(swapValues[i])}})});
	},
	pictures: function(){
		if( $("#right a").size() > 0 )
			$("#picture").attr("src", $("#right a:eq(0)").attr("href")).show();
		
		$("#right a").click(function(){
			href = $(this).attr("href");
			
			//$(this).wrap("<strong></strong>");
			$("#right a").removeClass("on");
			$(this).addClass("on");
			
			$("#picture").fadeOut(function(){
				$(this).attr("src", href).fadeIn();
			});
			
			return false;
		});
	}
}

$(document).ready(cmapub.init);
