$(document).ready(function(){

    // slideshow
    $('#slideshow').cycle({
		    fx: 'uncover', // transition type, ex: fade, scrollUp, shuffle, etc... blindY
		    prev:    '#prev',
        next:    '#next', 
        pause:         1,
        timeout: 5000
	  });

    // Contact form
    $("input.phone").mask("99.99.99.99.99");

		$('#contactForm #submit').click(function() {
		// Fade in the progress bar
		$('#contactForm #formProgress').hide();
		$('#contactForm #formProgress').html('Sending&hellip;');
		$('#contactForm #formProgress').fadeIn();
		
		// Disable the submit button
		$('#contactForm #submit').attr("disabled", "disabled");
		
		// Clear and hide any error messages
		$('#contactForm .formError').html('');
		
		// Set temporary variables for the script
		var isFocus=0;
		var isError=0;
		
		// Get the data from the form
		var name=$('#contactForm #name').val();
		var email=$('#contactForm #email').val();
    var phone=$('#contactForm #phone').val();
		var subject=$('#contactForm #subject').val();
		var message=$('#contactForm #message').val();
		
		// Validate the data
		if(name=='') {
			$('#contactForm #errorName').html('Ce champ est requis.');
			$('#contactForm #name').focus();
			isFocus=1;
			isError=1;
		}
		if(email=='') {
			$('#contactForm #errorEmail').html('Ce champ est requis.');
			if(isFocus==0) {
				$('#contactForm #email').focus();
				isFocus=1;
			}
			isError=1;
		} else {
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test(email)==false) {
				$('#contactForm #errorEmail').html('Adresse Email invalide');
				if(isFocus==0) {
					$('#contactForm #email').focus();
					isFocus=1;
				}
				isError=1;
			}
		}
		if(message=='') {
			$('#contactForm #errorMessage').html('Ce champ est requis.');
			if(isFocus==0) {
				$('#contactForm #message').focus();
				isFocus=1;
			}
			isError=1;
		}


                if(subject=='') {
                        $('#contactForm #errorSubject').html('Ce champ est requis.');
                        if(isFocus==0) {
                                $('#contactForm #subject').focus();
                                isFocus=1;
                        }
                        isError=1;
                }

		if(phone=='') {
			$('#contactForm #errorPhone').html('Ce champ est requis.');
			if(isFocus==0) {
				$('#contactForm #phone').focus();
				isFocus=1;
			}
			isError=1;
		}
		
		// Terminate the script if an error is found
		if(isError==1) {
			$('#contactForm #formProgress').html('');
			$('#contactForm #formProgress').hide();
			
			// Activate the submit button
			$('#contactForm #submit').attr("disabled", "");
			
			return false;
		}
		
		$.ajaxSetup ({
			cache: false
		});
		
		var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&subject=' + subject + '&message=' + message;  
		$.ajax({
			type: "POST",
			url: "apps/contact.php",
			data: dataString,
			success: function(msg) {
				
				// Check to see if the mail was successfully sent
				if(msg=='ok') {
					// Update the progress bar
					$('#contactForm #formProgress').html('Message envoyé.').delay(2000).fadeOut(400);
					
					// Clear the subject field and message textbox
					$('#contactForm #subject').val('');
					$('#contactForm #message').val('');
				} else {
					$('#contactForm #formProgress').html('');
					alert('Une erreur s\'est produite. Merci de réessayer.');
				}
				
				// Activate the submit button
				$('#contactForm #submit').attr("disabled", "");
			},
			error: function(ob,errStr) {
				$('#contactForm #formProgress').html('');
				alert('Une erreur s\'est produite. Merci de réessayer.');
				
				// Activate the submit button
				$('#contactForm #submit').attr("disabled", "");
			}
		});
		
		return false;
	});


	
	// Back to (the futur) top
	$('a[href=#top]').click(function(){
        $('html, body').animate({scrollTop:0}, 'slow');
        return false;
    });
	
	// background hover
	$("#content a img").hover(function() {
   	 	$(this).stop().animate({ "opacity": 0.5 }, 400);
    	},function() {
		$(this).stop().animate({ "opacity": 1 }, 400);
	});
	
	
  // icon number hover
	$("#icon-div-1").hover(function() {
   	 	$("#icon-hover-1").stop().animate({ backgroundColor: "#231F20", color: "#EC008C"}, 400);
    	},function() {
		$("#icon-hover-1").stop().animate({ backgroundColor: "white" }, 400);
	});
	
	$("#icon-div-2").hover(function() {
   	 	$("#icon-hover-2").stop().animate({ backgroundColor: "#231F20", color: "#EC008C"}, 400);
    	},function() {
		$("#icon-hover-2").stop().animate({ backgroundColor: "white" }, 400);
	});
	
	$("#icon-div-3").hover(function() {
   	 	$("#icon-hover-3").stop().animate({ backgroundColor: "#231F20", color: "#EC008C"}, 400);
    	},function() {
		$("#icon-hover-3").stop().animate({ backgroundColor: "white" }, 400);
	});
		
});

