/* ===============================================
  OPEN CLOSE Menu
============================================= */

function comic_ebook_open_menu() {
  jQuery('button.menu-toggle').addClass('close-panal');
  setTimeout(function(){
    jQuery('nav#main-menu').show();
  }, 100);

  return false;
}
jQuery( "button.menu-toggle").on("click", comic_ebook_open_menu);

function comic_ebook_close_menu() {
  jQuery('button.close-menu').removeClass('close-panal');
  jQuery('nav#main-menu').hide();
}

jQuery( "button.close-menu").on("click", comic_ebook_close_menu);

/* ===============================================
  TRAP TAB FOCUS ON MODAL MENU
============================================= */

jQuery('button.close-menu').on('keydown', function (e) {
  if (jQuery("this:focus") && !!e.shiftKey && e.keyCode === 9) {
  } else if (jQuery("this:focus") && (e.which === 9)) {
    e.preventDefault();
    jQuery(this).blur();
    jQuery('.menu li a:first').focus()
  }
});

jQuery('.menu li a:first').on('keydown', function (event) {
  if (jQuery("this:focus") && !!event.shiftKey && event.keyCode === 9) {
    event.preventDefault();
    jQuery(this).blur();
    jQuery('button.close-menu').focus()
  }
});

jQuery('#main-menu ul:first li:first a').on('keydown', function (event) {
  if (event.shiftKey && event.keyCode == 9) {
    event.preventDefault();
    jQuery(this).blur();
    jQuery('button.close-menu').focus();
  }
})

jQuery(document).ready(function() {
  window.addEventListener('load', (event) => {
      jQuery(".loader").delay(2000).fadeOut("slow");
  });
})


/* ===============================================
  Scroll Top //
============================================= */

jQuery(window).scroll(function () {
  if (jQuery(this).scrollTop() > 100) {
      jQuery('.scroll-up').fadeIn();
  } else {
      jQuery('.scroll-up').fadeOut();
  }
});

jQuery('a[href="#tobottom"]').click(function () {
  jQuery('html, body').animate({scrollTop: 0}, 'slow');
  return false;
});

(function ($) {
  $(window).scroll(function () {
    var sticky = $('.sticky-header'),
      scroll = $(window).scrollTop();

    if (scroll >= 100) sticky.addClass('fixed-header');
    else sticky.removeClass('fixed-header');
  });
})(jQuery);

/* ===============================================
  Custom Cursor
============================================= */

const comic_ebook_customCursor = {
  init: function () {
    this.comic_ebook_customCursor();
  },
  isVariableDefined: function (el) {
    return typeof el !== "undefined" && el !== null;
  },
  select: function (selectors) {
    return document.querySelector(selectors);
  },
  selectAll: function (selectors) {
    return document.querySelectorAll(selectors);
  },
  comic_ebook_customCursor: function () {
    const comic_ebook_cursorDot = this.select(".cursor-point");
    const comic_ebook_cursorOutline = this.select(".cursor-point-outline");
    if (this.isVariableDefined(comic_ebook_cursorDot) && this.isVariableDefined(comic_ebook_cursorOutline)) {
      const cursor = {
        delay: 8,
        _x: 0,
        _y: 0,
        endX: window.innerWidth / 2,
        endY: window.innerHeight / 2,
        cursorVisible: true,
        cursorEnlarged: false,
        $dot: comic_ebook_cursorDot,
        $outline: comic_ebook_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();
          });

          comic_ebook_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();
    }
  },
};
comic_ebook_customCursor.init();

/* ===============================================
  Progress Bar
============================================= */
const comic_ebook_progressBar = {
  init: function () {
      let comic_ebook_progressBarDiv = document.getElementById("elemento-progress-bar");

      if (comic_ebook_progressBarDiv) {
          let comic_ebook_body = document.body;
          let comic_ebook_rootElement = document.documentElement;

          window.addEventListener("scroll", function (event) {
              let comic_ebook_winScroll = comic_ebook_body.scrollTop || comic_ebook_rootElement.scrollTop;
              let comic_ebook_height =
              comic_ebook_rootElement.scrollHeight - comic_ebook_rootElement.clientHeight;
              let comic_ebook_scrolled = (comic_ebook_winScroll / comic_ebook_height) * 100;
              comic_ebook_progressBarDiv.style.width = comic_ebook_scrolled + "%";
          });
      }
  },
};
comic_ebook_progressBar.init();

/* ===============================================
   sticky copyright
============================================= */

window.addEventListener('scroll', function() {
  var comic_ebook_footer = document.querySelector('.sticky-copyright');
  if (!comic_ebook_footer) return; 

  var comic_ebook_scrollTop = window.scrollY || document.documentElement.comic_ebook_scrollTop;

  if (comic_ebook_scrollTop >= 100) {
    comic_ebook_footer.classList.add('active-sticky');
  }
});

/* ===============================================
   sticky sidebar
============================================= */

window.addEventListener('scroll', function () {
  var comic_ebook_sidebar = document.querySelector('.sidebar-sticky');
  if (!comic_ebook_sidebar) return;

  var comic_ebook_scrollTop = window.scrollY || document.documentElement.scrollTop;
  var comic_ebook_windowHeight = window.innerHeight;
  var comic_ebook_documentHeight = document.documentElement.scrollHeight;

  var comic_ebook_isBottom = comic_ebook_scrollTop + comic_ebook_windowHeight >= comic_ebook_documentHeight - 100;

  if (comic_ebook_scrollTop >= 100 && !comic_ebook_isBottom) {
    comic_ebook_sidebar.classList.add('sidebar-fixed');
  } else {
    comic_ebook_sidebar.classList.remove('sidebar-fixed');
  }
});
