$(document).on(
  "click",
  "ul.icons-system .dropdown-menu .dropdown-dont-close",
  function (e) {
    e.stopPropagation();
    return false;
  }
);

$(document).on("click", ".sq-accordion .item-head", function () {
  $(this)
    .closest(".sq-accordion")
    .children(".item.active")
    .removeClass("active");
  $(this).closest(".item").addClass("active");
});

$(document).on("show.bs.modal", ".modal", function () {
  var zIndex = 1040 + 10 * $(".modal:visible").length;
  $(this).css("z-index", zIndex);
  setTimeout(function () {
    $(".modal-backdrop")
      .not(".modal-stack")
      .css("z-index", zIndex - 1)
      .addClass("modal-stack");
  }, 0);
});

$(document).on("click", ".quant-tabs >.tabs-header > div", function () {
  window.dispatchEvent(new Event("resize"));
});

$(document).on("focus", ".sq-input input, .sq-input select", function () {
  if ($(this).closest(".sq-input-select-overlay").length > 0) {
    $(this).closest(".sq-input-select-overlay").addClass("sq-input-focused");
  }
  if ($(this).closest(".input-group").length > 0) {
    $(this).closest(".input-group").addClass("sq-input-focused");
  }
  showTooltipForInput(this);
});

$(document).on("blur", ".sq-input input, .sq-input select", function () {
  if ($(this).closest(".sq-input-select-overlay").length > 0) {
    $(this).closest(".sq-input-select-overlay").removeClass("sq-input-focused");
  }
  if ($(this).closest(".input-group").length > 0) {
    $(this).closest(".input-group").removeClass("sq-input-focused");
  }
  hideTooltipForInput();
});

$(document).on("mouseenter", ".sq-input input, .sq-input select", function () {
  if (
    $(".sq-input.sq-input-focused").length > 0 &&
    !$(this).closest(".sq-input").hasClass("sq-input-focused")
  ) {
    return;
  }
  showTooltipForInput(this);
});

function showTooltipForInput(thisElem) {
  if ($(thisElem).closest(".sq-input").find(".sq-tooltip").length <= 0) return;
  var thisTooltipContent = $(thisElem)
    .closest(".sq-input")
    .find(".sq-tooltip")
    .html();
  if (thisTooltipContent == "") return;
  var thisInputOffset = $(thisElem).offset();
  if ($(".sq-input-global-tooltip").length <= 0) {
    $("<div />").addClass("sq-input-global-tooltip").appendTo("body");
  }
  $(".sq-input-global-tooltip").html(thisTooltipContent);
  $(".sq-input-global-tooltip").show();
  var offsetBottom = $(window).height() - thisInputOffset.top + 10;
  var offsetLeft = thisInputOffset.left + 20;
  $(".sq-input-global-tooltip").css({
    position: "absolute",
    bottom: offsetBottom + "px",
    left: offsetLeft + "px",
  });
  $(".sq-input-global-tooltip").fadeIn(100);
}

$(document).on("wheel", function () {
  if (
    $(".sq-input-global-tooltip").length <= 0 ||
    $(".sq-input-global-tooltip").css("display") == "none"
  ) {
    return;
  }
  $(".sq-input-global-tooltip").hide();
});

function hideTooltipForInput() {
  if ($(".sq-input-global-tooltip").length > 0) {
    $(".sq-input-global-tooltip").fadeOut(100);
  }
}

$(document).on("mouseleave", ".sq-input input, .sq-input select", function () {
  if ($(".sq-input.sq-input-focused").length > 0) {
    return;
  }
  if ($(this).closest(".sq-input").hasClass("sq-input-focused")) {
    return;
  }
  hideTooltipForInput();
});

$(document).on("click", ".sq-help-ico", function (e) {
  e.stopPropagation();
  $(this).toggleClass("shown");
  if ($(this).hasClass("shown")) {
    rightPositionSqHelpIco(this);
  }
});

$(window).resize(function () {
  $(".sq-help-ico").each(function () {
    if ($(this).hasClass("shown")) {
      rightPositionSqHelpIco(this);
    }
    $(this).closest(".sq-fieldset").parent().addClass("has-help-container");
  });
});

$(document).on("click", function () {
  $(".sq-help-ico").removeClass("shown");
  $(".dropdown").removeClass("open");
});

$(document).on("click", ".sqn-databanks, .dropdown-menu", function () {
  $(".dropdown").removeClass("open");
});

function rightPositionSqHelpIco(thisElem) {
  var fieldsetParent = $(thisElem).closest(".sq-fieldset").parent();
  var fpWidth = fieldsetParent.width();
  var minAllowedWidth = 1100 + 300 + 45;
  if (fpWidth < minAllowedWidth) {
    $(thisElem).addClass("show-left");
  } else {
    $(thisElem).removeClass("show-left");
  }
}

$(document).on("focusin", "#passwordInput input", function () {
  console.log("x");
  this.oldValue = this.value;
  this.value = "";
});

$(document).on("focusout", "#passwordInput input", function () {
  if (this.value == "") {
    this.value = this.oldValue;
  }

  $(this).parent().addClass("sq-input-filled");
});

$(document).on(
  "click",
  ".sqn-settings-body, .modal, .sqn-databanks, #sq-notices, .split-pane-divider, .split-pane-divider-inner",
  function (e) {
    e.stopPropagation();
    $(".dropdown-parameters-settings.open").removeClass("open");
  }
);

$(document).on(
  "click",
  ".dropdown-parameters-settings .dropdown-menu",
  function (e) {
    e.stopPropagation();
  }
);

$(document).on("click", ".quant-tabs > .tabs-header div", function () {
  var itemName = $(this).data("item");
  if (itemName == "" || itemName == undefined) return;
  var $tabsContainer = $(this).parent().parent();
  if ($tabsContainer.children(".tabs-body").length <= 0) {
    $tabsContainer = $tabsContainer.parent().parent();
  }
  var $tabsBody = $tabsContainer.children(".tabs-body");
  $tabsContainer.children(".tabs-header div").removeClass("active");
  $tabsContainer
    .children(".tabs-header div[data-item=" + itemName + "]")
    .addClass("active");
  $tabsBody.children(".active").removeClass("active");
  $tabsBody.children("div[data-item=" + itemName + "]").addClass("active");
});

$(window).on("resize", function () {
  if (window.appConfig.product == "SQUANT") {
    return;
  }
  if ($(".quant-tabs >.tabs-header").length > 0) {
    tabsShowHiddenTabs();
  }
  var $iframe = $("#iframe_results_overlayed", window.parent.document);
  var newHeight = $(".split-pane-component-dashboard").height() - 51;
  if ($iframe.hasClass("active")) {
    if ($(".task-manager-menu").length > 0) {
      /* newHeight -= 30; */
      $iframe.addClass("iframe_results_overlayed_tasks");
    } else {
      $iframe.removeClass("iframe_results_overlayed_tasks");
    }
    $iframe.css("height", newHeight + "px");
  }
  var logHeight = newHeight - 700;
  $(".sqn-engine .log-scroll-content").css("height", logHeight + "px");
});

function displayNormalHeader(selector) {
  $(selector)
    .find(".tabs-header")
    .children("div")
    .each(function () {
      console.error(this);
      $(this).css("display", "block");
    });
}

function tabsShowHiddenTabs() {
  return;
  $(".quant-tabs").each(function () {
    if (!$(this).is(":visible")) {
      return;
    }
    $(this).find(".tabs-header").children("div").removeAttr("style");
    var tabsHeaderRect = $(this)
      .find(".tabs-header")[0]
      .getBoundingClientRect();
    var tabsHeaderRight = tabsHeaderRect.right;
    var tabsHeaderBottom =
      tabsHeaderRect.top + $(this).find(".tabs-header .active").outerHeight();
    var tabsHidden = [];
    var tabsHidden2 = [];
    $(this)
      .find(".tabs-header")
      .children("div")
      .each(function () {
        if (!$(this).hasClass("tabs-menu")) {
          var tabsItemRect = $(this)[0].getBoundingClientRect();
          var tabsItemTop = tabsItemRect.top;
          var tabsItemLeft = tabsItemRect.left;
          if (
            tabsItemTop > tabsHeaderBottom - 10 ||
            tabsItemLeft > tabsHeaderRight - 35
          ) {
            tabsHidden.push($(this)[0].outerHTML);
            tabsHidden2.push($(this));
          }
        }
      });
    for (var key in tabsHidden2) {
      tabsHidden2[key].hide();
    }
    $(this).find(".tabs-header").children(".tabs-menu").remove();
    if (tabsHidden.length > 0) {
      var $tabsMenu = $('<div class="tabs-menu">&#x25BC;</div>');
      var $tabsSubmenu = $('<div class="tabs-submenu"></div>');
      for (var itemKey in tabsHidden) {
        var itemItself = tabsHidden[itemKey];
        $tabsSubmenu.append($(itemItself));
      }
      $tabsMenu.append($tabsSubmenu);
      $(this).find(".tabs-header").append($tabsMenu);
    }
  });
}

$(document).on("click", ".disabled-area-placeholder", function (e) {
  e.preventDefault();
  e.stopPropagation();
  return false;
});

$(document).on(
  "click",
  ".settings-table-cross-check .simple-cs-line.category .categoryName",
  function (e) {
    $(this).parent().parent(".simple-cs-section").toggleClass("opened");
  }
);

history.pushState(null, document.title, location.href);
window.addEventListener("popstate", function (event) {
  history.pushState(null, document.title, location.href);
});
