// Menu
function charitable_trust_openNav() {
  jQuery(".sidenav").addClass('show');
}
function charitable_trust_closeNav() {
  jQuery(".sidenav").removeClass('show');
}

( function( window, document ) {
  function charitable_trust_keepFocusInMenu() {
    document.addEventListener( 'keydown', function( e ) {
      const charitable_trust_nav = document.querySelector( '.sidenav' );

      if ( ! charitable_trust_nav || ! charitable_trust_nav.classList.contains( 'show' ) ) {
        return;
      }
      const elements = [...charitable_trust_nav.querySelectorAll( 'input, a, button' )],
        charitable_trust_lastEl = elements[ elements.length - 1 ],
        charitable_trust_firstEl = elements[0],
        charitable_trust_activeEl = document.activeElement,
        tabKey = e.keyCode === 9,
        shiftKey = e.shiftKey;

      if ( ! shiftKey && tabKey && charitable_trust_lastEl === charitable_trust_activeEl ) {
        e.preventDefault();
        charitable_trust_firstEl.focus();
      }

      if ( shiftKey && tabKey && charitable_trust_firstEl === charitable_trust_activeEl ) {
        e.preventDefault();
        charitable_trust_lastEl.focus();
      }
    } );
  }
  charitable_trust_keepFocusInMenu();
} )( window, document );

document.addEventListener('DOMContentLoaded', function () {
  var preloader = document.getElementById('preloader');

  if (preloader) {  // Check if the element exists before trying to manipulate it
      var duration = 4000; // 10 seconds

      setTimeout(function () {
          preloader.style.display = 'none';
      }, duration);
  }
});

document.addEventListener('DOMContentLoaded', function () {
  // Get the header element
  var charitable_trust_header = document.getElementById('middle-header');

  // Get the initial offset of the header
  var charitable_trust_headerOffset = charitable_trust_header.offsetTop;

  // Function to handle scroll events
  function charitable_trust_handleScroll() {
      var charitable_trust_sticky_setting = jQuery('#middle-header').attr('data-sticky');
      if (window.pageYOffset > charitable_trust_headerOffset) {
          // Add the "sticky" class when scrolling down
          if(charitable_trust_sticky_setting == 1) {
            charitable_trust_header.classList.add('sticky');
          }
      } else {
          // Remove the "sticky" class when scrolling up
          if(charitable_trust_sticky_setting == 1) {
            charitable_trust_header.classList.remove('sticky');
          }
      }
  }
  // Attach the scroll event listener
  window.onscroll = charitable_trust_handleScroll;

});
/* Custom Cursor
 **-----------------------------------------------------*/
const charitable_trust_customCursor = {
  init: function () {
    this.charitable_trust_customCursor();
  },
  isVariableDefined: function (el) {
    return typeof el !== "undefined" && el !== null;
  },
  select: function (selectors) {
    return document.querySelector(selectors);
  },
  selectAll: function (selectors) {
    return document.querySelectorAll(selectors);
  },
  charitable_trust_customCursor: function () {
    const charitable_trust_cursorDot = this.select(".cursor-point");
    const charitable_trust_cursorOutline = this.select(".cursor-point-outline");
    if (this.isVariableDefined(charitable_trust_cursorDot) && this.isVariableDefined(charitable_trust_cursorOutline)) {
      const cursor = {
        delay: 8,
        _x: 0,
        _y: 0,
        endX: window.innerWidth / 2,
        endY: window.innerHeight / 2,
        cursorVisible: true,
        cursorEnlarged: false,
        $dot: charitable_trust_cursorDot,
        $outline: charitable_trust_cursorOutline,

        init: function () {
          this.dotSize = this.$dot.offsetWidth;
          this.outlineSize = this.$outline.offsetWidth;
          this.setupEventListeners();
          this.animateDotOutline();
        },

        updateCursor: function (e) {
          this.cursorVisible = true;
          this.toggleCursorVisibility();
          this.endX = e.clientX;
          this.endY = e.clientY;
          this.$dot.style.top = `${this.endY}px`;
          this.$dot.style.left = `${this.endX}px`;
        },

        setupEventListeners: function () {
          window.addEventListener("load", () => {
            this.cursorEnlarged = false;
            this.toggleCursorSize();
          });

          charitable_trust_customCursor.selectAll("a, button").forEach((el) => {
            el.addEventListener("mouseover", () => {
              this.cursorEnlarged = true;
              this.toggleCursorSize();
            });
            el.addEventListener("mouseout", () => {
              this.cursorEnlarged = false;
              this.toggleCursorSize();
            });
          });

          document.addEventListener("mousedown", () => {
            this.cursorEnlarged = true;
            this.toggleCursorSize();
          });
          document.addEventListener("mouseup", () => {
            this.cursorEnlarged = false;
            this.toggleCursorSize();
          });

          document.addEventListener("mousemove", (e) => {
            this.updateCursor(e);
          });

          document.addEventListener("mouseenter", () => {
            this.cursorVisible = true;
            this.toggleCursorVisibility();
            this.$dot.style.opacity = 1;
            this.$outline.style.opacity = 1;
          });

          document.addEventListener("mouseleave", () => {
            this.cursorVisible = false;
            this.toggleCursorVisibility();
            this.$dot.style.opacity = 0;
            this.$outline.style.opacity = 0;
          });
        },

        animateDotOutline: function () {
          this._x += (this.endX - this._x) / this.delay;
          this._y += (this.endY - this._y) / this.delay;
          this.$outline.style.top = `${this._y}px`;
          this.$outline.style.left = `${this._x}px`;

          requestAnimationFrame(this.animateDotOutline.bind(this));
        },

        toggleCursorSize: function () {
          if (this.cursorEnlarged) {
            this.$dot.style.transform = "translate(-50%, -50%) scale(0.75)";
            this.$outline.style.transform = "translate(-50%, -50%) scale(1.6)";
          } else {
            this.$dot.style.transform = "translate(-50%, -50%) scale(1)";
            this.$outline.style.transform = "translate(-50%, -50%) scale(1)";
          }
        },

        toggleCursorVisibility: function () {
          if (this.cursorVisible) {
            this.$dot.style.opacity = 1;
            this.$outline.style.opacity = 1;
          } else {
            this.$dot.style.opacity = 0;
            this.$outline.style.opacity = 0;
          }
        },
      };
      cursor.init();
    }
  },
};
charitable_trust_customCursor.init();

//scrollToTop

const charitable_trust_scrollToTop = {
  scrollToTop: {
    init() {
      this.button = document.getElementById("scrollToTopBtn");
      const svg = document.getElementById("progressCircle");
      this.circle = svg?.querySelector("circle");

      if (!this.button || !this.circle) return;

      this.radius = this.circle.r.baseVal.value;
      this.circumference = 2 * Math.PI * this.radius;

      this.circle.style.strokeDasharray = `${this.circumference}`;
      this.circle.style.strokeDashoffset = this.circumference;

      window.addEventListener("scroll", this.handleScroll.bind(this));
      this.button.addEventListener("click", this.scrollToTop.bind(this));
    },

    handleScroll() {
      const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

      this.button.style.display = scrollTop > 100 ? "flex" : "none";
      requestAnimationFrame(this.updateProgress.bind(this));
    },

    scrollToTop() {
      window.scrollTo({ top: 0, behavior: "smooth" });
    },

    updateProgress() {
      if (!this.circle) return;

      const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
      const scrollHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight;
      const progress = scrollTop / scrollHeight;

      const offset = this.circumference * (1 - progress);
      this.circle.style.strokeDashoffset = offset;
    }
  }
};
document.addEventListener("DOMContentLoaded", () => charitable_trust_scrollToTop .scrollToTop.init());