(function ($) {
  "use strict";

  $.fn.newsAxisAccessibleDropDown = function () {
    var el = $(this);

    /* Make dropdown menus keyboard accessible */

    $("a", el)
      .focus(function () {
        $(this).parents("li").addClass("hover");
      })
      .blur(function () {
        $(this).parents("li").removeClass("hover");
      });
  };
  jQuery(document).ready(function ($) {
    $("#news-axis-menu").newsAxisAccessibleDropDown();
  }); // end document ready

  window.addEventListener(
    "scroll",
    function (event) {
      var top = this.scrollY;
      if (top >= 30) {
        document.body.classList.add("scrolling");
      } else {
        document.body.classList.remove("scrolling");
      }
    },
    false
  );

  let aghome = document.querySelector(".aghome");
  if (aghome) {
    aghome.classList.add("show-axbanner");
    aghome.previousElementSibling.classList.add("has-aghome");
  }

 // Post ticker function
    function initTicker() {
      // Check if ticker exists first
      const ticker = document.getElementById("ax-ticker");

      // Only proceed if ticker is found
      if (!ticker) {
        return; // Exit the function if ticker is not found
      }

      // Main ticker functionality
      function clonePosts() {
        const posts = ticker.getElementsByClassName("ax-post");

        // Check if posts exist
        if (posts && posts.length > 0) {
          try {
            const originalWidth = ticker.offsetWidth;
            ticker.style.transform = "none";

            // Check if the document is RTL
            const isRTL = document.documentElement.getAttribute('dir') === 'rtl';
            
            // Create animation based on text direction
            const keyframes = isRTL ? 
              `@keyframes scroll {
                  0% { transform: translateX(-${originalWidth / 1}px); }
                  100% { transform: translateX(0); }
              }` :
              `@keyframes scroll {
                  0% { transform: translateX(0); }
                  100% { transform: translateX(-${originalWidth / 1}px); }
              }`;

            // Remove existing animation if present
            const existingStyle = document.querySelector(
              "style[data-ticker-animation]"
            );
            if (existingStyle) {
              existingStyle.remove();
            }

            // Add new animation
            const styleSheet = document.createElement("style");
            styleSheet.setAttribute("data-ticker-animation", "true");
            styleSheet.textContent = keyframes;
            document.head.appendChild(styleSheet);
          } catch (error) {
            console.warn("Ticker animation setup error:", error);
          }
        }
      }

      // Initialize the ticker
      if (document.readyState === "loading") {
        document.addEventListener("DOMContentLoaded", clonePosts);
      } else {
        clonePosts();
      }

      // Handle resize
      let resizeTimeout;
      window.addEventListener("resize", () => {
        clearTimeout(resizeTimeout);
        resizeTimeout = setTimeout(clonePosts, 250);
      });
    }

    // Start the initialization
    initTicker();

})(jQuery);