var HTTPPATH = "http://www.studio4fun.ca/images";

$(function() {
	var Lang = $("body").attr("lang");
	$("input[name=logIn]").click(function(event){
		var nobug = true;
		var msg = "";
		var tableau = new Array( true,"","" );	
		var errorsList = "";
		
		est_la("identifiant", tableau, "Veuillez indiquer votre identifiant /", "identifiant /");
		est_la("password", tableau, "Veuillez indiquer votre mot de passe /", "password /");

		if( tableau[0] != false ) {
			var identifiant = $("#identifiant").val();
			var password = $("#password").val();
			$.ajax({
				type: "POST",
				url: "/includes/php/logIn.php",
				data:"lang="+Lang+"&identifiant="+identifiant+"&password="+password,
				success: function(response) {
					if(response=="false")
						$("#loginErrors").html("<li>Mauvais User/Login</li>").slideDown(500);
					else
						window.location.href="/"+Lang+"/dispatch.html";
				}
			});
		} else {
			var aMSG = tableau[1].split(" /");
			for( var i = 0; i < aMSG.length; i++ ) {
				if( aMSG[i] != "" )
					errorsList += "<li>"+aMSG[i]+"</li>";
			}

			$("#loginErrors").html(errorsList).slideDown(500);
		}

		return false;
	});
	$("input[name=submit]").click(function(event){
		var nobug = true;
		var msg = "";
		var tableau = new Array(true,"","");
		var errorsList = "";

		est_la("user", tableau, " /", "user /");
		est_la("password01", tableau, "Veuillez indiquer votre mot de passe /", "password01 /");
		est_la("password02", tableau, "Veuillez indiquer votre mot de passe /", "password02 /");
		est_la("email", tableau, "Veuillez indiquer votre mot de passe /", "email /");
		est_courriel_valide("email", tableau, "Veuillez indiquer votre mot de passe /", "email /");
		est_la("fname", tableau, "Veuillez indiquer votre mot de passe /", "fname /");
		est_la("lname", tableau, "Veuillez indiquer votre mot de passe /", "lname /");
		est_la("phone", tableau, "Veuillez indiquer votre mot de passe /", "phone /");
		est_la("acceptTerms", tableau, "Veuillez indiquer votre mot de passe /", "acceptTerms /");
		est_la("over18", tableau, "Veuillez indiquer votre mot de passe /", "over18 /");
		est_la("captcha", tableau, "Veuillez indiquer votre mot de passe /", "captcha /");

		if(tableau[0] != false) {
			var captcha = $("#captcha").val();
			$.ajax({
				type: "POST",
				url: "/includes/php/checkCaptcha.php",
				data:"lang="+Lang+"&captcha="+captcha,
				success: function(response) {
					if(response=="false")
						$("#errors").html("<li>"+ErrorInvalidCaptcha+"</li>").slideDown(500);
					else {
						$("form[name=thisForm]").attr("action","post.html").submit();
					}
				}
			});
		} else {
			var aMSG = tableau[1].split(" /");
			for( var i = 0; i < aMSG.length; i++ ) {
				if( aMSG[i] != "" )
					errorsList += "<li>"+aMSG[i]+"</li>";
			}
			
			$("#errors").html(errorsList).slideDown(500);
		}

		return false;
	});

	// Contact form
	$("input[name=send]").click(function(event){
		var nobug = true;
		var msg = "";
		var tableau = new Array( true,"","" );
		var errorsList = "";
		
		est_la("name", tableau, "Veuillez indiquer votre votre <strong>Nom</strong> /", "name /");
		est_la("email", tableau, "Veuillez indiquer votre votre <strong>Courriel</strong> /", "email /");
		est_courriel_valide("email", tableau, "Veuillez indiquer une adresse courriel valide /", "email /");
		est_la("subject", tableau, "Veuillez indiquer le <strong>Sujet</strong> /", "subject /");
		est_la("message", tableau, "Veuillez insérer un commentaire /", "message /");
		est_la("recaptcha_response_field", tableau, "Veuillez confirmer que vous n'êtes pas un robot /", "recaptcha_response_field /");
		
		if(tableau[0]!=false) {
			var captcha = $("#recaptcha_response_field").val();
			var recaptcha_challenge_field = $("#recaptcha_challenge_field").val();
			$.ajax({
				type: "POST",
				url: "/includes/php/recaptcha.php",
				data: "recaptcha_response_field="+captcha+"&recaptcha_challenge_field="+recaptcha_challenge_field,
				success: function(response) {
					if(response=="false") {
						$("#errors").html("<li>Captcha invalide</li>").slideDown(500);
					} else {
						var name = $("input[name=name]").val();
						var email = $("input[name=email]").val();
						var subject = $("input[name=subject]").val();
						var message = $("textarea[name=message]").val();
						var dataString = "lang="+Lang+"&name="+name+"&email="+email+"&subject="+subject+"&message="+message;
						$.ajax({
							type: "POST",
							url: "/includes/php/sendContact.php",
							data: dataString,
							success: function(response) {
								$("form[name=thisForm]").slideUp(1000,function(event) {
									$("form[name=thisForm]").html("<fieldset style='width:400px;'>"+sendContact+"</fieldset>").slideDown(1000);
								});
							}
						});
					}
				}
			});
		} else {
			var aMSG = tableau[1].split(" /");
			for( var i = 0; i < aMSG.length; i++ ) {
				if( aMSG[i] != "" )
					errorsList += "<li>"+aMSG[i]+"</li>";
			};
			
			$("#errors").html(errorsList).slideDown(500);
		};
		    
		return false;
	});
	
	$("a[name=changeMonth]").click(function(event) {
		var date = $(this).attr("href");
		$("input[name=date]").val(date);
		$("form[name=changeMonth]").submit();
		return false;
	});
	
	$(".periods li a").hover(function() {
		$(this).children(".tooltip").stop(true, true).animate({opacity: "show", top: "-28"}, "fast");
	}, function() {
		$(this).children(".tooltip").animate({opacity: "hide", top: "-50"}, "fast");
	});
	
	$(".periods li a.reserve").click(function(event) {
		var period = $(this).attr("period");
		var date = $(this).attr("date");
		var userStatus = $(this).attr("usersStatus");
		var reservations_id = $(this).attr("id");
		
		if(userStatus=="admin") {
			var thisPeriod = $(this);
			var Lang = $("body").attr("lang");
			var status = $(this).attr("status");
			var studios_fk = $("input[name=studios_fk]").val();
			$.ajax({
				type: "POST",
				url: "/includes/php/blockPediod.php",
				data:"Lang="+Lang+"&date="+date+"&period="+period+"&studios_fk="+studios_fk+"&reservations_id="+reservations_id,
				success: function(response) {
					if(response!="") {
						thisPeriod.removeClass("disponible");
						thisPeriod.addClass("reserved");
						thisPeriod.attr("id",response);
					} else {
						thisPeriod.removeClass("reserved");
						thisPeriod.addClass("disponible");
						thisPeriod.attr("id","");
					}
				}
			});
		} else {
			$("input[name=pediode]").val(period);
			$("input[name=dayDate]").val(date);
			$("form[name=thisForm]").submit();
		}

		return false;
	});
	
	
	
	// Video SWF FILLE/GARS
	$("div#playerVIDEO").flash({
		src: HTTPPATH+"/flvPlayer.swf",width: 334, height:402},{ version: 9 },
		function(htmlOptions) {
			$this = $(this);
			htmlOptions.flashvars.flvURL = $(this).attr("flvURL");
			htmlOptions.wmode = 'transparent';
			this.innerHTML = '';
			$(this).addClass('').prepend($.fn.flash.transform(htmlOptions));
			return false;
	});
	$("div#playerVIDEO300").flash({
		src: HTTPPATH+"/flvPlayer.swf",width: 300, height:361},{ version: 9 },
		function(htmlOptions) {
			$this = $(this);
			htmlOptions.flashvars.flvURL = $(this).attr("flvURL");
			htmlOptions.wmode = 'transparent';
			this.innerHTML = '';
			$(this).addClass('').prepend($.fn.flash.transform(htmlOptions));
			return false;
	});
	
	
	// Video SWF FILLE/GARS
	$("div#playerVIDEOSmall").flash({
		src: HTTPPATH+"/flvPlayer.swf",width: 200, height:240.71},{ version: 9 },
		function(htmlOptions) {
			$this = $(this);
			htmlOptions.flashvars.flvURL = $(this).attr("flvURL");
			htmlOptions.wmode = 'transparent';
			this.innerHTML = '';
			$(this).addClass('').prepend($.fn.flash.transform(htmlOptions));
			return false;
	});
});

