//-------------------------------------------------------------------------------------------
// Unchecks all apartment checkboxes
function clearCheckboxes() {
	$("#apartments-wrapper input:checkbox").attr("checked", false);
}
//-------------------------------------------------------------------------------------------

$(function() {

	clearOfferInfo();
	
	// Set maximum height of the availability form in px
	var formMaxHeight = 870;
	
	//-------------------------------------------------------------------------------------------	
	// Get apartment checkboxes
	var app1Checkbox = $("#app1");
	var app2Checkbox = $("#app2");
	var app3Checkbox = $("#app3");
	var app4Checkbox = $("#app4");
	var app5Checkbox = $("#app5");
	
	// Get number of adults
	var adults = $("#adults");
	
	// Get number of children
	var children = $("#children");
	
	// Get contact name
	var contactName = $("#contact_name");
	
	//Get contact email
	var contactEmail = $("#contact_email");

	
	//-------------------------------------------------------------------------------------------
	// Define availability buttons click actions here
	//
	$("#btn-availability-01").click( function() {
		clearCheckboxes();
		app1Checkbox.attr("checked", true);
	});	
	
	$("#btn-availability-02").click( function() {
		clearCheckboxes();
		app2Checkbox.attr("checked", true);
	});	
	
	$("#btn-availability-03").click( function() {
		clearCheckboxes();
		app3Checkbox.attr("checked", true);
	});	
	
	$("#btn-availability-04").click( function() {
		clearCheckboxes();
		app4Checkbox.attr("checked", true);
	});	
	
	$("#btn-availability-05").click( function() {
		clearCheckboxes();
		app5Checkbox.attr("checked", true);
	});
	//-------------------------------------------------------------------------------------------


	//-------------------------------------------------------------------------------------------
	// Create pop-up image galleries for the apartments
	//
	$("a.app").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	150, 
		'speedOut'		:	150,
		'overlayColor': '#000',
		'overlayOpacity': 0.65,
		'titlePosition': 'inside',
		'overlayColor': '#000'
	});
	
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
		return (title && title.length ?  title : '' ) + '</div>';
	}
	//-------------------------------------------------------------------------------------------


	//-------------------------------------------------------------------------------------------
	// Create pop-up availability form
	//
	$("a.check-availability").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) > formMaxHeight) ? formMaxHeight : $(window).height()-120,
		'onStart' 		: function() {
			$("#availability").show();
			$("#availability-error").hide();
		},
		'onClosed'		: function() {
			clearCheckboxes();
			$("#availability-error").hide();
		}
	});
	
		
	$("#availability-form").validate({
	
		rules: {
			adults: {
				required: true,
				digits: true
			},
			children: {
				digits: true
			},
			contact_name: {
				required: true,
				minlength: 2
			},
			contact_email: {
				required: true,
				email: true,
				remote: "check_domain.php"
			},
			contact_answer: {
				required: true,
				remote: "check_answer.php"
			}
		},
		
		messages: {
			adults: {
				required: "Required.",
				digits: "Accepts only numbers."
			},
			children: {
				digits: "Accepts only numbers."
			},
			contact_name: {
				required: "Please enter your name.",
				minlength: "Must be longer than 2 characters."
			},
			contact_email: {
				required: "Email address is required.",
				email: "Please enter a valid email address.",
				remote: "Please use an existing email address."
			},
			contact_answer: {
				required: "Required.",
				remote: "I don't think so..."
			}
		}
	});
	
		
	$("#availability-form").submit( function() {
		
		if ( $("#availability-form").validate().form() ) {
				
			$.fancybox.showActivity();
			
			$.ajax({
				type		: "POST",
				cache		: false,
				url		: "_availability_form.php",
				data		: $("#availability-form").serialize(),
				success: function(msg) {
					$.fancybox(msg);
				},
				error: function(msg) {
					$.fancybox(msg);
				}
			});
			return false;
		} else {
			$.fancybox.resize();
			return false;
		}
	
		return false;
	});
	//-------------------------------------------------------------------------------------------


	//-------------------------------------------------------------------------------------------
	// Date Picker Controls 
	//
	var dates = $("#date_from, #date_till").datepicker({
		'dateFormat': 'dd.mm.yy',
		'showAnim'	: 'blind',
		'minDate'	: '0',
		'onSelect'	: function( selectedDate ) {
				var option = this.id == "date_from" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}
	})
	//-------------------------------------------------------------------------------------------
	
});


