$(document).ready(function() {
	// Hide body on load for fade in
	//$("body").css('opacity', 0);
	// HTML5 placeholder support
	$('input, textarea').placeholder();
	// Enable Tiny Carousel
	$('.carousel').each(function() {
		var carousel = $(this);
		carousel.tinycarousel({ display: 1, duration: 300 });
	});
	// Enable Slider
	$(".slider").prepend('<a href="#" class="buttons prev">Prev</a><a href="#" class="buttons next">Next</a>');
	$(".slider br").remove();
    $(".slider > div")
    .cycle({
	    fx:     'scrollLeft', 
	    speed:   600,
	    timeout:  6000,
	    prev:   '.slider .prev', 
    	next:   '.slider .next'
	});
	// Fix YouTube embeds
	$('iframe[src*="youtube"]').each(function() {
		var url = $(this).attr("src");
		$(this).attr("src",url+"?wmode=transparent");
	});
	
	// Booking form, page 2 group members
	function sh_bgrpmem_form(){
		var group_total = 0;
		var group_adults = $('li.size div.ginput_container select').first().val();
		var group_childs = $('li.size div.ginput_container select').last().val();
		group_total = parseInt(group_adults) + parseInt(group_childs);
	
		var counter = 0;
		$('fieldset.member').each(function(){
			if (counter < group_total) {
				$(this).show();
			} else {
				$(this).hide();
			}
			counter++;
		});
	}
	var clicked = 0; 
	function sh_bgrpmem_validate(){
		var testType = "";
		var targetLi = "";
		var errors = 0;
		var parentElement = "li";
		var errorClass = "gfield_error";
		var testType = "";
		var errorMessage = '<div class="gfield_description validation_message js-added">(required)</div>';
		var formError = '<strong class="message error js-added">Please complete the required fields to continue</strong>';
		if(clicked==1) {
			$('.gfield_contains_required input, .gfield_contains_required textarea, .room input, .name input, .age input').filter(':visible').each(function(){
				// SET CONTAINING ELEMENT FOR CLASS
				targetLi = $(this).parent().parent();
				// SET REGEX FOR FIELD TYPES
				var fieldType = $(this).attr("type");
				if (fieldType == "email") {
					testType = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/;
				} else {
					testType = /\w+/;
				}
				// TEST CURRENT FIELD
				if (testType.test($(this).val())) {
					targetLi.removeClass(errorClass);
					targetLi.find('.validation_message').remove();
					$('strong.error').remove();
				} else {
					errors = 1;
					targetLi.addClass(errorClass);
					targetLi.find('.validation_message').remove();
					targetLi.append(errorMessage);
				}
			});
		}
		// CHECK FOR ERRORS
		if (errors == 1 && clicked==1) {
			$('strong.error').remove();
			//$('.gf_page_steps, .gform_heading').after(formError);
			$('.gform_footer, .gform_page_footer').after(formError);
    		clicked = 0;
			return false;
		} else {
    		clicked = 0;
			return (true);
		}
	}
	// Select Boxes
	$('li.size div.ginput_container select').change(sh_bgrpmem_form);
	sh_bgrpmem_form();
	// Validation
	
	// Check if it's the next or submit button clicked
	$(".gform_next_button").live('mouseup keyup',function(){
    	clicked = 1;
	});
	$("#gform_1 .gform_button").live('mouseup keyup',function(){
    	clicked = 1;
	});
	$("#gform_submit_button_2").click(function(){
    	$(this).css('visibility','hidden');
	});
	$(".gform").submit(function() {
		result = sh_bgrpmem_validate();
		return result;
	});
});

$(window).bind("load", function() {
	// Fade in on load to combat FOUT
  	//$("body").fadeIn();

});
