jQuery(document).ready(function ($) {

    // Preloader
    jQuery('#preloader').delay(1500).fadeOut();
    jQuery('#loader').delay(1500).fadeOut("slow");

    // Scroll to Top Button
    if ($('.go-to-top').length) {
        var scrollTrigger = $('body').position(); // px
        var goToTop = function () {
            var scrollTop = $(window).scrollTop();
            if (scrollTop > 150) {
                $('.footer-go-to-top').addClass('show');
            } else {
                $('.footer-go-to-top').removeClass('show');
            }
        };
        goToTop();
        $(window).on('scroll', function () {
            goToTop();
        });
        $('.go-to-top').on('click', function (e) {
            e.preventDefault();
            $('html, body').animate({
                scrollTop: scrollTrigger.top
            }, 700);
        });
    }

    // Mobile Menu Focus
    $(window).resize(rental_estate_realtor_navTrapp);

    function rental_estate_realtor_navTrapp() {
        var width = $(window).width();
        if (width < 992) {
            $('.main-navigation').on('keydown', function (e) {
                if ($('.main-navigation').hasClass('toggled')) {
                    var focusableEls = $('.main-navigation a[href]:not([disabled]), .main-navigation button');
                    var firstFocusableEl = focusableEls[0];
                    var lastFocusableEl = focusableEls[focusableEls.length - 1];
                    var KEYCODE_TAB = 9;
                    var KEYCODE_ESC = 27;
                    if (e.key === 'Tab' || e.keyCode === KEYCODE_TAB) {
                        if (e.shiftKey) { // shift + tab
                            if (document.activeElement === firstFocusableEl) {
                                lastFocusableEl.focus();
                                e.preventDefault();
                            }
                        } else { // tab
                            if (document.activeElement === lastFocusableEl) {
                                firstFocusableEl.focus();
                                e.preventDefault();
                            }
                        }
                    }
                    if (e.keyCode === KEYCODE_ESC) {
                        $('.main-navigation').removeClass('toggled');
                        return;
                    }
                }
            });
        }
    }

    rental_estate_realtor_navTrapp();

});

// ---------------tab section--------------//
(function() {
    const tabs = document.querySelectorAll(".property-tabs__item");
    const items = document.querySelectorAll("#property-list .property-item");

    function rental_estate_realtor_applyFilter(filter) {
        items.forEach(function(item) {
            if (filter === "*") {
                item.style.display = "";
                return;
            }
            if (filter === "none") {
                item.style.display = "none";
                return;
            }
            // filter like ".property-type-12"
            const matches = item.classList.contains(filter.replace('.', ''));
            item.style.display = matches ? "" : "none";
        });
    }

    tabs.forEach(function(tab) {
        tab.addEventListener("click", function() {
            tabs.forEach(t => t.classList.remove("active"));
            this.classList.add("active");
            const filter = this.getAttribute("data-filter") || "*";
            rental_estate_realtor_applyFilter(filter);
        });

        // keyboard accessibility
        tab.addEventListener("keydown", function(e){
            if (e.key === "Enter" || e.key === " ") {
                e.preventDefault();
                this.click();
            }
        });
    });

    // Default: show All
    rental_estate_realtor_applyFilter("*");
})();


// --------------------Search form js-------------------//

document.addEventListener("DOMContentLoaded", function() {
    // Property Type dropdown
    let type = document.querySelector('select[name="type"]');
    if(type){
        type.setAttribute("data-placeholder", "Property Type");
        let typeFirst = type.querySelector("option:first-child");
        if(typeFirst){ typeFirst.textContent = "Property Type"; }
    }

    // Replace min/max price with one full-width text input
    let minPrice = document.querySelector('select[name="min-price"]');
    let maxPrice = document.querySelector('select[name="max-price"]');
    if(minPrice){
        minPrice.outerHTML = '<input type="text" name="price" placeholder="Price" style="color:#fff; width:100%; border-radius:30px; padding:10px;">';
    }
    if(maxPrice){
        maxPrice.remove(); // remove max price field completely
    }

    // Search button
    let searchBtn = document.querySelector('.ere-search-button');
    if(searchBtn){
        searchBtn.value = "Search";
    }

    //  Reorder fields: Property Location → Property Type → Price → Search
    let form = document.querySelector('.ere-search-form'); // wrapper form
    if(form && city && type){
        let priceInput = form.querySelector('input[name="price"]');
        if(priceInput && searchBtn){
            form.insertBefore(city.parentElement, type.parentElement);  // Move location before type
            form.insertBefore(priceInput, searchBtn.parentElement);     // Move price before search
        }
    }
});