/*
 * Jquery actions
 */
$(document).ready(function() {
	
	$(document).ajaxStart($.blockUI).ajaxStop($.unblockUI);

	$("a.interstitial").click(function() {
		interstitial();
	});
	
	$("a.overlaywinhref").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false,
		'href'			:	this.href,
		'type'			:	'iframe',
		'autoScale'		:	false,
		'autoDimensions':	false,
		'height'		:	550,		
		'onStart'		:	function() {
								$.blockUI({ baseZ: 9999  });
							},
		'onCancel'		:	function() {
								$.unblockUI();
							},
		'onComplete'	:	function() {
								$.unblockUI();
							}
	});
	
	$("a.overlaywinsearch").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false,
		'href'			:	this.href,
		'type'			:	'iframe',
		'autoScale'		:	false,
		'autoDimensions':	false,
		'width'			:	255,
		'height'		:	420,		
		'onStart'		:	function() {
								$.blockUI({ baseZ: 9999  });
							},
		'onCancel'		:	function() {
								$.unblockUI();
							},
		'onComplete'	:	function() {
								$.unblockUI();
							}
	});	
	
	$("a.editrontext").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false,
		'href'			:	this.href,
		'type'			:	'iframe',
		'autoScale'		:	true,
		'autoDimensions':	true,
		'height'		:	300,
		'onStart'		:	function() {
								$.blockUI({ baseZ: 9999  });
							},
		'onCancel'		:	function() {
								$.unblockUI();
							},
		'onComplete'	:	function() {
								$.unblockUI();
							}
	});
	
	$("#quicktranslations_form").submit(function() {	
		var valid = jQuery("#quicktranslations_form").validationEngine('validate');
		if(valid) {
			var str = $("#quicktranslations_form").serialize();
			$.ajax( {
				type : "POST",
				url : "/en/index/quickedit",
				data : str,
				success : function(msg) {
					if (msg == true) {
						showSuccessToast('Translation Updated');
						parent.window.location.reload();
					} else {
						showErrorToast('Translation Not Updated');
					}
				}
			});
		}
	    return false;
	});
	
	$("#callme_form").submit(function() {	
		var valid = jQuery("#callme_form").validationEngine('validate');
		if(valid) {
			var str = $("#callme_form").serialize();
			$.ajax( {
				type : "POST",
				url : "/en/fav-link/call",
				data : str,
				success : function(msg) {
					if (msg == true) {
						showStickySuccessToast('Thank you for your interest!! We will call you back');
						parent.$.fancybox.close();
					} else {
						showStickyErrorToast('Sorry. We were unable process your request. Please try again.');
					}
				}
			});
		}
	    return false;
	});
	
	$("#email_form").submit(function() {	
		var valid = jQuery("#email_form").validationEngine('validate');
		if(valid) {
			$("#customurl").val(window.parent.location.href);
			var str = $("#email_form").serialize();
			$.ajax( {
				type : "POST",
				url : "/en/fav-link/email",
				data : str,
				success : function(msg) {
					if (msg == true) {
						showStickySuccessToast('Thank you for your interest!! We will call you back');
						parent.$.fancybox.close();
					} else {
						showStickyErrorToast('Sorry. We were unable process your request. Please try again.');
					}
				}
			});
		}
	    return false;
	});
	
	$("#quote_form #country").change(function() {
		var country = $(this).val() ? $(this).val() : '';
		if(country) {
			$.getJSON('/en/search/cities?country='+country, function(j) {
				if(j) {
					var options = '<option value="">Select a Resort / City</option>';
					for(var i = 0; i < j.length; i++) {
						if(j[i].id) {
							var selected = false;
							if(j[i].selected) {
								selected = "selected='selected'";
							}
							options += '<option value="' + j[i].id + '"' + selected + '>' + j[i].name + '</option>';
						}
					}
					$('#city').html(options);
					$('#city').change();
				}
			});
			$.getJSON('/en/search/property-types?country='+country, function(j) {
				if(j) {
					var options = '<option value="">Select</option>';
					for(var i = 0; i < j.length; i++) {
						if(j[i].id) {
							var selected = false;
							if(j[i].selected) {
								selected = "selected='selected'";
							}
							options += '<option value="' + j[i].id + '"' + selected + '>' + j[i].name + '</option>';
						}
					}
					$('#propertyType').html(options);
					$("#propertyType").change();				
				}
			});
			$.getJSON('/en/search/properties?country='+country, function(j) {
				if(j) {
					var options = '<option value="">Select</option>';
					for(var i = 0; i < j.length; i++) {
						if(j[i].id) {
							var selected = false;
							if(j[i].selected) {
								selected = "selected='selected'";
							}
							options += '<option value="' + j[i].id + '"' + selected + '>' + j[i].name + '</option>';
						}
					}
					$('#property').html(options);
				}
			});			
		}
	}).change();	
	
	$("#quote_form #city").change(function() {
		var city = $(this).val() ? $(this).val() : '';
		if(city) {
			$.getJSON('/en/search/properties?city='+city, function(j) {
				if(j) {
					var options = '<option value="">Select</option>';
					for(var i = 0; i < j.length; i++) {
						if(j[i].id) {
							var selected = false;
							if(j[i].selected) {
								selected = "selected='selected'";
							}
							options += '<option value="' + j[i].id + '"' + selected + '>' + j[i].name + '</option>';
						}
					}
					$('#property').html(options);
				}
			});
			$.getJSON('/en/search/property-types?city='+city, function(j) {
				if(j) {
					var options = '<option value="">Select</option>';
					for(var i = 0; i < j.length; i++) {
						if(j[i].id) {
							var selected = false;
							if(j[i].selected) {
								selected = "selected='selected'";
							}
							options += '<option value="' + j[i].id + '"' + selected + '>' + j[i].name + '</option>';
						}
					}
					$('#propertyType').html(options);
					$("#propertyType").change();			
				}
			});			
		}
	}).change();
	
	$("#quote_form #propertyType").change(function() {
		var country = $("#country").val() ? $("#country").val() : '';
		var city = $("#city").val() ? $("#city").val() : '';
		var propertyType = $(this).val() ? $(this).val() : '';
		if( (country || city) && propertyType) {
			$.getJSON('/en/search/properties?city='+city+'&country='+country+'&propertyType='+propertyType, function(j) {
				if(j) {
					var options = '<option value="">Select</option>';
					for(var i = 0; i < j.length; i++) {
						if(j[i].id) {
							var selected = false;
							if(j[i].selected) {
								selected = "selected='selected'";
							}
							options += '<option value="' + j[i].id + '"' + selected + '>' + j[i].name + '</option>';
						}
					}
					$('#property').html(options);
				}
			});		
		}
	}).change();	
	
	$("#search_form #country").change(function() {
		var country = $(this).val() ? $(this).val() : '';
		var style = $("#style").val() ? $("#style").val() : '';
		// if country is france or corsica then only display the type
		if(country == 21 || country == 23) {
			$("#package-type").hide();
		} else {
			$("#package-type").hide();
		}
		if(country) {
			$.getJSON('/en/search/cities?country='+country+'&style='+style, function(j) {
				if(j) {
					var options = '<option value="">Select a Resort / City</option>';
					for(var i = 0; i < j.length; i++) {
						if(j[i].id) {
							var selected = false;
							if(j[i].selected) {
								selected = "selected='selected'";
							}
							options += '<option value="' + j[i].id + '"' + selected + '>' + j[i].name + '</option>';
						}
					}
					$('#city').html(options);
					$('#city').change();
					updateSearchFormSelects();
				}
			});
		}
	}).change();
	
	$("#search_form #city").change(function() {
		var city = $(this).val() ? $(this).val() : '';
		var propertyType = $('#propertyType').val() ? $('#propertyType').val() : '';
		if(city) {
			$.getJSON('/en/search/property-types?city='+city, function(j) {
				if(j) {
					var options = '<option value="">Select a Property Type</option>';
					for(var i = 0; i < j.length; i++) {
						if(j[i].id) {
							var selected = false;
							if(j[i].selected) {
								selected = "selected='selected'";
							}
							if(!propertyType) {
								selected = false;
							}
							options += '<option value="' + j[i].id + '"' + selected + '>' + j[i].name + '</option>';
						}
					}
					$('#propertyType').html(options);
					$("#propertyType").change();				
				}
			});
		}
	}).change();

	$("#search_form #propertyType").change(function() {
		var propertyType = $(this).val() ? $(this).val() : '';
		var a = $('#adults_1').val();
		var c = $('#children_1').val();
		// if propertyType is hotels then display rooms and star rating
		if(propertyType == 1) {
			$(".hotels").show();
			$(".villa").hide();
			$("#rooms").val(1);
			$("#rooms").hide();	
			$("#rating").show();

			var adults = '';
			for(var i = 0; i < 5; i++) {
				var selected = false;
				if(i == a) {
					selected = "selected='selected'";
				}				
				adults += '<option value="' + i + '" label="' + i + '"' + selected + '>' + i + '</option>';
			}			
			$('#adults_1').html(adults);
			
			var children = '';
			for(var i = 0; i < 5; i++) {
				var selected = false;
				if(i == c) {
					selected = "selected='selected'";
				}
				children += '<option value="' + i + '" label="' + i + '"' + selected + '>' + i + '</option>';
			}			
			$('#children_1').html(children);
		} else if (propertyType == 8) {
			$(".hotels").hide();
			$(".villa").show();
			$("#rooms").val(1);
			$("#rooms").hide();
			$("#rating").show();

			var adults = '';
			for(var i = 1; i < 21; i++) {
				var selected = false;
				if(i == a) {
					selected = "selected='selected'";
				}
				adults += '<option value="' + i + '" label="' + i + '"' + selected + '>' + i + '</option>';
			}			
			$('#adults_1').html(adults);
			
			var children = '';
			for(var i = 0; i < 20; i++) {
				var selected = false;
				if(i == c) {
					selected = "selected='selected'";
				}
				children += '<option value="' + i + '" label="' + i + '"' + selected + '>' + i + '</option>';
			}			
			$('#children_1').html(children);			
		}
		else {
			$(".hotels").hide();
			$(".villa").hide();
			$("#rating").hide();
			$("#rooms").hide();
			$('#adults_1').val(2);
			$('#children_1').val(0);
		}
		$("#rooms").change();
		$(".childrenselect").change();
	}).change();
	
	$("input[name=thisproperty]").change(function() {
		updateSearchFormSelects();
	}).change();
	
	$("#rooms").change(function() {
		var propertyType = $("#propertyType").val();
		var rooms = $(this).val() ? $(this).val() : '';
		if(!propertyType) {
			rooms = '';
		}
		// show as many paxes combo as rooms
		for(var i=1;i<=rooms;i++) {
			$(".roomsclass_"+i).show();
		}
		// hide the rest
		for(var i=5;i>rooms;i--) {
			$(".roomsclass_"+i).hide();
			if(rooms) {
				$("#adults_"+i).val(0);
				$("#children_"+i).val(0);
				for(var j=1;j<=19;j++) {
					$("#childage_"+i+"_"+j).val(0);
				}
			}
		}
	}).change();	
	
	$(".childrenselect").change(function() {
		var id = this.id;
		var patt = /children_(\d)/;
		var match = id.match(patt);
		var room = match[1];
		var children = $(this).val() ? $(this).val() : '';
		// show as many chilren ages drop-down as there are children
		for(var i=1;i<=children;i++) {
			$(".childageclass_"+room+"_"+i).show();
		}
		// hide the rest
		for(var i=20;i>children;i--) {
			$(".childageclass_"+room+"_"+i).hide();
		}
	}).change();
	
	$('.datepicker').setDefaultValue();
	var dates = $( "#departureDate, #returnDate" ).datepicker({
		changeMonth: true,
		changeYear: true,
		numberOfMonths: 1,
		showButtonPanel: true,
		minDate: -0,
		maxDate: "+1Y",
		'dateFormat':'dd/mm/yy',
		onSelect: function( selectedDate ) {
			var option = this.id == "departureDate" ? "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 );
		}
	});

});

function interstitial() {
	window.parent.$.blockUI({ 
		message: '<h2><img src="/images/busy.gif" /> Your holiday is on its way in just a moment</h2>',
		baseZ: 9999
	});
}

function checkChildren(field, rules, i, options) {
	var id = field.attr("id");
	var patt = /children_(\d)/;
	var match = id.match(patt);
	var room = match[1];	
	var val = field.val();
	for(i=1;i<=val;i++) {
		if($("#childage_"+room+"_"+i).is(":hidden") || !$("#childage_"+room+"_"+i).val() || $("#childage_"+room+"_"+i).val() == 0) {
			jQuery("#childage_"+room+"_"+i).validationEngine('showPrompt', options.allrules.childrenCount.alertText, 
					'error', true);	
			return options.allrules.childrenCount.alertText;
		} else {
			jQuery("#childage_"+room+"_"+i).validationEngine('hide');	
		}
	}
}

function checkPax(field, rules, i, options) {
	var val = $("#rooms").val();
	var allowedPax = val * 400;
	var pax = 0;
	for(i=1;i<=val;i++) {
		var roomPax = parseInt($("#adults_"+i).val()) + parseInt($("#children_"+i).val());
		if(roomPax <= 0) {
			return options.allrules.roomPaxCount.alertText;
		}
		pax += roomPax;
	}
	if(allowedPax < pax) {
		return options.allrules.paxCount.alertText;
	}
}

function updateSearchFormSelects() {
	var property = $("#property").val() ? $("#property").val() : '';
	// if property is provided then do not allow to change country/city/property
	// type and rating
	if(property) {
		var thisproperty = $("input[name=thisproperty]:checked").val();
		if(thisproperty == 1) {
			alterSelectState('country', true);
			alterSelectState('city', true);			
			alterSelectState('propertyType', true);
			alterSelectState('rating', true);
			$("#property").attr('disabled', false);
		} else if (thisproperty == 0) {
			alterSelectState('country', true);
			alterSelectState('city', true);
			alterSelectState('propertyType', false);
			alterSelectState('rating', false);
			$("#property").attr('disabled', true);
		} else {
			alterSelectState('country', false);
			alterSelectState('city', false);
			alterSelectState('propertyType', false);
			alterSelectState('rating', false);
			$("#property").attr('disabled', false);			
		}
	}
}

function alterSelectState(id, state) {
	var currentVal = $("#"+id).val();
	$("#"+id+"> option").each(function() {
		if($(this).val() != currentVal) {
			$(this).attr('disabled', state);
		}
	});
}
