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)
						window.location.href = "/" + Lang + "/dispatch.html";
					else
						$("#loginErrors").html("<li>Mauvais User/Login</li>").slideDown(500);
				}
			});
		} 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, no_password1, "password01 /");
		est_la("password02", tableau, no_password2, "password02 /");
		est_la("email", tableau, no_email, "email /");
		est_courriel_valide("email", tableau, email_valid, "email /");
		est_la("fname", tableau, no_fname, "fname /");
		est_la("lname", tableau, no_lname, "lname /");
		est_la("phone", tableau, no_phone, "phone /");
		est_la("acceptTerms", tableau, no_terms, "acceptTerms /");
		est_la("over18", tableau, no_18, "over18 /");
		est_la("captcha", tableau, no_captcha, "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;
	});*/



	$(".periods li a.reserve").click(function(event) {
		var period = $(this).attr("href");
		var studio_fk = $("input[name=studios_fk]").val();
		var nyro = true;

		if($(this).attr("rel")) {
			$.ajax({
				type: "POST",
				url: "/includes/php/add.php",
				data:"id=&type=RES&period=" + period + "&studio_fk=" + studio_fk,
				success: function(response) {
					$("a.reserve").attr("rel", "1");
				}
			});
		} else {
			$.nyroModalManual({
				bgColor: "#000000",
				url: "/choosePackage.php?period=" + period + "&studio_fk=" + studio_fk
			});
			
			nyro = false;
		}

		var nb_item = (Number($("span#nb_item").html()) + 1);
		$("span#nb_item").html(nb_item);

		$(this).parent("li").html("<em class='reserved'>&nbsp;</em>");
		$(this).remove();

		if(Number($("span#nb_item").html()) == 1 && nyro) {
			$.nyroModalManual({
				bgColor: "#000000",
				url: "/oneItem.php?lang=" + Lang + "&period=" + period
			});			
		}

		return false;
	});

	$(".periods li a.reserve_for_user").click(function(event) {
		var period = $(this).attr("href");
		var studio_fk = $("input[name=studios_fk]").val();
		var user_fk = $("input[name=user_fk]").val();

		if($(this).attr("rel")) {
			$.ajax({
				type: "POST",
				url: "/includes/php/add.php",
				data:"id=&type=RES&period=" + period + "&studio_fk=" + studio_fk +"&user_fk=" + user_fk,
				success: function(response) { $("a.reserve").attr("rel", "1"); }
			});
		} else {
			$.nyroModalManual({
				bgColor: "#000000",
				url: "/choosePackage.php?period=" + period + "&studio_fk=" + studio_fk + "&user_fk=" + user_fk
			});
		}
		
		var nb_item = (Number($("span#nb_item").html()) + 1);
		$("span#nb_item").html(nb_item);
		
		$(this).parent("li").html("<em class='reserved'>&nbsp;</em>");
		$(this).remove();

		return false;
		return false;
	});
	
	$(".periods li a.reserve_admin").click(function(event) {
		var period = $(this).attr("href");
		var studio_fk = $("input[name=studios_fk]").val();
		var type = $(this).attr("type");
		var this_link = $(this);
		
		$.ajax({
			type: "POST",
			url: "/includes/php/block_unblock.php",
			data:"type="+ type + "&period=" + period + "&studio_fk=" + studio_fk,
			success: function(response) {
				//alert(response);
				
				$("a.reserve").attr("rel", "1");

				if(type == "block")
					this_link.parent("li").html("<em class='reserved'>&nbsp;</em>");
				else {
					this_link.removeClass("reserved");
					this_link.addClass("disponible");
				}
			}
		});

		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;
	});
	
	
	
	$("input[name=validate_codepromo]").click(function(e){
		var promocode = $("input[name=codepromo]").val();
		var type = $("input[name=type]").val();
		
		$.ajax({
			type: "POST",
			url: "/includes/php/valide_promocode.php",
			data:"promocode="+promocode+"&type="+type,
			//dataType: "json",
			success: function(response) {
				if(response)
					window.location.reload();
				else {
					$("input[name=codepromo]").val("");
					$("input[name=codepromo]").attr("placeholder", "Invalid code !");
					$("input[name=codepromo]").css({border : "1px red solid"});
				}
					

				/*if(response.error) {
					alert("Invalid code.");
				} else {
					$("#form_promocode").submit();
				}*/
			}
		});

		return false;
	});
	
	
	
	
	
	
	$("a.remove").click(function(){
		if(confirm(confirm_suppress)) {
			var row_id = $(this).attr("href");
	
			$.ajax({
				type: "POST",
				url: "/includes/php/remove.php",
				data:"row_id=" + row_id,
				success: function(response) {
					window.location.reload();
				}
			});
		}
		
		return false;
	});

	$("a.add").click(function(){
		var type = $(this).attr("rel");
		var id = $(this).attr("href");
		var params = "id=" + id + "&type=" + type;

		if(type == "RES")
			params += "&period=" + id + "&studio_fk=" + $(this).attr("studio_fk");

		$.ajax({
			type: "POST",
			url: "/includes/php/add.php",
			data:params,
			success: function(response) {
				if(type == "FOR")
					window.location.href = "/en/cart.html";
			}
		});

		return false;
	});
	
	
	
	$("a.credit").click(function(){
		var id = $(this).attr("href");
		var value = $(this).attr("value");

		$.ajax({
			type : "POST",
			url : "/includes/php/substract_credits.php",
			data : "id=" + id + "&amount=" + value,
			success : function(response) {
				window.location.reload();
			}
		});

		$(this).addClass("credit_selected");
		$(this).removeClass("credit");

		return false;
	});
});

