//-------------------------------------------------------------------------------------------
// Unchecks all apartment checkboxes
function clearOfferInfo() {
	$("#offer-info").children().remove();
}
//-------------------------------------------------------------------------------------------


$(function() {

	//-------------------------------------------------------------------------------------------	
	// Create bgStretcher images slideshow
	//
	$("#intro").bgStretcher({
		images: ['images/bg_intro_01.jpg', 'images/bg_intro_02.jpg', 'images/bg_intro_03.jpg', 'images/bg_intro_04.jpg', 'images/bg_intro_05.jpg', 'images/bg_intro_06.jpg', 'images/bg_intro_07.jpg'], 
		imageWidth: 1400, 
		imageHeight: 900, 
		nextSlideDelay: 10000
	});
	//-------------------------------------------------------------------------------------------

	
	// Set maximum height of the availability form in px
	var formMaxHeight2 = 800;
	
	var offerInfo = $("#offer-info");
	var offerDescription = $("#offer_description");
	
	var offerDescription1 = $("#offer-01 .description");
	var offerDescription2 = $("#offer-02 .description");
	var offerDescription3 = $("#offer-03 .description");
	
	$("#btn-offer-01").click( function() {
		clearOfferInfo();
		offerInfo.append(offerDescription1.html());
		offerDescription.val(offerDescription1.text());
	});
	
	$("#btn-offer-02").click( function() {
		clearOfferInfo();
		offerInfo.append(offerDescription2.html());
		offerDescription.val(offerDescription2.text());
	});
	
	$("#btn-offer-03").click( function() {
		clearOfferInfo();
		offerInfo.append(offerDescription3.html());
		offerDescription.val(offerDescription3.text());
	});
	
		
	clearOfferInfo();
	
	//-------------------------------------------------------------------------------------------
	// Create pop-up offers form
	//
	$("a.send-inquiry").fancybox({
		'scrolling'		: 'auto',
		'titleShow'		: false,
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	150, 
		'speedOut'		:	150,
		'autoDimensions': false,
		'overlayColor': '#000',
		'overlayOpacity': 0.65,
		'width'			: 630,
		'height'			: (($(window).height()-120) > formMaxHeight2) ? formMaxHeight2 : $(window).height()-120,
		'onStart' 		: function() {
			$("#offer-wrapper").show();
		},
		'onClosed'		: function() {
			//clearCheckboxes();
		}
	});
	
	$("#offer-form").validate({
	
		rules: {
			offer_adults: {
				required: true,
				digits: true
			},
			offer_children: {
				digits: true
			},
			offer_contact_name: {
				required: true,
				minlength: 2
			},
			offer_contact_email: {
				required: true,
				email: true,
				remote: "check_domain_offer.php"
			},
			offer_contact_answer: {
				required: true,
				remote: "check_answer_offer.php"
			}
		},
		
		messages: {
			offer_adults: {
				required: "Required.",
				digits: "Accepts only numbers."
			},
			offer_children: {
				digits: "Accepts only numbers."
			},
			offer_contact_name: {
				required: "Please enter your name.",
				minlength: "Must be longer than 2 characters."
			},
			offer_contact_email: {
				required: "Email address is required.",
				email: "Please enter a valid email address.",
				remote: "Please use an existing email address."
			},
			offer_contact_answer: {
				required: "Required.",
				remote: "I don't think so..."
			}
		}
	});
	
		
	$("#offer-form").submit( function() {
		
		//console.log("Submitting.");
		
		if ( $("#offer-form").validate().form() ) {
			//console.log("Passed validation.");
				
			$.fancybox.showActivity();
			
			$.ajax({
				type		: "POST",
				cache		: false,
				url		: "_offer_form.php",
				data		: $("#offer-form").serialize(),
				success: function(msg) {
					$.fancybox(msg);
				},
				error: function(msg) {
					$.fancybox(msg);
				}
			});
			return false;
		} else {
			//console.log("Validation failed.");
			$.fancybox.resize();
			return false;
		}
		return false;
		//console.log("Submitted when it shouldn't have been.");
	});
	//-------------------------------------------------------------------------------------------
	
});



