
function CheckAvailable() {
	jQuery("#startdatehid").val("");
	jQuery("#todatehid").val("");
	jQuery("#holiday_price").html("");
	$("#available-content").html("<div class='avail-loader'>Please wait, loading availability...</div>");
	$.ajax({
		url: "/includes/custom/availabilitycheck.php",
		type: "GET",
		cache: false,
		async: false,
		data: { day: jQuery("#daychange").val(), month: jQuery("#monthchange").val(), holidaylength: jQuery("#hollength").val(), propref: jQuery("#propref").val() },
		dataType: "text",
		success: function(msg){
			$("#available-content").html(msg);
		}
	});
}

function getPrice(startDate, endDate, propRef, people, brandcode, numberofpets) { 
	$.get("/includes/getPrice.php", {startDate: startDate, endDate: endDate, propRef: propRef, people: people, brandcode: brandcode, numpets: numberofpets, response: 'echo'}, function(priceData) {
		if(priceData == "Please Call" || priceData == "Please select a changeover day") {
			$("#holiday_price").html(priceData);
		}
		else {
			$("#holiday_price").html("&pound;" + priceData);
		}
	});
}

function getAdults() {
	if($("#adults").val() != "") {
		return $("#adults").val();
	}
	else {
		return 0;
	}
}

function getChildren() {
	if($("#children").val() != "") {
		return $("#children").val();
	}
	else {
		return 0;
	}
}

function getInfants() {
	if($("#infants").val() != "") {
		return $("#infants").val();
	}
	else {
		return 0;
	}
}

function checkParty() {
	var adults = $("#adults").val();
	var children = $("#children").val();
	var maxpeople = $("#maxpeople").val();
	var people = 0;	
	try {	
		people = parseInt(adults) + parseInt(children);		
		if(adults == 0) {
			return 3;
		}
		
		if(people > 0) {
			if(maxpeople >= people) {
				return 1;
			}
			else {
				return 0;
			}
		}
		else {
			return 2;
		}
	}
	catch(e) {
		return 0;
	}
}

function CheckAvailFormProperty() {
		
	if($("#startdatehid").val() == "") {
		alert("Please click on the calendar to select your holiday dates.");
		return false;
	}
	
	try {
		var propref = $("#property").val();
		var sbCheck = "";
		bodyContent = $.ajax({
				url: "/includes/custom/shortBreakCheck.php",
				type: "GET",
				cache: false,
				async: false,
				data: {startdate: $("#startdatehid").val(), todate: $("#todatehid").val(), propref: propref},
				dataType: "text",
				success: function(msg){
					sbCheck = msg;
				}
			}).responseText;
			
		if(sbCheck != "") {
			alert(sbCheck);
			return false;
		}
			
	}
	catch(e) {
		alert(e + " An error occurred taking your booking, please call us and we will be happy to help.");
		return false;
	}
	
	if($("#holiday_price").html() == "Please Call" || $("#holiday_price").html() == "Please select a changeover day" || $("#holiday_price").html() == "" || $("#holiday_price").html() == "Unavailable") {
		alert("Sorry, the property is not available for those dates.");
		return false;
	}
	
	var people = checkParty();
	
	if(people == 0) {
		alert("The number in your party exceeds the amount allowed on the property.");
		return false;
	}
	else if(people == 2) {
		alert("Please select how many people are coming to the property.");
		return false;
	}
	else if(people == 3) {
		alert("Please select how many adults are coming to the property.");
		return false;
	}
	
	if(jQuery("#booking-login").is(':hidden')) {
		jQuery("#booking-login").css({ display: 'block' });
		jQuery("#availformcontainer").animate({ top: '-260px' }, 200, "linear");
		return false;
	}
}

jQuery(document).ready(function() {
	jQuery("#monthchange,#daychange,#hollength").change(CheckAvailable);

	if(jQuery('#property_carousel li').size() > 1) {
		jQuery('#property_carousel li:first').before(jQuery('#property_carousel li:last'));
		jQuery('#right_scroll').click(function(){
			var item_width = jQuery('#property_carousel li').outerWidth();
			var left_indent = parseInt(jQuery('#property_carousel').css('left')) - item_width;
			jQuery('#property_carousel:not(:animated)').animate({'left' : left_indent},500,function(){
				jQuery('#property_carousel li:last').after($('#property_carousel li:first'));
				jQuery('#property_carousel').css({'left' : '-' + item_width + 'px'});
			});
			return false;
		});

		jQuery('#left_scroll').click(function(){
			var item_width = jQuery('#property_carousel li').outerWidth();
			var left_indent = parseInt(jQuery('#property_carousel').css('left')) + item_width;
			jQuery('#property_carousel:not(:animated)').animate({'left' : left_indent},500,function(){
				jQuery('#property_carousel li:first').before($('#property_carousel li:last'));
				jQuery('#property_carousel').css({'left' : '-' + item_width + 'px'});
			});
			return false;
		});
	}
	else {
		jQuery('#property_carousel').css({'left' : '0px'});
	}
	
	/* if(jQuery('#property-photo-list li').size() > 3) {
		//jQuery('#property-photo-list li:first').before(jQuery('#property-photo-list li:last'));
		jQuery('a#photos_right_scroll').click(function(){
			var item_width = jQuery('#property-photo-list li').outerWidth();
			var left_indent = parseInt(jQuery('#property-photo-list').css('left')) - item_width;
			jQuery('#property-photo-list:not(:animated)').animate({'left' : left_indent},500,function(){
				jQuery('#property-photo-list li:last').after($('#property-photo-list li:first'));
				jQuery('#property-photo-list').css({'left' : '-280px'});
			});
			return false;
		});
		
		jQuery('a#photos_left_scroll').click(function(){
			var item_width = jQuery('#property-photo-list li').outerWidth();
			var left_indent = parseInt(jQuery('#property-photo-list').css('left')) + item_width;
			jQuery('#property-photo-list:not(:animated)').animate({'left' : left_indent},500,function(){
				jQuery('#property-photo-list li:first').before($('#property-photo-list li:last'));
				jQuery('#property-photo-list').css({'left' : '-280px'});
			});
			return false;
		});
	}
	else {
		jQuery('#property-photo-list').css({'left' : '0px'});
		jQuery('a#photos_left_scroll,a#photos_right_scroll').hide();
	} */
	
	if(jQuery('#property-photo-list li').size() > 3) {
		jQuery('#property-photo-list li:first').before(jQuery('#property-photo-list li:last'));

		jQuery('a#photos_right_scroll').click(function(){
			if(jQuery('#property-photo-list li').size() == 4) {
				jQuery('#property-photo-list li:first').clone().appendTo('#property-photo-list');
			}
			var item_width = jQuery('#property-photo-list li').outerWidth();
			var left_indent = parseInt(jQuery('#property-photo-list').css('left')) - item_width;
			jQuery('#property-photo-list:not(:animated)').animate({'left' : left_indent},500,function(){
				jQuery('#property-photo-list li:last').after($('#property-photo-list li:first'));
				jQuery('#property-photo-list').css({'left' : '-280px'});
				jQuery('#property-photo-list li:last').remove();
			});
			return false;
		});
		
		jQuery('a#photos_left_scroll').click(function(){
			var item_width = jQuery('#property-photo-list li').outerWidth();
			var left_indent = parseInt(jQuery('#property-photo-list').css('left')) + item_width;
			jQuery('#property-photo-list:not(:animated)').animate({'left' : left_indent},500,function(){
				jQuery('#property-photo-list li:first').before($('#property-photo-list li:last'));
				jQuery('#property-photo-list').css({'left' : '-280px'});
			});
			return false;
		});
	}
	else {
		jQuery('#property-photo-list').css({'left' : '0px'});
		jQuery('a#photos_left_scroll,a#photos_right_scroll').hide();
	}
	
	
	jQuery('div.prop-avail-selection:not(div.notavailable)').live("click", function() {
		jQuery("#booking-login").css({ display: 'none' });
		jQuery("#availformcontainer").css({ top: '0px' });
		var id = jQuery(this).attr("id");
		var startdate = jQuery("#startdate" + id).val();
		var todate = jQuery("#todate" + id).val();
		var hollength = jQuery("#hollength" + id).val();
		jQuery("#startdatehid").val(startdate);
		jQuery("#todatehid").val(todate);
		getPrice(startdate, todate, jQuery("#propref").val(), 100, jQuery("#brandcode").val(), jQuery("#numpets").val());
		
		$("#booking-details-container").dialog({
			resizable: false,
			height:310,
			width:500,
			modal: true,
			dialogClass: "booking-modal"
		});
	});
	
	jQuery("#adults,#children,#infants,#numpets").change(function() {
		adults = getAdults();
		children = getChildren();
		infants = getInfants();
		var numpeople = parseInt(adults) + parseInt(children);
		numpeople = ((numpeople == 0) ? 100 : numpeople);
		var startdate = jQuery("#startdatehid").val();
		var todate = jQuery("#todatehid").val();
		getPrice(startdate, todate, jQuery("#propref").val(), numpeople, jQuery("#brandcode").val(), jQuery("#numpets").val());
	});
	
	jQuery("a#emailtoafriend").live('click', function() {
		jQuery("div.email-a-friend-modal-container").dialog({ 
			resizable: false, 
			modal: true, 
			width: 400,
			dialogClass: "email-a-friend-modal", 
			buttons: { 
				"Send": function() {
					// result error message
					var errorMsg = '';
					var boolValid = true;
					var radNames = '';
					jQuery("form#emailafriend").find(":input").each(function() {
						$(this).attr("disabled", true); 
						if($(this).hasClass("required")) {
							$(this).removeClass("invalid");
							if($(this).attr("name").indexOf("email") >= 0) {
								var mail_filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
								var tmpResult = mail_filter.test($(this).val());
								if(!tmpResult) {
									boolValid = false;
									errorMsg += "Email Address is not valid\n";
									$(this).addClass("invalid").fadeIn("slow");
								}
							}
							else {
								if ($(this).val() == "" || $(this).val() == $(this).attr("title")) {
									boolValid = false;
									errorMsg += $(this).attr("title") + "\n";
									$(this).addClass("invalid").fadeIn("slow");
								}
								else {
									if($(this).attr("type") == "radio") {
										var selected = true;
										if($('input[name=' + $(this).attr("name") + ']:checked').length == 0) {
											selected = false;
											radNames += $(this).attr("name");
										}
											
										if(!selected && $(this).attr("name").indexOf(radNames) < 0) {
											boolValid = false;
											errorMsg += $(this).attr("title") + "\n";
											$(this).addClass("invalid").fadeIn("slow");
										}
									}
									else if($(this).attr("type") == "checkbox") {
										if($(this).attr("checked")===false) {
											boolValid = false;
											errorMsg += $(this).attr("title") + "\n";
											$(this).addClass("invalid").fadeIn("slow");
										}							
									}
								}
							}	
						}
					});
						
					if (!boolValid) {
						alert("The following information is missing:\n\n" + errorMsg);
						$(this).find(":input").each(function() {
							$(this).attr("disabled", false); 
						});
					}
					else {
						$(this).find(":input").each(function() {
							$(this).attr("disabled", false); 
						});
						jQuery.postJSON('/includes/emailafriend.php', jQuery("form#emailafriend").serialize(), function(data) {
							if(data.status == 'ok') {
								jQuery("form#emailafriend").fadeOut(200, function() {
									jQuery("div#emailafrienderrormessage").html("<p>"+data.response+"</p>").fadeIn(200, function() {
										jQuery('div.email-a-friend-modal div.ui-dialog-buttonpane button.send').fadeOut(100);
									});
								});
							} else {
								alert(data.response);
							}
						});
					}
				}, 
				"Close": function() {
					jQuery(this).dialog("close"); 
				}
			}
		});
		var buttons = jQuery('div.email-a-friend-modal div.ui-dialog-buttonpane').children('button');
		jQuery(buttons[0]).addClass("send");
		jQuery(buttons[1]).addClass("close");
		return false;
	});
	
});
