$(document).ready(
	function() {
		$(window).load(
			function() {
				
				$("#outter_container").css("visibility", "visible");
				
				$("#mask-image-title span").text($("#big_image_gallery").attr("title"));
			}
		);
		
		$("#slideshow").cycle({
			fx: "fade"
		});
		
		$("#gallery").smoothDivScroll({autoScroll: "", autoScrollDirection: "endlessloop", pauseAutoScroll: "mouseover", visibleHotSpots: "always"});
		$("#side-gallery").smoothDivScroll({autoScroll: "", autoScrollDirection: "endlessloop", pauseAutoScroll: "mouseover", visibleHotSpots: "always"});

		$(".box1").children(".textBrowser").hide();
		$(".box1").each(
			function() {
				$(this).children(".textBrowser:first").show();
			}
		);
		
		if (window.name != "show") {
			$('.pagenav ul').hide();
		}
		
		$(".button_btnback").attr("src", templateDir.concat("/images/seta_esq_down.png"));
		$(".button_btnfront").attr("src", templateDir.concat("/images/seta_dir.png"));
		
		$(".textBrowser_front").mousedown(
			function() {
				$(this).attr("src", templateDir.concat("/images/seta_dir_down.png"));
			}
		);
		
		$(".textBrowser_front").mouseup(
			function() {
				$(this).attr("src", templateDir.concat("/images/seta_dir.png"));
				
				var index = $(this).parent().children(".textBrowser:visible").index() - $(this).parent().children(".textBrowser:first").index();
	
				var size = $(this).parent().children(".textBrowser").size();
				
				$(this).parent().children(".textBrowser_back").attr("src", templateDir.concat("/images/seta_esq.png"));
				
				if (index == size - 1) {
					$(this).attr("src", templateDir.concat("/images/seta_dir_down.png"));
					return;
				}
				
				if (index == size - 2) {
					$(this).attr("src", templateDir.concat("/images/seta_dir_down.png"));
				}
				
				$(this).parent().children(".textBrowser:visible").fadeOut('fast',
					function() {
						$(this).next().fadeIn('fast');
					}
				);
				
				//$(this).parent().children(".p_paragraph2:visible:first").hide();
			}
		);
		
		$(".textBrowser_back").mousedown(
			function() {
				$(this).attr("src", templateDir.concat("/images/seta_esq_down.png"));
			}
		);
		
		$(".textBrowser_back").mouseup(
			function() {
				$(this).attr("src", templateDir.concat("/images/seta_esq.png"));
				
				var index = $(this).parent().children(".textBrowser:visible").index() - $(this).parent().children(".textBrowser:first").index();
				
				var size = $(this).parent().children(".textBrowser").size();
				
				$(this).parent().children(".textBrowser_front").attr("src", templateDir.concat("/images/seta_dir.png"));
				
				if (index == 0) {
					$(this).attr("src", templateDir.concat("/images/seta_esq_down.png"));
					return;
				}
				
				if (index == 1) {
					$(this).attr("src", templateDir.concat("/images/seta_esq_down.png"));
				}
				
				$(this).parent().children(".textBrowser:visible").fadeOut('fast',
					function() {
						$(this).prev().fadeIn('fast');
					}
				);
				
				//$(this).parent().children(".p_paragraph2:visible").prev().show();
				
				//$(this).parent().children(".p_paragraph2:visible:last").hide();
			}
		);
		
		$(".thumbnail_gallery").click(
			function() {
				$("#big_image_gallery").css("display", "none");
				$(".content_loading").css("display", "block");
				var img_path = new String($(this).attr("src"));
				var index = img_path.indexOf("_");
				var img_path_sliced = img_path.slice(0, index);
				var img_path_new = img_path_sliced.concat(".jpg");
				$("#big_image_gallery").attr("src", img_path_new);
				$("#big_image_gallery").attr("title", $(this).attr("title"));
			}
		);

		$("#anchor-gallery").hover (
			function() {
				$(this).css("color", "#FFFFFF");
				
				//$("#mask-image-title").css("visibility", "hidden");
				//$("#mask-anchor-gallery").css("visibility", "hidden");
				$("#mask-gallery").css("visibility", "visible");
				$("#gallery").css("visibility", "visible");
			},
			function() {
			}
		);
		
		$("#anchor-gallery").click (
			function() {
				$("#mask-image-title").css("visibility", "hidden");
				$("#mask-anchor-gallery").css("visibility", "hidden");
				$("#mask-gallery").css("visibility", "visible");
				$("#gallery").css("visibility", "visible");
			}
		);
		
		$("#gallery").mouseleave (
			function() {
				$("#anchor-gallery").css("color", "#999999");
				$("#mask-gallery").css("visibility", "hidden");
				$("#gallery").css("visibility", "hidden");
				$("#mask-image-title").css("visibility", "visible");
				$("#mask-anchor-gallery").css("visibility", "visible");
			}
		);
		
		$("#big_image_gallery").load(
			function() {
				$(".content_loading").css("display", "none");
				$("#mask-image-title span").text($(this).attr("title"));
				$(this).fadeIn(500);
			}
		);

		$('.separator').click(
			function() {
				window.name = "";
				var checkElement = $(this).next();
				
				if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
					checkElement.slideUp('normal');
					return false;
				}
				
				if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
					$('.separator ul:visible').slideUp('normal');
					checkElement.slideDown('normal');
					return false;
				}
			}
		);
		
		$(".pagenav ul li").click(
			function() {
				window.name = "show";
			}
		);
		
		$('#form_question').ajaxForm(
			{
				beforeSubmit: validateQuestionForm,
				success: showSuccessQuestion
			}
		);
		
		function showSuccessQuestion(responseText, statusText, xhr, $form) {
			if(lang == "pt" || lang == "") {
				alert("Obrigado pelo seu contacto. Ir\u00E1 receber uma resposta em breve.");
			}
			
			if(lang == "en") {
				alert("Thank you for your contact. We will get back to you as soon as possible.");
			}
			
			if(lang == "es") {
				alert("Gracias por su contacto. Recibir\u00E1 una respuesta brevemente.");
			}
		}
		
		function validateQuestionForm(formData, jqForm, options) {
			if($("#form_question #question_name").attr("value") != "" && $("#form_question #question_email").attr("value") != "" && $("#form_question #question_subject").attr("value") != "" && $("#form_question #question_message").attr("value") != "") {
				return true;
			}
			else {
				if(lang == "pt" || lang == "") {
					alert("Existem campos obrigat\u00F3rios por preencher. Por favor corriga.");
				}
				
				if(lang == "en") {
					alert("Please fill in all mandatory fields.");
				}
				
				if(lang == "es") {
					alert("Llene todos los campos obligat\u00F3rios, por favor.");
				}
				
				return false;
			}
		}
		
		/*$('#form_newsletter').ajaxForm(
			{
				beforeSubmit: validateNewsletterForm,
				success: showSuccessNewsletter
			}
		);
		
		function showSuccessNewsletter(responseText, statusText, xhr, $form) {
			if(lang == "pt" || lang == "") {
				alert("Ir\u00E1 receber dentro de momentos o pedido de confirma\u00E7\u00E3o para receber a nossa newsletter. Por favor verifique a sua caixa de email.");
			}
			
			if(lang == "en") {
				alert("In a few moments you will receive a subscription request for our newsletter. Please check you email.");
			}
			
			if(lang == "es") {
				alert("Recibir\u00E1 dentro de momentos el pedido de confirmaci\U00F3n para nuestra newsletter. Verifique su email.");
			}
		}
		
		function validateNewsletterForm(formData, jqForm, options) {
			apos = $("#email_newsletter").attr("value").indexOf("@");
			dotpos = $("#email_newsletter").attr("value").lastIndexOf(".");
			
			if (apos < 1 || dotpos-apos < 2) {
				if(lang == "pt" || lang == "") {
					alert("Endere\u00E7o de email inv\u00E1lido!");
				}
				
				if(lang == "en") {
					alert("Invalid email address!");
				}
				
				if(lang == "es") {
					alert("Enderezo de email inv\u00E1lido!");
				}
				
				return false;
			}
			else {
				return true;
			}
		}*/

		$("a#inline").fancybox({'hideOnContentClick': false, 'overlayColor': '#000000'});
		
		$("#booking_link").fancybox({"autoDimensions": false, "height": 800, "hideOnContentClick": false, "overlayColor": "#000000", "width": 800, "type" : "iframe", "hideOnOverlayClick": false, "centerOnScroll": true});
		
		$(".tarifas").fancybox({"autoDimensions": false, "height": 800, "hideOnContentClick": false, "overlayColor": "#000000", "width": 950, "type" : "iframe", "hideOnOverlayClick": false, "centerOnScroll": true}); 
		
		$(".booking").bind("submit", function() {
			$("#booking_link").attr("href", $(this).attr("action"));
			$("#booking_link").click();
			
		});
		
		/*$(".tarifas").bind("click", function() {
			$("#booking_link").attr("href", $(this).attr("href"));
			$("#booking_link").trigger("click");
		});*/
	}
);

