$(function() { const wnd = $(window), wndWidth = wnd.width(), wndHeight = wnd.height(), header = $('.header'); if (wnd.width() > 1023) { var mt = 25; } else { var mt = 10; } wnd.scroll(function(){ let scrolled = wnd.scrollTop(); if (scrolled > mt) { header.addClass('fixed'); } else { header.removeClass('fixed'); } }); if (wnd.scrollTop() > mt) { header.addClass('fixed'); } $(".modal_close, .modal_continue").on("click", function() { $('.modal_wrap').removeClass('open') }); $('.modal_wrap').on('click', function(e) { if ($(e.target).closest('.modal').length === 0) { $('.modal_wrap').removeClass('open') } }); $(".modal_field span").on("click", function() { $(this).next('input').focus(); }); $('body').delegate('.products__item:not(.disabled)', 'click', function(e) { $('.modal_left').show(); $('.modal_right').show(); $('.modal_center').hide(); $('.modal_form input[name=date]').val($('.filter-date input[name=date]').val()); $('.modal_form input[name=start]').val($('.filter-start_range input[name=start]').val()); $('.modal_form input[name=resource_id]').val($(this).data('resource')); $('.modal_form input[name=guests]').val($('.filter-passangers input[name=guests]').val()); $('.modal_form input[name=hours]').val($('.filter-hours input[name=hours]').val()); $('.modal_left').css('background-image', 'url(' + $(this).find('s').data('image') + ')'); if($(this).data('file') != ''){ $('.modal_left .modal_left-specification').attr('href', $(this).data('file')).show(); } else { $('.modal_left .modal_left-specification').hide(); } $('.modal_left h2').text($(this).find('.item-info-title').text()); $('.modal_wrap').addClass('open'); }); $('.phone').mask('+7-999-999-9999'); $('.teaser__filter-firstline > div > span').on("click", function(){ $(this).next('input').focus(); }); $('.filter-start input').mask('99:00'); var sliderStart = $( "#sliderStart" ).slider({ orientation: "horizontal", range: "min", animate: true, min: 1, max: 24, value: $('.filter-start_range').data('time'), slide: function( event, ui ) { if(ui.value.toString().padStart(2, '0')=='24'){ $('.filter-start input').val('00:00'); //const selectedDate = myDatePicker.selectedDates[0]; //const nextDate = new Date(selectedDate.getTime()); //nextDate.setDate(nextDate.getDate() + 1); //myDatePicker.setDate(nextDate); } else { $('.filter-start input').val(ui.value.toString().padStart(2, '0')+':00'); } getFilterButton(); } }); $(".filter-start input").on( "change", function() { const [hours, minutes] = $(this).val().split(":"); if(hours > 23){ const hours = 23; $(this).val('23:00'); } if(hours == 24 || hours == '00'){ const hours = 24; $(this).val('00:00'); } sliderStart.slider( "value", hours); getFilterButton(); }); var sliderPrice = $( "#sliderPrice" ).slider({ range: true, orientation: "horizontal", animate: true, min: 4000, max: 130000, step: 1000, values: [ 4000, 130000 ], slide: function( event, ui ) { min = ui.values[0].toLocaleString('en-US'); max = ui.values[1].toLocaleString('en-US'); $('.filter-price_min').val(min); $('.filter-price_max').val(max); } }); $(".filter-price_min").on( "change", function() { numberStringWithoutCommas = $(this).val().replace(/,/g, ""); totalPrice = parseFloat(numberStringWithoutCommas); max = sliderPrice.slider( "values",1 ); $(this).val(totalPrice.toLocaleString('en-US')); if(totalPrice > max){ max = totalPrice; $(".filter-price_max").val(max.toLocaleString('en-US')); } sliderPrice.slider( "values", [totalPrice, max]); }); $(".filter-price_max").on( "change", function() { numberStringWithoutCommas = $(this).val().replace(/,/g, ""); totalPrice = parseFloat(numberStringWithoutCommas); min = sliderPrice.slider( "values",0 ); $(this).val(totalPrice.toLocaleString('en-US')); if(totalPrice < min){ min = totalPrice; $(".filter-price_min").val(min.toLocaleString('en-US')); } sliderPrice.slider( "values", [min, totalPrice]); }); $(".filter-passangers input").on( "change", function() { getFilterButton(); }); $(".filter-hours input").on( "change", function() { if($(this).val() > 23){ $(this).val('23'); } getFilterButton(); }); $(".filter-date input").on( "change", function() { getFilterButton(); }); function getFilterButton(){ if($(".filter-date input").val() != ''){ [day, month, year] = $(".filter-date input").val().split('.'); date = new Date(`${year}-${month}-${day}`); formattedDate = date.toLocaleDateString('ru-RU', { day: 'numeric', month: 'long', year: 'numeric', }); formattedDate+=' с '+$(".filter-start input").val(); } else { formattedDate = ''; } forwho = ''; if($('.filter-passangers input').val() != ''){ forwho+='Для '+$('.filter-passangers input').val()+' '+declension($('.filter-passangers input').val(), ['человека','человек','человек'])+' '; } if($('.filter-hours input').val() != ''){ forwho+='на '+$('.filter-hours input').val()+' '+declension($('.filter-hours input').val(), ['час','часа','часов'])+' '; } if(forwho != '' && formattedDate != ''){ forwho+=' - '; } filterButton = forwho+formattedDate; if(filterButton != ''){ $('.filter-button_result').text(filterButton).slideDown(); } } today = new Date(); formattedDate = today.toLocaleDateString('ru-RU', { day: '2-digit', month: '2-digit', year: 'numeric' }); const myDatePicker = $(".filter-date input").flatpickr({ minDate: "today", enableTime: false, dateFormat: "d.m.Y", locale: "ru", defaultDate: formattedDate }); getFilterButton(); function declension(number, options) { const cases = [2, 0, 1, 1, 1, 2]; return options[(number % 100 > 4 && number % 100 < 20) ? 2 : cases[number % 10 < 5 ? number % 10 : 5]]; } $('body').delegate('.modal_form', 'submit', function(e) { e.preventDefault(); var form = $(this); form.find('input').removeClass('error'); form.find('.errors').remove(); errors = false; form.find('input').each(function(){ if($(this).val() == ''){ errors = true; $(this).addClass('error'); } }); if(errors == false){ $.ajax({ type: 'POST', url: '/index/book', data: form.serialize(), success: function(response) { if(response.success){ $('.modal_left').hide(); $('.modal_right').hide(); $('.modal_center').show(); } if(response.error){ form.append('
'+response.error+'
'); } }, error: function() { alert('Form submission error'); } }); } }); $('body').delegate('.teaser__filter', 'submit', function(e) { e.preventDefault(); var form = $(this); $.ajax({ type: 'POST', url: '/index/find', data: form.serialize(), beforeSend: function(){ $('.products').addClass('loading'); $('.products').html(''); }, success: function(response) { setTimeout(function(){ $('.products').removeClass('loading'); $('.products').html(response); if(wndWidth < 820){ $('.item-info-options li').each(function(){ if($(this).find('span').data('short') != ''){ $(this).find('span').text($(this).find('span').data('short')); } }); } },500); }, error: function() { alert('Form submission error'); } }); }); $('.teaser__filter').submit(); if(wndWidth < 820){ $('input').on('focus', function() { let value = $(this).val(); $(this).val('').val(value); }); } });