initialMarginLeft = -530;
strenghtParallax = 30;
widthPortfolio = 710;
marginPortfolio = 5;
maxIndex = 10;
$(document).ready(function() {
	//numVer = parseInt($.browser.version, 10);
	//if (!($.browser.msie)) {
			
		/////////////// parallax
		if (!($.browser.msie)) {
			$(document).mousemove(function(e) {
				percent = e.clientX/$(window).width();
				p1st = Math.ceil(percent*strenghtParallax)+initialMarginLeft;
				p2d = Math.ceil(percent*strenghtParallax/2)+initialMarginLeft;
				p3d = Math.ceil(percent*strenghtParallax/4)+initialMarginLeft;
				$("li#p1st").css("margin-left",p1st+"px");
				$("li#p2d").css("margin-left",p2d+"px");
				$("li#p3d").css("margin-left",p3d+"px");
				$("#blackFox").css("margin-left",p1st+"px");
			});
		}
			
		////////////// show/hide texte home
		$("#hiddenText").hide();
		$("a#notreDada").css('display','block');
		$("#hiddenText").slideUp("fast");
		$("a#notreDada").click(function() {
			$(this).css('z-index','100');
			if ($(this).css('text-align') == 'right') {
				$(this).css('background-position','right 0'); 
				$(this).css('text-align','left');
				$(this).animate({'width':'345px'},200,function() {
					$("#featuredText").slideUp("fast");
					$("#hiddenText").slideDown("slow");	
				});
			} else {
				$(this).css('background-position','right -36px');
				$(this).animate({'width':'100px'},200,function() {
					$(this).css('text-align','right');
					$("#featuredText").slideDown("fast");
					$("#hiddenText").slideUp("slow");
				});
			}
			return false;
		});
		$("a#notreDada").hover(function() {
			if ($(this).css('text-align') == 'left') {
				$(this).css('background-position','right -145px');
			} else {
				$(this).css('background-position','right -181px');
			}
		},	function() {
				if ($(this).css('text-align') == 'left') {
					$(this).css('background-position','right 0');
				} else {
					$(this).css('background-position','right -36px');
				}
		});
	
		//////////////// portfolio 
		// transition
		$("#hiddenText h3").css("display","none");
		$("li a#panorama").css("display","block");
		$("li a#panorama").click(toPortfolio);
		$("#contactP").click(function() {
			backFromPortfolio();
			$("#footer").fadeTo(900, 1, function(){
				toContact();
			});
		});
		$("a#backP").click(backFromPortfolio);
		// en savoir +
		$("#portfolio li .knowMoreText").fadeTo(0, 0);
		$("#portfolio li h3").fadeTo(0, 1);
		$("a.knowMore").hover(function() {
			daddy = $(this).parent();
			$(".knowMoreText",daddy).fadeTo('fast', 1);
		},	function() {
			daddy = $(this).parent();
			$(".knowMoreText",daddy).fadeTo('slow',0);
		});
	
		// slider
		currentIndex = 0;
		$('#portfolio li').css({ opacity: 0.3 });
		$('#portfolio li:first').css({ opacity: 1 });
		$('a#rightArrow').click(goLeft);
		$('a#leftArrow').click(goRight); 
		$('a#rightArrow').hover(function() {
			nextIndex = currentIndex+1;
			if (nextIndex != maxIndex) {
				$("#portfolio li:eq("+nextIndex+")").css("opacity","0.5");
				$(this).css("background-position","0 -167px");
			}
		},function() {
			nextIndex = currentIndex+1;
			if (nextIndex != maxIndex) {
				$("#portfolio li:eq("+nextIndex+")").css("opacity","0.3");
				$(this).css("background-position","0 -37px");
			}
		});
		$('a#leftArrow').hover(function() {
			prevIndex = currentIndex-1;
			if (prevIndex != -1) {
				$("#portfolio li:eq("+prevIndex+")").css("opacity","0.5");
				$(this).css("background-position","0 -212px");
			}
		},function() {
			prevIndex = currentIndex+1;
			if (prevIndex != -1) {
				$("#portfolio li:eq("+prevIndex+")").css("opacity","0.3");
				$(this).css("background-position","0 -82px");
			}
		});
		showHideLeftRight();
	
		/////////////// CONTACT
		$("#contactForm").css("display","block");
		$("a#contact").click(toContact);
		$("a#returnC").click(backFromContact)
		$("a#panoramaC").click(function() {
			backFromContact();
			$("#footer").fadeTo(900, 1, function(){
				toPortfolio();
			});
		});
	
		//envoi formulaire - erreurs
		$("#ok").click(function() {
            var emptyName = false;
			var emptyCompany = false;
			var beCalled = false;
			var emptyPhone = false;
			var beNews = false;
            var emptyMail = false;
            var goodMail = true;

			var company = $("#yourName").val();
            if (company == "") {
                $("#yourNameLabel").addClass('error');
                $("#yourName").addClass('errorInput');
                emptyCompany = true;
            } else {
                $("#yourNameLabel").removeClass('error');
                $("#yourName").removeClass('errorInput');
                emptyCompany = false;
            }

            var name = $("#company").val();
            if (name == "") {
                $("#companyLabel").addClass('error');
                $("#company").addClass('errorInput');
                emptyName = true;
            } else {
                $("#companyLabel").removeClass('error');
                $("#company").removeClass('errorInput');
                emptyName = false;
            }

			if($("#callMe:checked").val() == "on") {
				beCalled = true;
				var tel = $("#yourTel").val();
	            if (tel == "") {
	                $("#yourTelLabel").addClass('error');
	                $("#yourTel").addClass('errorInput');
	                emptyPhone = true;
	            } else {
	                $("#yourTelLabel").removeClass('error');
	                $("#yourTel").removeClass('errorInput');
	                emptyPhone = false;
	            }
			} else {
				$("#yourTelLabel").removeClass('error');
                $("#yourTel").removeClass('errorInput');
                emptyPhone = true;
			}
			
			if($("#newsLaitue:checked").val() == "on") {
				beNews = true;
				var email = $("#email").val();
	            if (email == "") {
	                $("#emailLabel").addClass('error');
	                $("#email").addClass('errorInput');
	                emptyMail = true;
	            } else {
	                emptyMail = false;
	                if (isValidEmailAddress(email)) {
	                    $("#emailLabel").removeClass('error');
	                    $("#email").removeClass('errorInput');
	                    goodMail = true;
	                } else {
	                    $("#emailLabel").addClass('error');
	                    $("#email").addClass('errorInput');
	                    goodMail = false;
	                }
	            }
			} else {
				$("#emailLabel").removeClass('error');
                $("#email").removeClass('errorInput');
                goodMail = true;
				emptyMail = true;
			}

            
            var messageErreur = '';
            if (emptyName || emptyCompany) {
                messageErreur = 'Les champs Nom et Société sont obligatoires.';
            } else {
				if (beNews || beCalled) {
					if (beNews) {
						if (emptyMail) {
							messageErreur = 'Pour recevoir la newslaitue, nous avons besoin de votre email.';
						} else {
							if (!goodMail) {
								messageErreur = 'Votre email ne semble pas valide.';
							}
						}
					}
					if (beCalled) {
						if (emptyPhone) {
							messageErreur = 'Pour vous rappeler, nous avons besoin de votre numéro.';	
						}
					}
				} else {
					messageErreur = "Vous devez cocher au moins une des deux cases.";
				}
            }
            if (messageErreur != '') {
                $('#errorMsg').html(messageErreur);
				$('#errorMsg').fadeTo('fast',1);
                return false;
            } else {
            	// envoi
	            var dataString = 'yourName=' + escape(name) + '&societe=' + escape(company) + '&beCalled=' + escape(beCalled) + '&beNews=' + escape(beNews) + '&tel=' + escape(tel) + '&email=' + escape(email) + '&inAction=ajaxSubmit';      
				$.ajax({
					type: "POST",url: "_files/contactPost.php",data: dataString,success: function() {
						$("#formContact").fadeTo('fast',0,function() {
							$("#formContact").html("<h3>Merci!</h3><p style='padding-left:25px'>Votre message a bien été envoyé.");
							$("#formContact").fadeTo('slow',1);
						})
						return false;
					}
	            });
	            return false;
			}
        });
	//}
});

function showHideLeftRight() {
	var players = $('.vimeo');
	$f(players[0]).api('pause');
	if (currentIndex == 0) {
		$('a#leftArrow').css("display","none");
		$('#maskVimeo').css("display","none");
	} else {
		$('a#leftArrow').css("display","block");
		$('#maskVimeo').css("display","block");
	}
	if (currentIndex == maxIndex-1) {
		$('a#rightArrow').css("display","none");
	} else {
		$('a#rightArrow').css("display","block");
	}
	nextIndex = currentIndex+1;
	if (nextIndex != maxIndex) {
		$("#portfolio li:eq("+nextIndex+")").unbind("mouseenter mouseleave click");
		$("#portfolio li:eq("+nextIndex+")").css("cursor","pointer");
		$("#portfolio li:eq("+nextIndex+")").css("opacity","0.5");
		$("#portfolio li:eq("+nextIndex+")").hover(function() {
			$(this).css("opacity","0.5");
			$("#rightArrow").css("background-position","0 -167px");
		},function() {
			$(this).css("opacity","0.3");
			$("#rightArrow").css("background-position","0 -37px");
		});
		$("#portfolio li:eq("+nextIndex+")").click(goLeft);
	}
	prevIndex = currentIndex-1;
	if (prevIndex != -1) {
		$("#portfolio li:eq("+prevIndex+")").unbind("mouseenter mouseleave click");
		$("#portfolio li:eq("+prevIndex+")").css("cursor","pointer");
		$("#portfolio li:eq("+prevIndex+")").css("opacity","0.5");
		$("#portfolio li:eq("+prevIndex+")").hover(function() {
			$(this).css("opacity","0.5");
			$("#leftArrow").css("background-position","0 -212px");
		},function() {
			$(this).css("opacity","0.3");
			$("#leftArrow").css("background-position","0 -82px");
		});
		$("#portfolio li:eq("+prevIndex+")").click(goRight);
	}
}

function goRight() {
	if (currentIndex>0) {
		$("#portfolio li:eq("+currentIndex+")").fadeTo('slow',0.3);
		currentIndex--;
		newMargin =-(widthPortfolio+marginPortfolio)*currentIndex-widthPortfolio/2;
		$("#portfolio li:eq("+currentIndex+")").unbind("mouseenter mouseleave click");
		$("#portfolio li:eq("+currentIndex+")").css("cursor","default");
		$('#portfolio').animate({'margin-left':newMargin},200, function() {
			$("#portfolio li:eq("+currentIndex+")").fadeTo('slow',1);
		});
	}
	$("#rightArrow").css("background-position","0 -37px");
	showHideLeftRight();
	return false;
}

function goLeft() {
	if (currentIndex<(maxIndex-1)) {
		$("#portfolio li:eq("+currentIndex+")").fadeTo('slow',0.3);
		currentIndex++;
		newMargin = -(widthPortfolio+marginPortfolio)*currentIndex-widthPortfolio/2;
		$("#portfolio li:eq("+currentIndex+")").unbind("mouseenter mouseleave click");
		$('#portfolio').animate({'margin-left':newMargin},200, function() {
			$("#portfolio li:eq("+currentIndex+")").fadeTo('slow',1);
		});
	}
	$("#leftArrow").css("background-position","0 -82px");
	showHideLeftRight();
	return false;
}

function backFromContact() {
	$("#content").animate({"left":"50%","margin-left":"-530px"},900, function() {
			$("#parallax").css({"z-index":"6"});
	});
	$("#p4th").animate({"left":"50%"},500);
	$("#p3d").animate({"left":"50%"},700);
	$("#p2d").animate({"left":"50%"},800);
	$("#p1st").animate({"left":"50%"},900);
	$("#contactForm").animate({"right":"150%"},900);
	$("ul#OnFaitQuoi").fadeTo(600,1);
	$("#content h2").fadeTo(600,1);
	return false;
}

function backFromPortfolio() {
	var players = $('.vimeo');
	$f(players[0]).api('pause');
	$("#portfolioContainer").animate({"top":"-150%"},800);
	$("#backFromPortfolio").animate({"bottom":"-50px"},300, function() {
		$("#backFromPortfolio").css("display","none");
		$("#p4th").animate({"bottom":"27px"},400);
		$("#p3d").animate({"bottom":"27px"},600);
		$("#p2d").animate({"bottom":"27px"},500);
		$("#content").css("display","block");
		$("#content").animate({"bottom":"18%"},600);
	});
	$("#p1st").css({"display":"block"});
	$("#p1st").animate({"bottom":"27px"},800);
	$("#blackFox").animate({"bottom":"27px","opacity":"0"},800, function() {
		$("#p1st").css("z-index","6");
		$("#blackFox").css("display","none");
	});
	return false;
}

function toContact() {
	$("#parallax").css({"z-index":"19"});
	$("#content").animate({"left":"75%","margin-left":"0"},900);
	$("#p4th").animate({"left":"90%"},500);
	$("#p3d").animate({"left":"90%"},700);
	$("#p2d").animate({"left":"90%"},800);
	$("#p1st").animate({"left":"90%"},900);
	$("#contactForm").animate({"right":"45%"},900);
	$("ul#OnFaitQuoi").fadeTo(600,0);
	$("#content h2").fadeTo(600,0);
	return false;
}

function toPortfolio() {
	$("#portfolioContainer").animate({"top":"0%"},800);
	$("#content").animate({"bottom":"-100%"},600,function() {
		$("#content").css("display","none");
	});
	$("#p4th").animate({"bottom":"-365px"},400, function() {
		$("#backFromPortfolio").css("display","block");
		$("#backFromPortfolio").animate({"bottom":"27px"},300);
	});
	$("#p3d").animate({"bottom":"-134px"},600);
	$("#p2d").animate({"bottom":"-96px"},800);
	$("#p1st").animate({"bottom":"-330px"},800);
	$("#p1st").css({"z-index":"16"});
	$("#blackFox").css({"opacity":"0","display":"block"});
	$("#blackFox").animate({"bottom":"-330px","opacity":"1"},800, function() {
		$("#p1st").css({"display":"none"});
	});
	return false;
}

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}
