$(document).ready(function() {
		var titleposition = ($.browser.msie && $.browser.version.substring(0,1) == '6') ? 'inside' : 'outside';

		$(".fancybox").fancybox({
			'hideOnContentClick': false,
			'cyclic': true,
			'titlePosition': titleposition
		});

		$(".fancybox-staticmap").fancybox({
			'hideOnContentClick': false,
			'width': 370,
			'height': 370,
			'titlePosition': titleposition
		});

		$(".fancybox-googlemap").fancybox({
			'hideOnContentClick': false,
			'width': 705,
			'height': 530,
			'titlePosition': titleposition
		});

		$(".fancybox-search").fancybox({
			'hideOnContentClick': false,
			'width': 700,
			//'height': 530,
			'titlePosition': titleposition
		});
		$(".fancybox-search-static").fancybox({
			'hideOnContentClick': false,
			'width': 375,
			'height': 330,
			'titlePosition': titleposition
		});

		$(".fancybox-summary").fancybox({
			'hideOnContentClick': false,
			'width': 960,
			'height': 450,
			'titlePosition': titleposition
		});


		$(".fancybox-hotel").fancybox({
			'hideOnContentClick': false,
			'width': 720,
			'height': 760,
			'titlePosition': titleposition
		});

		$(".fancybox-search-hotel").fancybox({
			'hideOnContentClick': false,
			'width': 700,
			'titlePosition': titleposition
		});

		$(".fancybox-account").fancybox({
			'hideOnContentClick': false,
			'width': 720,
			'height': 760,
			'titlePosition': titleposition
		});

		$(".fancybox-booking").fancybox({
			'hideOnContentClick': false,
			'width': 720,
			'height': 500,
			'titlePosition': titleposition
		});

		$(".fancybox-moderation").fancybox({
			'hideOnContentClick': false,
			'width': 800,
			'height': 900,
			'titlePosition': titleposition
		});

		$(".fancybox-booking-details").fancybox({
			'hideOnContentClick': false,
			'width': 1000,
			'height': 1000,
			'titlePosition': titleposition
		});

		$(".fancybox-voucher").fancybox({
			'hideOnContentClick': false,
			'width': 400,
			'height': 160,
			'titlePosition': titleposition
		});

		$(".fancybox-friend").fancybox({
			'scrolling': 'no',
			'autoDimensions': true,
			'autoScale': false,
			'titlePosition': titleposition,
			'onClosed': function() {
				$(".error-msg").hide();
				$(".msg").hide();
				$("#sendtofriendform").show();
				$('#comments').css('float', 'right');
				$('#required').css('float', 'right');
				$.fancybox.resize();
			},
			'onComplete': function(){
				$('#comments').css('float', 'none');
				$('#required').css('float', 'none');
				$.fancybox.resize();
			}
		});


		$(".fancybox-terms").fancybox({
			'hideOnContentClick': false,
			'width': 1024,
			'height': 600,
			'titlePosition': titleposition
		});

		$(".fancybox-more-numbers").fancybox({
			'hideOnContentClick': false,
			'width': 250,
			'height': 400,
			'titlePosition': titleposition
		});


		$(".fancybox-info").fancybox({
			'hideOnContentClick': false,
			'titlePosition': titleposition,
			'onComplete': function(){
				$.fancybox.resize();
			}
		});

		$(".fancybox-refund").fancybox({
			'hideOnContentClick': false,
			'titlePosition': titleposition,
			'width': 550,
			'height': 300,
			'onClosed': function() {
				location.reload(true);
			}
		});

		$("#loadinglink").fancybox({
			'autoDimensions'	: true,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'modal'				: true,
			'padding'			: 10,
			'centerOnScroll'	: true
		});

		$(".fancybox-newsletter").fancybox({
			'scrolling': 'no',
			//'autoDimensions': true,
			'autoScale': false,
			'titlePosition': titleposition,
			'onClosed': function() {
				$("#newsletterform").show();
				$(".error-msg").hide();
				$(".msg").hide();
			},
			'onComplete': function(){
				$('#newsletteremail').focus();
			}
		});

		if($.client.os == "Windows") $('#login_popup').css('width', '30em');
		if($.browser.msie && $.browser.version.substring(0,1) == '6') $('#login_popup').css('width', '31em');
		$(".login_popup").fancybox({
			'scrolling': 'no',
			//'autoDimensions': true,
			'autoScale': false,
			'titlePosition': titleposition,
			'onClosed': function() {
				$("#login_popup_form .error-msg").hide();
			},
			'onComplete': function(){
				$('#formForgotPass').hide();
				$.fancybox.resize();
				$('#login_email').focus();
			}

		});

		$("#login_popup_form").bind("submit", function() {

			if ($("#login_email").val().length < 1 || $("#login_password").val().length < 1) {
				$("#empty").show();
				$.fancybox.resize();
				return false;
			}

			$.fancybox.showActivity();

			$.ajax({
				type	: "POST",
				cache	: false,
				data	: $(this).serializeArray(),
				success: function(data) {
					var result = unserialize(data);
					if(result.result){
						//connexion ok
						if(result.url == '' || result.url == null){
							$('#login_link').hide();
							$('#account_link').css('display', 'inline');
							$('#logout_link').css('display', 'inline');

							$('#login_form').hide();
							$('#login_ok').show();
							$('#loginlinkspan').hide();
							$.fancybox.resize();

							if(result.member){
								setOptionSelected('title', result.member.title);
								$('#familyname').val(result.member.familyname);
								$('#firstname').val(result.member.firstname);
								$('#email').val(result.member.email);
								$('#emailconfirm').val(result.member.email);
								$('#phone').val(result.member.phone);
								$('#mobile').val(result.member.mobile);
								$('#fax').val(result.member.fax);
								$('#address').val(result.member.address);
								$('#zipcode').val(result.member.zipcode);
								$('#city').val(result.member.city);
								setOptionSelected('country', result.member.country);

								$('#connection').hide();
							}
							$.fancybox.hideActivity();
						}
						//connexion ok mais redirection
						else{
							window.location = result.url;
						}

					}
					else {
						$(".error-msg").hide();
						$('#'+result.error).show();
						$.fancybox.resize();
						$.fancybox.hideActivity();
					}
				}
			});

			return false;
		});

		$("#formForgotPass").bind("submit", function() {

			if ($("#txtemail").val().length < 1) {
				$("#empty").show();
				$.fancybox.resize();
				return false;
			}

			$.fancybox.showActivity();

			$.ajax({
				type	: "POST",
				cache	: false,
				data	: $(this).serializeArray(),
				success: function(data) {
					$(".error-msg").hide();
					if(document.getElementById(data)) $('#'+data).show();
					else{
						$.get(data, function(result){
							if(result == 'ok') $('#sended').show();
							else $('#senderror').show();
							$.fancybox.resize();
						});
					}
					$.fancybox.resize();
					$.fancybox.hideActivity();
				}
			});

			return false;
		});


		$("#contact_passwd_link").fancybox({
			'scrolling': 'no',
			//'autoDimensions': true,
			'autoScale': false,
			'titlePosition': titleposition,
			'onStart': function(){
				$("#contactpasswdtxtemail").focus();
			},
			'onClosed': function() {
				$(".error-msg").hide();
			},
			'onComplete': function(){
				$.fancybox.resize();
			}

		});

		$("#login_contact_form").bind("submit", function() {

			if ($("#contact_login_email").val().length < 1 || $("#contact_login_password").val().length < 1) {
				$("#contactempty").show();
				return false;
			}

			//$('#login-submit-loading').show();
			$.ajax({
				type	: "POST",
				cache	: false,
				data	: $(this).serializeArray(),
				beforeSend: function(){ $('#login-submit-loading').show(); },
				success: function(data) {
					//alert(data);
					var result = unserialize(data);
					if(result.result){
						//connexion ok
						if(result.url == '' || result.url == null){
							$('#login_link').hide();
							$('#account_link').css('display', 'inline');
							$('#logout_link').css('display', 'inline');

							$('#connection').hide();
							$('#booking_form').show();

							if(result.member){
								setOptionSelected('title', result.member.title);
								$('#familyname').val(result.member.familyname);
								$('#firstname').val(result.member.firstname);
								$('#email').val(result.member.email);
								$('#emailconfirm').val(result.member.email);
								$('#phone').val(result.member.phone);
								$('#mobile').val(result.member.mobile);
								$('#fax').val(result.member.fax);
								$('#address').val(result.member.address);
								$('#zipcode').val(result.member.zipcode);
								$('#city').val(result.member.city);
								if(result.member.refererid){
									$('#referercodediv').hide();
									$('#referercode').val('');
								}
								setOptionSelected('country', result.member.country);
							}
						}
						//connexion ok mais redirection
						else{
							window.location = result.url;
						}

					}
					else {
						$(".error-msg").hide();
						$('#contact'+result.error).show();
					}

					$('#login-submit-loading').hide();
				}
			});

			return false;

		});

		$("#contactFormForgotPass").bind("submit", function() {

			if ($("#contactpasswdtxtemail").val().length < 1) {
				$("#contactpasswdpassempty").show();
				$.fancybox.resize();
				return false;
			}
			$.fancybox.showActivity();

			$.ajax({
				type	: "POST",
				cache	: false,
				data	: $(this).serializeArray(),
				success: function(data) {

					$(".error-msg").hide();
					if(document.getElementById(data)) $('#contactpasswd'+data).show();
					else{
						$.get(data, function(result){
							if(result == 'ok') $('#contactpasswdsended').show();
							else $('#contactpasswdsenderror').show();
							$.fancybox.resize();
						});
					}

					$.fancybox.resize();
					$.fancybox.hideActivity();
				}
			});

			return false;

		});

		$("#newsletterform").bind("submit", function() {
			if ($("#newsletteremail").val().length < 1) {

				$("#newsletterempty").show();
				$.fancybox.resize();
				return false;
			}

			$.fancybox.showActivity();

			$.ajax({
				type	: "POST",
				cache	: false,
				data	: $(this).serializeArray(),
				success: function(data) {

					$(".error-msg").hide();

					var result = unserialize(data);
					if(result.result){
						$("#newsletterform").hide();
						$("#"+result.msg).show();
					}
					else{
						$('#'+result.error).show();
					}

					$.fancybox.resize();
					$.fancybox.hideActivity();
				}
			});

			return false;
		});

		$("#sendtofriendform").bind("submit", function() {
			if ($("#username").val().length < 1 || $("#senderemail").val().length < 1 || $("#friendemail").val().length < 1) {

				if($("#username").val().length < 1) $("#stfemptyusername").show();
				if($("#senderemail").val().length < 1 || $("#friendemail").val().length < 1) $("#stfempty").show();

				$.fancybox.resize();
				return false;
			}

			$.fancybox.showActivity();

			$.ajax({
				type	: "POST",
				cache	: false,
				data	: $(this).serializeArray(),
				success: function(data) {

					$(".error-msg").hide();

					var result = unserialize(data);
					if(result.result){
						$("#sendtofriendform").hide();
						$("#"+result.msg).show();
					}
					else{
						alert(result.error);
						$.each(result.error, function(key, value){
							$('#'+value).show();
						})

					}

					$.fancybox.resize();
					$.fancybox.hideActivity();
				}
			});

			return false;
		});

		// Cancel a reservation.
		$(".fancybox-cancel").fancybox({
			'scrolling'			: 'no',
			//'autoScale'		: true,
			//'autoDimensions': true,
			'titlePosition'	: titleposition,
			'onClosed'			: function() { },
			'onComplete'		: function() {

				$('#cancelbookingformbutton').attr('disabled', '');
				$('#cancelbooking_id').html($('#cancelbooking_currentid').val());
				$('#cancelbooking_date').html($('#cancelbooking_currentdate').val());
				$('#cancelbooking_amount').html($('#cancelbooking_currentamount').val());
				$('#alreadycancelled').attr('checked', '');
			}
		});

		$("#cancelbookingform").bind("submit", function() {

			$.fancybox.showActivity();
			$('#cancelbookingformbutton').attr('disabled', 'disabled');

			$.ajax({
				type		: "POST",
				cache		: false,
				url		: $('#proxycancelbooking').val(),
				data		: $(this).serializeArray(),
				success	: function(data) {

					$.fancybox.hideActivity();
					var sdata = data.split('**');
					var sdata2 = sdata[1].split('##');

					if(sdata[0] == 'ok') {

						var txtresult = '<strong>Booking cancelled</strong> <br /><br >';
							txtresult = txtresult+'cancel amount: '+sdata2[0]+' '+sdata2[1]+'<br />cancel provider ref:'+sdata2[2];
					}
					else {

						var txtresult = '<strong>Unable to cancel this booking</strong> <br /><br >';
							txtresult = txtresult+'<span style="color: red">'+sdata2[0]+'</span>';
					}

					$.fancybox('<div style="height: 150px; width: 700px;">'+txtresult+'</div>');

				},
				error	: function(data) { }
			});

			return false;
		});

		$(".fancybox-check_all_popup_alert").fancybox({
			'hideOnContentClick': false,
			'width': 600,
			'height': 250,
			'titlePosition': titleposition
		});

	});

