var $ = jQuery;

///////////// SEARCH BAR

$("#searchHeaderForm").on("click", function () {
  $("#SearchsContainerBack").fadeIn();
  $("#SearchsContainer").addClass("active");
});
$("#resSearchIcon").on("click", function () {
  $("#SearchsContainerBack").fadeIn();
  $("#SearchsContainer").addClass("active");
});

$("#closeSea").on("click", function () {
  setTimeout(function () {
    $("#SearchsContainerBack").fadeOut(300);
  }, 500);
  $("#SearchsContainer").removeClass("active");
});

///////////// MOBILE MENU

$("#hambergerMenu").click(function () {
  $("#SearchsContainerBack").fadeIn();
  $(".menuMobilesection").addClass("active");
});

$(".iconClose").click(function () {
  setTimeout(function () {
    $("#SearchsContainerBack").fadeOut(300);
  }, 500);
  $(".menuMobilesection").removeClass("active");
});

let svgCodeMenuItem =
  '<svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M9.5 4.50015L6 7.50015L2.5 4.50015" stroke="white" stroke-linecap="round" stroke-linejoin="round"/></svg>';
$(
  ".menuHeader .menu-menu-primary-container > ul > .menu-item-has-children > a"
).after(svgCodeMenuItem);

let svgCode = `
<svg width="20" height="8" viewBox="0 0 20 8" fill="none" xmlns="http://www.w3.org/2000/svg">
  <path d="M1 4L19 4" stroke="#F7BE00" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
  <path d="M4 1L1 4L4 7" stroke="#F7BE00" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
</svg>`;

$(".menuSideBarMenumobile ul#menu-menu-mobile a").each(function () {
  if ($(this).siblings("ul").length > 0) {
    $(this).after(svgCode);
  }
});

$(".menuSideBarMenumobile").on("click", "svg", function (e) {
  e.preventDefault();

  let $svg = $(this);
  let $submenu = $svg.siblings("ul");

  $svg.closest("ul").find("> li > ul").slideUp(300);
  $svg.closest("ul").find("> li > svg").css("transform", "rotate(360deg)");

  if ($submenu.is(":visible")) {
    $submenu.slideUp(300);
    $svg.css("transform", "rotate(360deg)");
  } else {
    $submenu.slideDown(300);
    $svg.css("transform", "rotate(270deg)");
  }
});

///////////// ّFOOTER

function footerAccordion() {
  if ($(window).width() < 768) {
    $(".footerItemsTitle")
      .off("click")
      .on("click", function (e) {
        const $this = $(this);
        const $nextUl = $this.next("ul");

        if ($nextUl.is(":visible")) {
          $nextUl.slideUp();
          $this.removeClass("active");
        } else {
          $(".footerItems ul:visible").slideUp();
          $(".footerItemsTitle").removeClass("active");

          $nextUl.slideDown();
          $this.addClass("active");
        }
      });

    $(".footerItems ul").hide();
  } else {
    $(".footerItems ul").show();
    $(".footerItemsTitle").off("click").removeClass("active");
  }
}

$(document).ready(footerAccordion);
$(window).resize(footerAccordion);
