jQuery(function($) {

  /* -----------------------------------------
  Preloader
  ----------------------------------------- */
  $('#preloader').delay(1000).fadeOut();
  $('#loader').delay(1000).fadeOut("slow");

  /* -----------------------------------------
  Navigation
  ----------------------------------------- */
  $('.menu-toggle').click(function() {
      $(this).toggleClass('open');
  });

  /* -----------------------------------------
  Keyboard Navigation
  ----------------------------------------- */
  $(window).on('load resize', property_dealer_navigation)

  function property_dealer_navigation(event) {
      if ($(window).width() < 1200) {
          $('.main-navigation').find("li").last().bind('keydown', function(e) {
              if (e.shiftKey && e.which === 9) {
                  if ($(this).hasClass('focus')) {
                  }

              } else if (e.which === 9) {
                  e.preventDefault();
                  $('#masthead').find('.menu-toggle').focus();
              }                
          })
      } else {
          $('.main-navigation').find("li").unbind('keydown')
      }
  }

  property_dealer_navigation()

  var property_dealer_primary_menu_toggle = $('#masthead .menu-toggle');
  property_dealer_primary_menu_toggle.on('keydown', function(e) {
      var tabKey = e.keyCode === 9;
      var shiftKey = e.shiftKey;

      if (property_dealer_primary_menu_toggle.hasClass('open')) {
          if (shiftKey && tabKey) {
              e.preventDefault();
              const $the_last_li = $('.main-navigation').find("li").last()
              $the_last_li.find('a').focus()
              if (!$the_last_li.hasClass('focus')) {

                  const $is_parent_on_top = true
                  let $the_parent_ul = $the_last_li.closest('ul.sub-menu')

                  let count = 0

                  while (!!$the_parent_ul.length) {
                      ++count

                      const $the_parent_li = $the_parent_ul.closest('li')

                      if (!!$the_parent_li.length) {
                          $the_parent_li.addClass('focus')
                          $the_parent_ul = $the_parent_li.closest('ul.sub-menu')

                          // Blur the cross
                          $(this).blur()
                          $the_last_li.addClass('focus')
                      }

                      if (!$the_parent_ul.length) {
                          break;
                      }
                  }

              }

          };
      }
  })

  // Determine if the document is RTL
  var isRtl = $('html').attr('dir') === 'rtl';

  /* -----------------------------------------
  Properties Section
  ----------------------------------------- */

  jQuery(document).ready(function($) {
    jQuery('.property-carousel').owlCarousel({
      loop: false,
      margin: 30,
      nav: false,
      rtl: false,
      dots: false,
      autoplay: true,
      responsive: {
        0: { items: 1, margin: 10, stagePadding: 5 },
        576: { items: 1, stagePadding: 5 },
        600: { items: 2, margin: 30, stagePadding: 5 },
        1000: { items: 3, stagePadding: 5 },
        1200: { items: 3, stagePadding: 5 }
      }
    });
  });

  /* -----------------------------------------
  Scroll Top
  ----------------------------------------- */
  var property_dealer_scrollToTopBtn = $('.property-dealer-scroll-to-top');

  $(window).scroll(function() {
      if ($(window).scrollTop() > 400) {
          property_dealer_scrollToTopBtn.addClass('show');
      } else {
          property_dealer_scrollToTopBtn.removeClass('show');
      }
  });

  property_dealer_scrollToTopBtn.on('click', function(e) {
      e.preventDefault();
      $('html, body').animate({
          scrollTop: 0
      }, '300');
  });

  // last two words
  jQuery(".banner-catption-wrapper .banner-caption-title a").html(function(){  
    var textArray = jQuery(this).text().trim().split(" ");
    
    if (textArray.length > 2) {
        var lastTwo = textArray.splice(-2).join(" "); 
        return textArray.join(" ") + ` <span class='banner-text'>${lastTwo}</span>`;
    } else {
        return `<span class='banner-text'>${textArray.join(" ")}</span>`;
    }
  });
    
});

document.addEventListener('DOMContentLoaded', function() {
    const property_dealer_header = document.querySelector('.sticky-header');
    if (property_dealer_header) { 
      window.addEventListener('scroll', function() {
        if (window.scrollY > 0) {
          property_dealer_header.classList.add('is-sticky');
        } else {
          property_dealer_header.classList.remove('is-sticky');
        }
      });
    }
  });

/* Progress Bar */
document.addEventListener("DOMContentLoaded", function () {
    const property_dealer_progressBar =
        document.getElementById("property_dealer_aster_progress_bar");
    if (!property_dealer_progressBar) return;
    window.addEventListener("scroll", function () {
        const property_dealer_scrollTop =
            document.documentElement.scrollTop || document.body.scrollTop;
        const property_dealer_height =
            document.documentElement.scrollHeight -
            document.documentElement.clientHeight;
        const property_dealer_scrolled =
            (property_dealer_scrollTop / property_dealer_height) * 100;
        property_dealer_progressBar.style.width =
            property_dealer_scrolled + "%";
    });
});
