const hotel_search__autocomplete = 'https://book.maldivlertatili.com//hotels-search-autocomplete/'; const hotel_search__check_destination_url = 'https://book.maldivlertatili.com//hotels-check-destination/'; const transfer_search__transfer_from = 'https://book.maldivlertatili.com//transfer-search/transfer-from/'; const transfer_search__transfer_from_hint = 'https://book.maldivlertatili.com//transfer-search/transfer-from-hint/'; const transfer_search__transfer_to = 'https://book.maldivlertatili.com//transfer-search/transfer-to/'; const transfer_search__transfer_to_hint = 'https://book.maldivlertatili.com//transfer-search/transfer-to-hint/'; let hotel_url = undefined; const transfer_search_url = 'https://book.maldivlertatili.com//transfer-search/'; const trip_search_url = 'https://book.maldivlertatili.com//trip-search/';
$(function () {
// ========= ОТЕЛИ ================ const today_date = new Date() $('#id_date_start, #id_date_end').on('change', function (e) { var new_date = $(this).val() new_date = new_date.split('.') new_date = new Date(new_date[2], parseInt(new_date[1], 10) - 1, new_date[0]) if (new_date < today_date) { alert(warning_date_msg) $(this).val(today_date.getDate() + '.' + (today_date.getMonth() + 1) + '.' + today_date.getFullYear()) } }); // =========================================================================== /* Meal plan */ function hotelFilterMeal() { var filter_meal = $('#hotel-filter-meal').val(); $('#meal-plan').val(filter_meal); } var $meal_plan_hidden_input = $('#meal-plan'); var $hotel_filter_meal = $('#hotel-filter-meal'); var hotel_meal_plan_initial = $meal_plan_hidden_input.val(); if ($hotel_filter_meal.length) { $hotel_filter_meal.change(hotelFilterMeal); if (hotel_meal_plan_initial) { $hotel_filter_meal.val(hotel_meal_plan_initial) } else hotelFilterMeal(); } else console.warn('No #hotel-filter-meal element on page.'); /* конец Meal plan */ // ============================================================================= // =========================================================================== /* Destination */ var $hotel_filter_country = $('#hotel-filter-country'); var $destination = $('#id_destination'); var $request_with = $('#id_request_with'); var $resort_slug = $('#id_resort_slug'); var $request_value = $('#id_request_value'); $.widget('custom.catcomplete', $.ui.autocomplete, { _create: function () { this._super(); this.widget().menu('option', 'items', '> :not(.ui-autocomplete-category)'); }, _renderMenu: function (ul, items) { var that = this, currentCategory = ''; $.each(items, function (index, item) { var li; if (item.category !== currentCategory) { ul.append('
'); currentCategory = item.category; } li = that._renderItemData(ul, item); if (item.category) { li.attr('aria-label', item.category + ' : ' + item.label); } }); }, _renderItem: function (ul, item) { var inner_html = ''; if (false && item.thumb) { // } else { inner_html = '
'; } return $('
') .data('ui-autocomplete-item', item) .append(inner_html) .appendTo(ul); } });
$destination.keyup(function (event) { $request_with.val("") $resort_slug.val("") $hotel_filter_country.val("") });
$destination.catcomplete({ delay: 100, minLength: 2, autoFocus: true, source: function (request, response) { $.ajax({ url: hotel_search__autocomplete, dataType: 'json', data: { query: request.term, rate_code: $('#id_rate_code').val() }, success: function (data) { response(data); } }); }, select: function (event, ui) { if (ui.item) { $request_with.val(ui.item.requestWith); $request_value.val(ui.item.requestValue); if (ui.item.requestWith === "country_id") { $hotel_filter_country.find('option[value=' + ui.item.label.toLowerCase() + ']').prop('selected', true)
} else { if (ui.item.requestWith === "resort_id") $request_with.val("resort_id") $hotel_filter_country.val("") if (ui.item.requestWith === "hotel_id") hotel_url = ui.item.url } } } });
$destination.change(function (event) { if (!$destination.val()) { $request_with.val(''); $request_value.val(''); } });
$("#id_transfer_from").catcomplete({ minLength: 2, autoFocus: true, maxResults: 20, source: function (request, response) { $.ajax({ url: transfer_search__transfer_from, dataType: "json", data: { term: request.term, transfer_from_country_id: $('#transfer_from_country_id').val(), }, success: function (data) { response(data); } }); }, select: function (event, ui) { if (ui.item && ui.item.RequestValue && ui.item.RequestWith) { var $transfer_to_request_with = $("#transfer-from-request-with"), $transfer_to_request_value = $("#transfer-from-request-value"), $transfer_to = $("#id_transfer_to");
$transfer_to_request_with.val(ui.item.RequestWith); $transfer_to_request_value.val(ui.item.RequestValue);
$('#transfer-to-hint-wrapper').removeClass('hidden'); $transfer_to.val("").removeAttr("disabled"); $('#transfer-to-hint-loader-wrapper').removeClass('hidden'); $('#transfer-to-hint-icon-wrapper').addClass('hidden'); setTimeout("$('#transfer-to-hint-loader-wrapper').addClass('hidden');", 1000); setTimeout("$('#transfer-to-hint-icon-wrapper').removeClass('hidden');", 1000);
var transfer_search_transfer_to_hint = $('#transfer-search-transfer-to-hint'); var href = transfer_search_transfer_to_hint.data("href"); transfer_search_transfer_to_hint.attr('href', href + '?transfer_from_request_with=' + ui.item.RequestWith + '&transfer_from_request_value=' + ui.item.RequestValue); $('#transfer-from-menu').text(ui.label); LoadFastHintsTo(); } else { $transfer_to.attr("disabled", "disabled"); } } });
$("#id_transfer_to").catcomplete({ minLength: 2, maxResults: 20, source: function (request, response) { $.ajax({ url: transfer_search__transfer_to, dataType: "json", data: { term: request.term, transfer_from_request_with: $('#transfer-from-request-with').val(), transfer_from_request_value: $('#transfer-from-request-value').val() }, success: function (data) { response(data); } }); }, select: function (event, ui) { if (ui.item) { $("#transfer-to-request-with").val(ui.item.RequestWith); $("#transfer-to-request-value").val(ui.item.RequestValue); $('#transfer-to-menu').text(ui.label); $("#show-transfers-button").attr('disabled', false); } else { $("#show-transfers-button").attr('disabled', true); } } }); /* конец Destination */ // ===========================================================================
// =========================================================================== /* Arrival & Departure */
var datepicker_exists = $('.datepicker').length; var $date_start = $("#id_date_start"); var $date_end = $("#id_date_end");
function removeDateStartCookie() { var booking_date_start = document.cookie.match(/add_new_hotel_to_booking_date_end=(\d{2}.\d{2}.\d{4})/); if (booking_date_start) { booking_date_start = booking_date_start[1] document.cookie = 'add_new_hotel_to_booking_date_end=' + encodeURIComponent(booking_date_start) + '; path=/; max-age=0'; } } $date_start.on('change', removeDateStartCookie) $date_end.on('change', removeDateStartCookie)
let initial_date_start = new Date(today_date.setDate(today_date.getDate() + 14)) let initial_date_end = new Date(today_date.setDate(today_date.getDate() + 7))
if (datepicker_exists) { $date_start.datepicker({ minDate: new Date(), dateFormat: 'dd.mm.yy', firstDay: 1, onSelect: function (selectedDate) { var date = $(this).datepicker('getDate'); date.setDate(date.getDate() + 7); $date_end.datepicker('setDate', date); } }); $date_end.datepicker({ dateFormat: 'dd.mm.yy', firstDay: 1, }); $date_start.datepicker("setDate", initial_date_start); $date_end.datepicker("setDate", initial_date_end); } else console.warn('No .datepicker\'s on page.');
/* конец Arrival & Departure */ // =============================================================================
// =========================================================================== /* Dates unknown */
$("#id_dates_unknown").click(function () { if ($(this).prop('checked')) { $date_start.css('opacity', 0.5); $date_end.css('opacity', 0.5); $date_start.datepicker('destroy'); $date_end.datepicker('destroy'); } else { $date_start.css('opacity', 1); $date_end.css('opacity', 1); $date_start.datepicker({ dateFormat: 'dd.mm.yy', firstDay: 1 }); $date_end.datepicker({ dateFormat: 'dd.mm.yy', firstDay: 1 }); } });
/* конец Dates unknown */ // ===========================================================================
// =========================================================================== /* Travellers ages */ function CalcTravellersAges() { var travellers_ages = [] adults = $('#id_adults').val(); for (var i = 0; i < adults; i++) { travellers_ages.push(25) } $('span[class="child-label"]').each(function () { var child_age = parseInt($(this).text().replace(/\+/i, '').replace(/ /i, '')); travellers_ages.push(child_age); }) $('#travellers-ages').val(travellers_ages.join('-')); } $('#id_adults').change(CalcTravellersAges); // =========================================================================== /* Child ages */ var $childs = $("#id_childs"); var child_ages = []; var check = false; $("#id_childs, #id_transfer_childs").popover({ html: true, trigger: 'manual', }).on('shown.bs.popover', function () { $('.popover').find('select').each(function (index, element) { if (element.name in child_ages) { $(element).val(child_ages[element.name]); } }); check = false; $('.popover').find('.popover-content').addClass('clearfix'); var _this = this; $(document).on('click', function (e) { if (!$('.popover').is(e.target) && !$childs.is(e.target) && $('.popover').has(e.target).length === 0 && $childs.has(e.target).length === 0) { $(_this).popover('hide'); } }) }).on('mouseenter', function () { var _this = this; if (!$(this).data()['bs.popover'].tip().hasClass('in') && $(this).val() > 0) $(this).popover('show'); $(this).siblings('.popover').on('mouseleave', function () { setTimeout(function () { if (!$('.popover:hover').length && !check) { $(_this).popover('hide'); } }, 500); }); }).on('mouseleave', function () { var _this = this; //console.log("HERE1 ", _this); setTimeout(function () { //console.log("HERE2 ", $('.popover:hover').length, check); if (!$('.popover:hover').length && !check) { //console.log ("HERE3") $(_this).popover('hide'); } }, 100); });
var prev_children_count = 0;
$(document).on('change', 'select[name^="child"]', function () { // этот коллбэк срабатывает при выборе селекта количества детей, // или селекта возраста одного ребёнка
var name = $(this).attr('name');
var value = $(this).val(); child_ages[name] = '' + value;
var dependent_selects = $('select[name=' + name + ']'); dependent_selects.each(function (index, element) { // реактивные изменения во всех связанных селектах $(this).val(value); });
// для формы поиска в стиле мальдивз-бонус
if (name === 'childs') { // при изменении количества детей var $hotel_search_children_count = $('.hotel-search__children-count'); if ($hotel_search_children_count.length) { var $hotel_search_people_count = $hotel_search_children_count.closest('.hotel-search__people-count'); if (parseInt(value, 10) > 0) { $hotel_search_people_count.addClass('hotel-search__people-count--with-children'); $hotel_search_children_count.removeClass('hidden').find('.count').html(value); } else { $hotel_search_children_count.addClass('hidden').find('.count').html(value); $hotel_search_people_count.removeClass('hotel-search__people-count--with-children'); } } }
// конец: для формы поиска в стиле мальдивз-бонус
// Показываем возраста детей:
var $quantity_child_wrapper = $(this).closest('.quantity-child-wrapper'); var $child_ages_wrapper = $('.child-ages-wrapper'); if ($child_ages_wrapper && $quantity_child_wrapper) { var $child_age_selects = $child_ages_wrapper.find('.child-age-added select'); var next_children_count = $child_age_selects.length; var children_count_decreased = next_children_count < prev_children_count; var $children_quantity = $quantity_child_wrapper.find('.children-quantity'); if (next_children_count) { // количество детей больше нуля if (children_count_decreased) { // если количество детей уменьшилось var delete_count = prev_children_count - next_children_count; hide_children_age_labels(delete_count, $quantity_child_wrapper); // прячем неиспользуемые лэйблы детей } render_children_age_labels($child_age_selects, $quantity_child_wrapper); if ($children_quantity.is(':hidden')) $children_quantity.show(); // отображаем блок с количеством возрастов } else if (!next_children_count) { // количество детей обнулилось hide_children_age_labels(prev_children_count, $quantity_child_wrapper); // прячем неиспользуемые лэйблы детей if ($children_quantity.is(':visible')) $children_quantity.hide(); // скрываем блок с количеством возрастов } prev_children_count = next_children_count; } else { console.warn('There is no .quantity-child-wrapper in select ancestors or .child-ages-wrapper no on page!'); } function hide_children_age_labels(count, $wrapper_element) { var $children_age_labels = $wrapper_element.find('[data-child-number]:not([style*="display: none"])'); if ($children_age_labels.length) { var $children_age_labels_to_delete = $children_age_labels.slice(-count); $children_age_labels_to_delete.each(function (i, el) { $(el).hide(); }); return true; } else { return false; } } function render_children_age_labels($selects, $wrapper_element) { $selects.each(function (i, select_element) { var select_element_name = $(select_element).attr('name'); var child_age_value = $(select_element).val(); var $child_age_label = get_or_create_child_age_label(select_element_name, $wrapper_element); var set_plus = i ? ' + ' : ''; $child_age_label.html(set_plus + child_age_value); if ($child_age_label.is(':hidden')) $child_age_label.show(); }); } function get_or_create_child_age_label(select_name, $ancestor_for_search) { var $children_age_labels = $ancestor_for_search.find('[data-child-number]'); var should_create = !$children_age_labels || !$ancestor_for_search.find('[data-child-number=' + select_name + ']').length; if (should_create) { var $children_quantity_value = $ancestor_for_search.find('.children-quantity__value'); $children_quantity_value.html($children_quantity_value.html() + '' ); } return $ancestor_for_search.find('[data-child-number=' + select_name + ']'); } CalcTravellersAges(); });
$("#id_childs, #id_transfer_childs").change(function () { var counter_start = null; var popover; var remove = false; var child_number = 0; var child_quantity = parseInt($(this).val(), 10); var quantity_child_wrapper = $(this).closest('.quantity-child-wrapper'); var child_ages_wrapper = quantity_child_wrapper.find('.child-ages-wrapper');
child_ages_wrapper.find('.child-age-template.child-age-added').each(function () { child_number = $(this).find('.child-number').text(); child_number = parseInt(child_number, 10); if (child_number > child_quantity) { remove = true; $(this).remove(); } });
if (!remove) { if ((child_number > 0) && (child_number < child_quantity)) { counter_start = child_number + 1; } else { counter_start = 1; } var html = child_ages_wrapper.html(); for (var i = counter_start; i <= child_quantity; i++) { var current = 'child__' + i; var child_age_template = $(this).closest('.quantity-child-wrapper').find('.child-ages-wrapper').find('.child-age-template.for-clone').clone(); var child_age_template_select = child_age_template.find('select'); child_age_template.addClass('child-age-added'); child_age_template.removeClass('for-clone').removeClass('hidden'); child_age_template_select.attr('class', 'form-control input-sm child__' + i); child_age_template_select.attr('name', current); if (current in child_ages) { child_age_template_select.find('option[value=' + child_ages[current] + ']').attr('selected', 'selected'); } child_age_template.find('.child-number').text(i); child_ages_wrapper.find('.child-ages-block').append(child_age_template); } } if (!child_ages_wrapper.hasClass('hidden')) child_ages_wrapper.addClass('hidden'); $(this).attr('data-content', child_ages_wrapper.html()); popover = $(this).data('popover'); if (popover) { popover.setContent(); } if (child_quantity > 0) $(this).popover('show'); else $(this).popover('hide'); check = true; });
// начальные дети $("#id_childs, #id_transfer_childs").change(); CalcTravellersAges(); $('.popover').hide();
/* конец Child ages */ // ===========================================================================
// =========================================================================== /* Guests and Nationality*/ var $hotel_filter_guests = $("#hotel-filter-guests"), $hotel_filter_nationality = $("#hotel-filter-nationality"), $id_guests = $("#id_guests"), $id_nationality = $("#id_nationality");
$id_guests.on('change', function () { $hotel_filter_guests.text($id_guests.val()) }) $id_nationality.on('change', function () { $hotel_filter_nationality.text($id_nationality.find('option:selected').data('iso')) }) /* конец Guests and Nationality */ // ===========================================================================
var $submit_button = $('#hotel-search-form-submit');
$submit_button.click(function (e) { e.preventDefault(); $('#hotel-search-form-top-panel-additional').html('') if ($request_with.val() === "hotel_id" && hotel_url) { var get_params = $("#hotel-search-form").serialize(); window.location = 'https://book.maldivlertatili.com/' + hotel_url + "?" + get_params; } else {
if ($destination.val() && !$request_with.val()) { $.ajax({ url: hotel_search__check_destination_url, dataType: 'json', data: { query: $destination.val(), rate_code: $('#id_rate_code').val() }, success: function (data) { if ("success" in data) { $('#hotel-search-form-top-panel-additional').append(data["success"]) $('#hotel-search-form-top-panel-additional').on('click', 'a[data-request-with="country _id"], a[data-request-with="resort_id"]', function (e) { var request_with_val = $(this).data('request-with') $request_with.val(request_with_val) if (request_with_val === 'country_id') $request_value.val($(this).data('request-value')) if (request_with_val === 'resort_id') $request_value.val($(this).data('request-value')) $destination.val($(this).text()) }) } } }); } else { $("#hotel-search-form").submit(); // window.location = $("#hotel-search-form").attr('action') + '/?' + $("#hotel-search-form").serialize() } } });
// ========= ОТЕЛИ КОНЕЦ ================
})