(function ($) {
  $(window).on("elementor/frontend/init", function () {
    elementorFrontend.hooks.addAction(
      "frontend/element_ready/eventful-event-grid-widget.default",
      function (scope, $) {        
        var efpe_myScript = function () {
          if ($(".ta-container").length > 0) {
            $(scope)
              .find(".ta-container")
              .each(function () {
                var efpe_container = $(this),
                  efpe_container_id = efpe_container.attr("id"),
                  EFPE_Wrapper_ID = "#" + efpe_container_id,
                  pc_sid = $(EFPE_Wrapper_ID).data("sid"),
                  pc_pagination = $(EFPE_Wrapper_ID).data("pagination"),
                  post_per_page = $(EFPE_Wrapper_ID).data("post_per_page"),
                  efpe_settings = $(EFPE_Wrapper_ID).data("efpe_settings"),
                  efpe_post_limit = $(EFPE_Wrapper_ID).data("efpe_post_limit"),
                  efpeAccordion = $("#" + efpe_container_id + " .ta-collapse"),
                  ajaxurl = ta_efpe.ajaxurl,
                  nonce = ta_efpe.nonce;
                $(
                  ".ta-overlay.efpe-post,.ta-content-box.efpe-post",
                  EFPE_Wrapper_ID
                ).on("mouseover", function () {
                  $(this)
                    .find(".eventful__item__content.animated:not(.efpe_hover)")
                    .addClass("efpe_hover");
                });

                function efpe_item_same_height() {
                  var maxHeight = 0;
                  $(EFPE_Wrapper_ID + ".efpe_same_height .item").each(
                    function () {
                      if ($(this).find(".efpe-post").height() > maxHeight) {
                        maxHeight = $(this).find(".efpe-post").height();
                      }
                    }
                  );
                  $(EFPE_Wrapper_ID + ".efpe_same_height .efpe-post").height(
                    maxHeight
                  );
                }
                if (
                  $(EFPE_Wrapper_ID + ".efpe_same_height").hasClass(
                    "efpe-filter-wrapper"
                  )
                ) {
                  efpe_item_same_height();
                }

                // Ajax Action for Live filter.
                var keyword = "",
                  orderby = "",
                  taxonomy = "",
                  order = "",
                  term_id = "",
                  page = "",
                  spsp_lang = $(EFPE_Wrapper_ID).data("lang");
                var author_id = "",
                  custom_field_key = "",
                  custom_field_value = "",
                  efpe_hash_url = Array(),
                  efpe_last_filter = "",
                  custom_fields_array = Array(),
                  is_pagination_url_change = true;
                function efpe_ajax_action(selected_term_list = null) {
                  jQuery.ajax({
                    url: ajaxurl,
                    type: "POST",
                    data: {
                      page: page,
                      id: pc_sid,
                      pagination_type: pc_pagination,
                      post_per_page: post_per_page,
                      efpe_settings: efpe_settings,
                      efpe_post_limit: efpe_post_limit,
                      action: "eventful_for_elementor_post_order",
                      author_id: author_id,
                      lang: spsp_lang,
                      order: order,
                      keyword: keyword,
                      orderby: orderby,
                      taxonomy: taxonomy,
                      term_id: term_id,
                      nonce: nonce,
                      term_list: selected_term_list,
                      custom_fields_array: custom_fields_array,
                    },
                    success: function (data) {
                      var $data = $(data);
                      var $newElements = $data;

                      if ($(EFPE_Wrapper_ID).hasClass("efpe-masonry")) {
                        var $post_wrapper = $(".ta-row", EFPE_Wrapper_ID);
                        $post_wrapper.masonry("destroy");
                        $post_wrapper
                          .html($newElements)
                          .imagesLoaded(function () {
                            $post_wrapper.masonry();
                          });
                      } else if (
                        $(EFPE_Wrapper_ID).hasClass("efpe-filter-wrapper")
                      ) {
                        $(
                          ".ta-row, .efpe-timeline-grid, .ta-efpe-collapse, .table-responsive tbody",
                          EFPE_Wrapper_ID
                        ).html($newElements);
                        $grid
                          .append($newElements)
                          .isotope("appended", $newElements)
                          .imagesLoaded(function () {
                            $grid.isotope("layout");
                          });
                        efpe_item_same_height();
                      } else {
                        var $newElements = $data.css({
                          opacity: 0,
                        });
                        $(
                          ".ta-row, .efpe-timeline-grid, .ta-efpe-collapse, .table-responsive tbody",
                          EFPE_Wrapper_ID
                        ).html($newElements);
                        if (efpeAccordion.length > 0) {
                          efpeAccordion.accordion("refresh");
                          if (accordion_mode === "multi-open") {
                            efpeAccordion
                              .find(".efpe-collapse-header")
                              .next()
                              .slideDown();
                            efpeAccordion
                              .find(".efpe-collapse-header .fa")
                              .removeClass("fa-plus")
                              .addClass("fa-minus");
                          }
                        }
                        var $newElements = $data.css({
                          opacity: 1,
                        });
                      }
                      efpe_lazyload();
                    },
                  });
                }

                // Pagination.
                function efpe_pagination_action(selected_term_list = null) {
                  var LoadMoreText = $(
                    ".ta-efpe-pagination-data",
                    EFPE_Wrapper_ID
                  ).data("loadmoretext");
                  var EndingMessage = $(
                    ".ta-efpe-pagination-data",
                    EFPE_Wrapper_ID
                  ).data("endingtext");
                  jQuery.ajax({
                    url: ajaxurl,
                    type: "POST",
                    data: {
                      page: page,
                      id: pc_sid,
                      pagination_type: pc_pagination,
                      post_per_page: post_per_page,
                      efpe_settings: efpe_settings,
                      efpe_post_limit: efpe_post_limit,
                      action: "post_pagination_bar",
                      author_id: author_id,
                      order: order,
                      lang: spsp_lang,
                      keyword: keyword,
                      orderby: orderby,
                      taxonomy: taxonomy,
                      term_id: term_id,
                      term_list: selected_term_list,
                      custom_fields_array: custom_fields_array,
                      nonce: nonce,
                    },

                    success: function (data) {
                      var $data = $(data);
                      var $newElements = $data;
                      $(
                        ".efpe-post-pagination.efpe-on-desktop:not(.no_ajax)",
                        EFPE_Wrapper_ID
                      ).html($newElements);
                      if (
                        Pagination_Type == "infinite_scroll" ||
                        Pagination_Type == "ajax_load_more"
                      ) {
                        $(".efpe-load-more", EFPE_Wrapper_ID)
                          .removeClass("finished")
                          .removeClass("efpe-hide")
                          .html(
                            '<button efpe-processing="0">' +
                              LoadMoreText +
                              "</button>"
                          );
                      }
                      if (Pagination_Type == "infinite_scroll") {
                        $(".efpe-load-more", EFPE_Wrapper_ID).addClass(
                          "efpe-hide"
                        );
                      }
                      if (
                        !$(".efpe-post-pagination a", EFPE_Wrapper_ID).length
                      ) {
                        $(".efpe-load-more", EFPE_Wrapper_ID)
                          .show()
                          .html(EndingMessage);
                      }
                      efpe_lazyload();
                    },
                    error: function (jqXHR, exception) {
                      var msg = "";
                      if (jqXHR.status === 0) {
                        msg = "Not connect.\n Verify Network.";
                      } else if (jqXHR.status == 404) {
                        msg = "Requested page not found. [404]";
                      } else if (jqXHR.status == 500) {
                        msg = "Internal Server Error [500].";
                      } else if (exception === "parsererror") {
                        msg = "Requested JSON parse failed.";
                      } else if (exception === "timeout") {
                        msg = "Time out error.";
                      } else if (exception === "abort") {
                        msg = "Ajax request aborted.";
                      } else {
                        msg = "Uncaught Error.\n" + jqXHR.responseText;
                      }
                      $("#post").html(msg);
                    },
                  });
                }

                // Live filter button reset on ajax call.
                function efpe_live_filter_reset(selected_term_list = null) {
                  jQuery.ajax({
                    url: ajaxurl,
                    type: "POST",
                    data: {
                      page: page,
                      id: pc_sid,
                      pagination_type: pc_pagination,
                      post_per_page: post_per_page,
                      efpe_settings: efpe_settings,
                      efpe_post_limit: efpe_post_limit,
                      action: "efpe_live_filter_reset",
                      author_id: author_id,
                      order: order,
                      lang: spsp_lang,
                      keyword: keyword,
                      orderby: orderby,
                      taxonomy: taxonomy,
                      term_id: term_id,
                      term_list: selected_term_list,
                      last_filter: efpe_last_filter,
                      custom_fields_array: custom_fields_array,
                      nonce: nonce,
                    },
                    success: function (data) {
                      var $data = $(data);
                      var $newElements = $data.animate({
                        opacity: 0.5,
                      });
                      $(".efpe-filter-bar", EFPE_Wrapper_ID).html($newElements);
                      custom_field_filter_slider();
                      $newElements.animate({
                        opacity: 1,
                      });
                    },
                  });
                }
                // Update Hash url array.
                function efpe_hash_update_arr(
                  efpe_filter_keyword,
                  filter_arr,
                  key
                ) {
                  if (efpe_hash_url.length > 0) {
                    efpe_hash_url.forEach(function (row) {
                      if (
                        $.inArray(efpe_filter_keyword, row.efpe_filter_keyword)
                      ) {
                        row[key] = efpe_filter_keyword;
                      } else {
                        efpe_hash_url.push(filter_arr);
                      }
                    });
                  } else {
                    efpe_hash_url.push(filter_arr);
                  }
                  return efpe_hash_url;
                }
                // On normal pagination go to current shortcode.
                var url_hash = window.location.search;
                if (url_hash.indexOf("paged") >= 0) {
                  var s_id = /paged(\w+)/.exec(url_hash)[1];
                  var spscurrent_id = document.getElementById(
                    "efpe_wrapper-" + s_id
                  );
                  spscurrent_id.scrollIntoView();
                }
                // Update url.
                var selected_term_list = Array();
                function efpe_update_url() {
                  var p_search = window.location.search;
                  if (p_search.indexOf("page_id") >= 0) {
                    var efpe_page = /page_id\=(\d+)/.exec(p_search)[1];
                    var efpe_url = "?page_id=" + efpe_page + "&";
                  } else {
                    var efpe_url = "&";
                  }
                  if (efpe_hash_url.length > 0) {
                    $.map(efpe_hash_url, function (value, index) {
                      $.map(value, function (value2, index2) {
                        if (
                          value2 == "all" ||
                          value2 == "none" ||
                          value2 == "" ||
                          value2 == "page"
                        ) {
                          efpe_url += "";
                        } else {
                          efpe_url += "efpe_" + index2 + "=" + value2 + "&";
                        }
                      });
                    });
                  }
                  if (selected_term_list.length > 0) {
                    var term_total_length = selected_term_list.length;
                    $.map(selected_term_list, function (value, index) {
                      if (value.term_id == "all" || value.term_id == "") {
                        efpe_url += "";
                      } else {
                        if (index == term_total_length - 1) {
                          efpe_url +=
                            "tx_" + value.taxonomy + "=" + value.term_id + "";
                        } else {
                          efpe_url +=
                            "tx_" + value.taxonomy + "=" + value.term_id + "&";
                        }
                      }
                    });
                  }
                  if (custom_fields_array.length > 0) {
                    var meta_field_total_length = custom_fields_array.length;
                    $.map(custom_fields_array, function (value, index) {
                      //  if (index == meta_field_total_length - 1) {
                      efpe_url +=
                        "cf" +
                        value.custom_field_key +
                        "=" +
                        value.custom_field_value +
                        "&";
                      // }
                    });
                  }
                  if (
                    efpe_hash_url.length < 0 ||
                    selected_term_list.length < 0
                  ) {
                    efpe_url = "";
                  }
                  if (efpe_url.length > 1) {
                    var slf = "";
                    if (efpe_last_filter.length > 0) {
                      var slf = "&slf=" + efpe_last_filter;
                    }

                    efpe_url = "?efpe=" + pc_sid + slf + efpe_url;

                    history.pushState(null, null, encodeURI(efpe_url));
                  } else {
                    var uri = window.location.toString();
                    if (uri.indexOf("?") > 0) {
                      var clean_uri = uri.substring(0, uri.indexOf("?"));
                      window.history.replaceState(
                        {},
                        document.title,
                        clean_uri
                      );
                    }
                  }
                }

                function custom_field_filter_slider() {
                  $(EFPE_Wrapper_ID + " .efpe-custom-field-filter-slider").each(
                    function () {
                      var _that = $(this);
                      var _input = _that.find(".efpe-input");
                      var custom_field_key = _input.attr("name");
                      var _min = _input.data("min");
                      var _crmin = _input.data("crmin");
                      var _crmax = _input.data("crmax");
                      var _max = _input.data("max");
                      _that.find(".efpe-slider").slider({
                        range: true,
                        min: _crmin,
                        max: _crmax,
                        values: [_min, _max],
                        slide: function (event, ui) {
                          _input.val(ui.values[0] + " - " + ui.values[1]);
                        },
                        stop: function (event, ui) {
                          _input.data("max", ui.values[1]);

                          custom_field_value =
                            ui.values[0] + " " + ui.values[1];
                          custom_fields_array.push({
                            custom_field_key,
                            custom_field_value,
                          });
                          custom_fields_array.map(function (person) {
                            if (person.custom_field_key === custom_field_key) {
                              person.custom_field_value = custom_field_value;
                            }
                          });
                          custom_fields_array = $.grep(
                            custom_fields_array,
                            function (e, i) {
                              return e.custom_field_value.length;
                            }
                          );
                          custom_fields_array = custom_fields_array
                            .map(JSON.stringify)
                            .reverse() // convert to JSON string the array content, then reverse it (to check from end to beginning)
                            .filter(function (item, index, arr) {
                              return arr.indexOf(item, index + 1) === -1;
                            }) // check if there is any occurrence of the item in whole array
                            .reverse()
                            .map(JSON.parse);
                          efpe_update_url();
                          efpe_last_filter = custom_field_key;
                          efpe_ajax_action(selected_term_list);
                          efpe_live_filter_reset(selected_term_list);
                        },
                      });
                    }
                  );
                }
                custom_field_filter_slider();
                // Ajax post search.
                $("input.efpe-search-field", EFPE_Wrapper_ID).on(
                  "keyup",
                  function () {
                    var that = $(this);
                    keyword = that.val();
                    efpe_last_filter = "keyword";
                    var efpe_search_arr = { keyword, keyword };
                    efpe_live_filter_reset(selected_term_list);
                    efpe_hash_update_arr(keyword, efpe_search_arr, "keyword");
                    efpe_update_url();
                    efpe_ajax_action(selected_term_list);
                    efpe_pagination_action();
                    is_pagination_url_change = false;
                    efpe_hash_update_arr("page", { page: "" }, "page");
                    efpe_update_url();
                  }
                );

                // Event order by.
                $(".efpe-order-by", EFPE_Wrapper_ID).on("change", function () {
                  var that;
                  $(this)
                    .find("option:selected, input:radio:checked")
                    .each(function () {
                      that = $(this);
                      orderby = that.val();
                    });
                  var orerbyarr = { orderby, orderby };
                  efpe_hash_update_arr(orderby, orerbyarr, "orderby");
                  efpe_update_url();
                  efpe_ajax_action();
                  efpe_pagination_action();
                  efpe_hash_update_arr("page", { page: "" }, "page");
                  efpe_update_url();
                });

                function efpe_filter_push(myarr, item) {
                  var found = false;
                  var i = 0;
                  while (i < myarr.length) {
                    if (myarr[i] === item) {
                      // Do the logic (delete or replace)
                      found = true;
                      break;
                    }
                    i++;
                  }
                  // Add the item
                  if (!found) myarr.push(item);
                  return myarr;
                }

                // Pre Filter Init.
                var tax_list = Array();
                $(".efpe-filter-by", EFPE_Wrapper_ID)
                  .find("option:selected, input:radio:checked")
                  .each(function () {
                    term_id = $(this).val();
                    taxonomy = $(this).data("taxonomy");
                    var selected_tax_length = selected_term_list.length;
                    if (selected_tax_length > 0) {
                      var selected_tax =
                        selected_term_list[selected_tax_length - 1]["taxonomy"];
                      selected_term_list.map(function (person) {
                        if (person.taxonomy === taxonomy) {
                          person.term_id = term_id;
                        }
                      });
                      // if ($.inArray(taxonomy, tax_list) == -1) {
                      selected_term_list.push({
                        taxonomy,
                        term_id,
                      });
                      //  }
                      if (
                        selected_term_list[selected_tax_length - 1][
                          "term_id"
                        ] == "all" &&
                        selected_tax === taxonomy
                      ) {
                        tax_list = tax_list.filter(function (val) {
                          return val !== taxonomy;
                        });
                      } else {
                        tax_list = efpe_filter_push(tax_list, taxonomy);
                      }
                      selected_term_list = $.grep(
                        selected_term_list,
                        function (e, i) {
                          return e.term_id != "all";
                        }
                      );
                    } else {
                      selected_term_list.push({
                        taxonomy,
                        term_id,
                      });
                      selected_term_list = $.grep(
                        selected_term_list,
                        function (e, i) {
                          return e.term_id != "all";
                        }
                      );
                      tax_list = Array(taxonomy);
                    }
                  });
                $(".efpe-author-filter", EFPE_Wrapper_ID)
                  .find("option:selected, input:radio:checked")
                  .each(function () {
                    that = $(this);
                    author_id = that.val();
                  });
                $(".efpe-order", EFPE_Wrapper_ID)
                  .find("option:selected, input:radio:checked")
                  .each(function () {
                    that = $(this);
                    order = $(this).val();
                  });
                $(".efpe-order-by", EFPE_Wrapper_ID)
                  .find("option:selected, input:radio:checked")
                  .each(function () {
                    that = $(this);
                    orderby = that.val();
                  });
                $("input.efpe-search-field", EFPE_Wrapper_ID).each(function () {
                  var that = $(this);
                  keyword = that.val();
                });
                $(".efpe-filter-by-checkbox", EFPE_Wrapper_ID).each(
                  function () {
                    var current_tax = $(this).data("taxonomy");
                    var term_ids = "";
                    $(this)
                      .find(
                        "input[name='" + current_tax + "']:checkbox:checked"
                      )
                      .each(function () {
                        term_ids += $(this).val() + ",";
                        taxonomy = $(this).data("taxonomy");
                      });
                    term_id = term_ids.replace(/,(?=\s*$)/, "");
                    selected_term_list.map(function (person) {
                      if (person.taxonomy === current_tax) {
                        person.term_id = term_id;
                      }
                    });
                    selected_term_list.push({
                      taxonomy,
                      term_id,
                    });
                  }
                );
                selected_term_list = $.grep(
                  selected_term_list,
                  function (e, i) {
                    return e.term_id.length;
                  }
                );
                // Custom field filter.
                $(".efpe-custom-field-filter", EFPE_Wrapper_ID).each(
                  function () {
                    $(this)
                      .find("option:selected, input:radio:checked")
                      .each(function () {
                        custom_field_key = $(this).attr("name");
                        custom_field_value = $(this).val();
                        custom_fields_array.map(function (person) {
                          if (person.custom_field_key === custom_field_key) {
                            person.custom_field_value = custom_field_value;
                          }
                        });
                        custom_fields_array.push({
                          custom_field_key,
                          custom_field_value,
                        });
                      });
                  }
                );
                selected_term_list = selected_term_list
                  .map(JSON.stringify)
                  .reverse() // convert to JSON string the array content, then reverse it (to check from end to beginning)
                  .filter(function (item, index, arr) {
                    return arr.indexOf(item, index + 1) === -1;
                  }) // check if there is any occurence of the item in whole array
                  .reverse()
                  .map(JSON.parse);
                // Filter by checkbox.
                $(EFPE_Wrapper_ID).on(
                  "change",
                  ".efpe-filter-by-checkbox",
                  function (e) {
                    e.stopPropagation();
                    e.preventDefault();
                    $(".efpe-filter-by-checkbox", EFPE_Wrapper_ID).each(
                      function () {
                        var current_tax = $(this).data("taxonomy");
                        var term_ids = "";
                        $(this)
                          .find(
                            "input[name='" + current_tax + "']:checkbox:checked"
                          )
                          .each(function () {
                            term_ids += $(this).val() + ",";
                            taxonomy = $(this).data("taxonomy");
                          });
                        term_id = term_ids.replace(/,(?=\s*$)/, "");
                        selected_term_list.map(function (person) {
                          if (person.taxonomy === current_tax) {
                            person.term_id = term_id;
                          }
                        });
                        selected_term_list.push({
                          taxonomy,
                          term_id,
                        });
                      }
                    );
                    selected_term_list = $.grep(
                      selected_term_list,
                      function (e, i) {
                        return e.term_id.length;
                      }
                    );
                    selected_term_list = selected_term_list
                      .map(JSON.stringify)
                      .reverse() // convert to JSON string the array content, then reverse it (to check from end to beginning)
                      .filter(function (item, index, arr) {
                        return arr.indexOf(item, index + 1) === -1;
                      }) // check if there is any occurence of the item in whole array
                      .reverse()
                      .map(JSON.parse);
                    var term_ids = "";
                    $(this)
                      .find("input:checkbox:checked")
                      .each(function () {
                        term_ids += $(this).val() + ",";
                        taxonomy = $(this).data("taxonomy");
                      });
                    taxonomy = $(this).data("taxonomy");
                    term_id = term_ids.replace(/,(?=\s*$)/, "");
                    if (term_id.length > 0) {
                      efpe_last_filter = taxonomy;
                    } else {
                      efpe_last_filter = efpe_last_filter;
                    }
                    efpe_hash_update_arr("page", { page: "" }, "page");
                    efpe_update_url();
                    efpe_live_filter_reset(selected_term_list);
                    efpe_ajax_action(selected_term_list);
                    efpe_pagination_action(selected_term_list);
                  }
                );

                // Filter by custom field.
                $(EFPE_Wrapper_ID).on(
                  "change",
                  ".efpe-custom-field-filter",
                  function (e) {
                    e.stopPropagation();
                    e.preventDefault();
                    $(".efpe-custom-field-filter", EFPE_Wrapper_ID).each(
                      function () {
                        $(this)
                          .find("option:selected, input:radio:checked")
                          .each(function () {
                            custom_field_key = $(this).attr("name");
                            custom_field_value = $(this).val();
                            custom_fields_array.map(function (person) {
                              if (
                                person.custom_field_key === custom_field_key
                              ) {
                                person.custom_field_value = custom_field_value;
                              }
                            });
                            custom_fields_array.push({
                              custom_field_key,
                              custom_field_value,
                            });
                          });
                      }
                    );
                    efpe_last_filter = $(this)
                      .find("option:selected, input:radio:checked")
                      .attr("name");
                    custom_fields_array = $.grep(
                      custom_fields_array,
                      function (e, i) {
                        return e.custom_field_value != "all";
                      }
                    );
                    custom_fields_array = custom_fields_array
                      .map(JSON.stringify)
                      .reverse() // convert to JSON string the array content, then reverse it (to check from end to beginning)
                      .filter(function (item, index, arr) {
                        return arr.indexOf(item, index + 1) === -1;
                      }) // check if there is any occurence of the item in whole array
                      .reverse()
                      .map(JSON.parse);
                    efpe_ajax_action(selected_term_list);
                    efpe_pagination_action(selected_term_list);
                    efpe_live_filter_reset(selected_term_list);
                    efpe_update_url();
                  }
                );
                // Filter by checkbox custom field.
                $(EFPE_Wrapper_ID).on(
                  "change",
                  ".efpe-custom-field-filter-checkbox",
                  function (e) {
                    e.stopPropagation();
                    e.preventDefault();
                    $(
                      ".efpe-custom-field-filter-checkbox",
                      EFPE_Wrapper_ID
                    ).each(function () {
                      var custom_field_key = $(this)
                        .find("input:checkbox")
                        .attr("name");
                      var custom_field_value = "";
                      $(this)
                        .find(
                          "input[name='" +
                            custom_field_key +
                            "']:checkbox:checked"
                        )
                        .each(function () {
                          custom_field_key = $(this).attr("name");
                          custom_field_value += $(this).val() + ",";
                        });
                      custom_field_value = custom_field_value.replace(
                        /,(?=\s*$)/,
                        ""
                      );
                      custom_fields_array.push({
                        custom_field_key,
                        custom_field_value,
                      });
                      custom_fields_array.map(function (person) {
                        if (person.custom_field_key === custom_field_key) {
                          person.custom_field_value = custom_field_value;
                        }
                      });
                      custom_fields_array = $.grep(
                        custom_fields_array,
                        function (e, i) {
                          return e.custom_field_value.length;
                        }
                      );
                    });
                    efpe_last_filter = $(this)
                      .find("input:checkbox:checked")
                      .attr("name");
                    custom_fields_array = custom_fields_array
                      .map(JSON.stringify)
                      .reverse() // convert to JSON string the array content, then reverse it (to check from end to beginning)
                      .filter(function (item, index, arr) {
                        return arr.indexOf(item, index + 1) === -1;
                      }) // check if there is any occurrence of the item in whole array
                      .reverse()
                      .map(JSON.parse);
                    efpe_ajax_action(selected_term_list);
                    efpe_pagination_action(selected_term_list);
                    efpe_live_filter_reset(selected_term_list);
                    efpe_update_url();
                  }
                );
                // Filter by taxonomy.
                $(EFPE_Wrapper_ID).on(
                  "change",
                  ".efpe-filter-by",
                  function (e) {
                    e.stopPropagation();
                    e.preventDefault();
                    $(this)
                      .find("option:selected, input:radio:checked")
                      .each(function () {
                        term_id = $(this).val();
                        taxonomy = $(this).data("taxonomy");
                        var selected_tax_length = selected_term_list.length;
                        if (selected_tax_length > 0) {
                          var selected_tax =
                            selected_term_list[selected_tax_length - 1][
                              "taxonomy"
                            ];
                          selected_term_list.map(function (person) {
                            if (person.taxonomy === taxonomy) {
                              person.term_id = term_id;
                            }
                          });
                          // if ($.inArray(taxonomy, tax_list) == -1) {
                          selected_term_list.push({
                            taxonomy,
                            term_id,
                          });
                          //  }
                          if (
                            selected_term_list[selected_tax_length - 1][
                              "term_id"
                            ] == "all" &&
                            selected_tax === taxonomy
                          ) {
                            tax_list = tax_list.filter(function (val) {
                              return val !== taxonomy;
                            });
                          } else {
                            tax_list = efpe_filter_push(tax_list, taxonomy);
                          }
                          selected_term_list = $.grep(
                            selected_term_list,
                            function (e, i) {
                              return e.term_id != "all";
                            }
                          );
                        } else {
                          selected_term_list.push({
                            taxonomy,
                            term_id,
                          });
                          tax_list = Array(taxonomy);
                        }
                      });
                    if (term_id == "all") {
                      efpe_last_filter = efpe_last_filter;
                    } else {
                      efpe_last_filter = taxonomy;
                    }
                    selected_term_list = selected_term_list
                      .map(JSON.stringify)
                      .reverse()
                      .filter(function (item, index, selected_term_list) {
                        return (
                          selected_term_list.indexOf(item, index + 1) === -1
                        );
                      })
                      .reverse()
                      .map(JSON.parse);
                    efpe_hash_update_arr("page", { page: "" }, "page");
                    efpe_update_url();
                    // if ($('.efpe-filter-by', EFPE_Wrapper_ID).length > 1) {
                    efpe_live_filter_reset(selected_term_list);
                    //}
                    efpe_ajax_action(selected_term_list);
                    efpe_pagination_action(selected_term_list);
                  }
                );
                // Author filter.
                $(EFPE_Wrapper_ID).on(
                  "change",
                  ".efpe-author-filter",
                  function (e) {
                    var that;
                    $(this)
                      .find("option:selected, input:radio:checked")
                      .each(function () {
                        that = $(this);
                        author_id = that.val();
                      });
                    var author_arr = { author_id, author_id };
                    if (author_id == "all") {
                      efpe_last_filter = efpe_last_filter;
                    } else {
                      efpe_last_filter = "author_id";
                    }
                    efpe_hash_update_arr(author_id, author_arr, "author_id");
                    efpe_update_url();
                    efpe_live_filter_reset(selected_term_list);
                    efpe_ajax_action();
                    efpe_pagination_action();
                  }
                );

                // Event order asc/dsc.
                $(EFPE_Wrapper_ID).on("change", ".efpe-order", function (e) {
                  var that;
                  $(this)
                    .find("option:selected, input:radio:checked")
                    .each(function () {
                      that = $(this);
                      order = $(this).val();
                    });
                  var order_arr = { order, order };
                  efpe_hash_update_arr(order, order_arr, "order");
                  efpe_update_url();
                  efpe_ajax_action();
                  efpe_pagination_action();
                  efpe_hash_update_arr("page", { page: "" }, "page");
                  efpe_update_url();
                });
                /**
                 * Grid masonry.
                 */
                if ($(EFPE_Wrapper_ID).hasClass("efpe-masonry")) {
                  var $post_wrapper = $(".ta-row", EFPE_Wrapper_ID);
                  $post_wrapper.imagesLoaded(function () {
                    $post_wrapper.masonry(/* {
                itemSelector: 'div[class*=efpe-item-]',
                //fitWidth: true,
                percentPosition: true
              } */);
                  });
                }

                /**
                 * The Pagination effects.
                 *
                 * The effects for pagination to work for both mobile and other screens.
                 */
                var Pagination_Type = $(EFPE_Wrapper_ID).data("pagination");
                if ($(window).width() <= 480) {
                  var Pagination_Type =
                    $(EFPE_Wrapper_ID).data("pagination_mobile");
                }
                // Ajax number pagination
                if (Pagination_Type == "ajax_pagination") {
                  $(EFPE_Wrapper_ID).on(
                    "click",
                    ".efpe-post-pagination a",
                    function (e) {
                      e.preventDefault();
                      var that = $(this);
                      var totalPage = $(
                          ".efpe-post-pagination.efpe-on-desktop a:not(.efpe_next, .efpe_prev)",
                          EFPE_Wrapper_ID
                        ).length,
                        currentPage = parseInt(
                          $(
                            ".efpe-post-pagination.efpe-on-desktop .active:not(.efpe_next, .efpe_prev)",
                            EFPE_Wrapper_ID
                          ).data("page")
                        );
                      if ($(window).width() <= 480) {
                        var totalPage = $(
                            ".efpe-post-pagination.efpe-on-mobile a:not(.efpe_next, .efpe_prev)",
                            EFPE_Wrapper_ID
                          ).length,
                          currentPage = parseInt(
                            $(
                              ".efpe-post-pagination.efpe-on-mobile .active:not(.efpe_next, .efpe_prev)",
                              EFPE_Wrapper_ID
                            ).data("page")
                          );
                      }
                      page = parseInt(that.data("page"));
                      if (that.hasClass("efpe_next")) {
                        if (totalPage > currentPage) {
                          var page = currentPage + 1;
                        } else {
                          return;
                        }
                      }
                      if (that.hasClass("efpe_prev")) {
                        if (currentPage > 1) {
                          var page = currentPage - 1;
                        } else {
                          return;
                        }
                      }                      
                      var efpe_paged = { page, page };
                      $.ajax({
                        url: ajaxurl,
                        type: "post",
                        data: {
                          page: page,
                          id: pc_sid,
                          pagination_type: pc_pagination,
                          post_per_page: post_per_page,
                          efpe_settings: efpe_settings,
                          efpe_post_limit: efpe_post_limit,
                          action: "post_grid_ajax",
                          author_id: author_id,
                          order: order,
                          lang: spsp_lang,
                          keyword: keyword,
                          orderby: orderby,
                          taxonomy: taxonomy,
                          term_id: term_id,
                          term_list: selected_term_list,
                          custom_fields_array: custom_fields_array,
                          nonce: nonce,
                        },
                        error: function (response) {},
                        success: function (response) {
                          var $data = $(response);
                          var $newElements = $data;
                          if ($(EFPE_Wrapper_ID).hasClass("efpe-masonry")) {
                            var $post_wrapper = $(".ta-row", EFPE_Wrapper_ID);
                            $post_wrapper.masonry("destroy");
                            $post_wrapper
                              .html($newElements)
                              .imagesLoaded(function () {
                                $post_wrapper.masonry();
                              });
                          } else if (
                            $(EFPE_Wrapper_ID).hasClass("efpe-filter-wrapper")
                          ) {
                            $grid
                              .html($newElements)
                              .isotope("appended", $newElements)
                              .imagesLoaded(function () {
                                $grid.isotope("layout");
                              });
                            efpe_item_same_height();
                          } else {
                            $(
                              ".ta-row, .efpe-timeline-grid, .ta-collapse, .table-responsive tbody",
                              EFPE_Wrapper_ID
                            ).html($newElements);
                            if (efpeAccordion.length > 0) {
                              efpeAccordion.accordion("refresh");
                              if (accordion_mode === "multi-open") {
                                efpeAccordion
                                  .find(".efpe-collapse-header")
                                  .next()
                                  .slideDown();
                                efpeAccordion
                                  .find(".efpe-collapse-header .fa")
                                  .removeClass("fa-plus")
                                  .addClass("fa-minus");
                              }
                            }
                            var $newElements = $data.css({
                              opacity: 1,
                            });
                          }
                          $(".page-numbers", EFPE_Wrapper_ID).removeClass(
                            "active"
                          );
                          $(".page-numbers", EFPE_Wrapper_ID).each(function () {
                            // if (parseInt($('.efpe-post-pagination a').data('page')) === page) {
                            $(
                              ".efpe-post-pagination a[data-page=" + page + "]",
                              EFPE_Wrapper_ID
                            ).addClass("active");
                            // }
                          });
                          $(".efpe_next", EFPE_Wrapper_ID).removeClass(
                            "active"
                          );
                          $(".efpe_prev", EFPE_Wrapper_ID).removeClass(
                            "active"
                          );
                          $(
                            ".efpe-post-pagination a.active",
                            EFPE_Wrapper_ID
                          ).each(function () {
                            if (parseInt($(this).data("page")) === totalPage) {
                              $(".efpe_next", EFPE_Wrapper_ID).addClass(
                                "active"
                              );
                            }
                            if (parseInt($(this).data("page")) === 1) {
                              $(".efpe_prev", EFPE_Wrapper_ID).addClass(
                                "active"
                              );
                            }
                          });
                          if (efpeAccordion.length > 0) {
                            efpeAccordion.accordion("refresh");
                            if (accordion_mode === "multi-open") {
                              efpeAccordion
                                .find(".efpe-collapse-header")
                                .next()
                                .slideDown();
                              efpeAccordion
                                .find(".efpe-collapse-header .fa")
                                .removeClass("fa-plus")
                                .addClass("fa-minus");
                            }
                          }
                          $newElements.animate({
                            opacity: 1,
                          });
                          efpe_lazyload();
                          // Ajax Number pagination go to current shortcode top.
                          var url_hash = window.location.search;
                          if (url_hash.indexOf("efpe_page") >= 0) {
                            var current_screen_id =
                              document.querySelector(EFPE_Wrapper_ID);
                            current_screen_id.scrollIntoView();
                          }
                        },
                      });
                      efpe_hash_update_arr(page, efpe_paged, "page");
                      efpe_update_url();
                    }
                  );
                }

                /**
                 * Ajax load on click and Infinite scroll.
                 */
                if (
                  Pagination_Type == "infinite_scroll" ||
                  Pagination_Type == "ajax_load_more"
                ) {
                  $(EFPE_Wrapper_ID).each(function () {
                    var EndingMessage = $(this)
                      .find(".ta-efpe-pagination-data")
                      .data("endingtext");
                    var LoadMoreText = $(this)
                      .find(".ta-efpe-pagination-data")
                      .data("loadmoretext");
                    if (
                      !$(this)
                        .find(".efpe-load-more")
                        .hasClass("efpe-load-more-initialize")
                    ) {
                      if (
                        $(".efpe-post-pagination a", EFPE_Wrapper_ID).length
                      ) {
                        $(".efpe-post-pagination", EFPE_Wrapper_ID)
                          .eq(0)
                          .before(
                            '<div class="efpe-load-more"><button efpe-processing="0">' +
                              LoadMoreText +
                              "</button></div>"
                          );
                      }
                      if (Pagination_Type == "infinite_scroll") {
                        $(".efpe-load-more", EFPE_Wrapper_ID).addClass(
                          "efpe-hide"
                        );
                      }
                      $(".efpe-post-pagination", EFPE_Wrapper_ID).addClass(
                        "efpe-hide"
                      );
                      $(
                        ".ta-row div[class*=efpe-item-]",
                        EFPE_Wrapper_ID
                      ).addClass("efpe-added");
                      $(this)
                        .find(".efpe-load-more")
                        .addClass("efpe-load-more-initialize");
                      $(this).on(
                        "click",
                        ".efpe-load-more button",
                        function (e) {
                          e.preventDefault();
                          if (
                            $(
                              ".efpe-post-pagination a.active:not(.efpe_next, .efpe_prev)",
                              EFPE_Wrapper_ID
                            ).length
                          ) {
                            $(".efpe-load-more button").attr(
                              "efpe-processing",
                              1
                            );
                            var current_page = parseInt(
                              $(
                                ".efpe-post-pagination a.active:not(.efpe_next, .efpe_prev)",
                                EFPE_Wrapper_ID
                              ).data("page")
                            );
                            current_page = current_page + 1;
                            $(".efpe-load-more", EFPE_Wrapper_ID).hide();
                            $(".efpe-post-pagination", EFPE_Wrapper_ID)
                              .eq(0)
                              .before(
                                '<div class="efpe-infinite-scroll-loader"><svg width="44" height="44" viewBox="0 0 44 44" xmlns="http://www.w3.org/2000/svg" stroke="#444"><g fill="none" fill-rule="evenodd" stroke-width="2"><circle cx="22" cy="22" r="1"><animate attributeName="r" begin="0s" dur="1.8s" values="1; 20" calcMode="spline" keyTimes="0; 1" keySplines="0.165, 0.84, 0.44, 1" repeatCount="indefinite" /> <animate attributeName="stroke-opacity" begin="0s" dur="1.8s" values="1; 0" calcMode="spline" keyTimes="0; 1" keySplines="0.3, 0.61, 0.355, 1" repeatCount="indefinite" /> </circle> <circle cx="22" cy="22" r="1"> <animate attributeName="r" begin="-0.9s" dur="1.8s" values="1; 20" calcMode="spline" keyTimes="0; 1" keySplines="0.165, 0.84, 0.44, 1" repeatCount="indefinite" /> <animate attributeName="stroke-opacity" begin="-0.9s" dur="1.8s" values="1; 0" calcMode="spline" keyTimes="0; 1" keySplines="0.3, 0.61, 0.355, 1" repeatCount="indefinite"/></circle></g></svg></div>'
                              );
                            var totalPage = $(
                              ".efpe-post-pagination.efpe-on-desktop.infinite_scroll a:not(.efpe_next, .efpe_prev), .efpe-post-pagination.efpe-on-desktop.ajax_load_more a:not(.efpe_next, .efpe_prev)",
                              EFPE_Wrapper_ID
                            ).length;
                            if ($(window).width() <= 480) {
                              var totalPage = $(
                                ".efpe-post-pagination.efpe-on-mobile.infinite_scroll a:not(.efpe_next, .efpe_prev), .efpe-post-pagination.ajax_load_more.efpe-on-mobile  a:not(.efpe_next, .efpe_prev)",
                                EFPE_Wrapper_ID
                              ).length;
                            }
                            page = current_page;
                            $.ajax({
                              url: ajaxurl,
                              type: "post",
                              data: {
                                page: page,
                                id: pc_sid,
                                pagination_type: pc_pagination,
                                post_per_page: post_per_page,
                                efpe_settings: efpe_settings,
                                efpe_post_limit: efpe_post_limit,
                                action: "post_grid_ajax",
                                author_id: author_id,
                                order: order,
                                lang: spsp_lang,
                                keyword: keyword,
                                orderby: orderby,
                                taxonomy: taxonomy,
                                term_id: term_id,
                                term_list: selected_term_list,
                                custom_fields_array: custom_fields_array,
                                nonce: nonce,
                              },
                              error: function (response) {},
                              success: function (response) {
                                var $data = $(response);
                                var $newElements = $data;
                                if (
                                  $(EFPE_Wrapper_ID).hasClass("efpe-masonry")
                                ) {
                                  var $post_wrapper = $(
                                    ".ta-row",
                                    EFPE_Wrapper_ID
                                  );
                                  $post_wrapper.masonry("destroy");
                                  $post_wrapper
                                    .append($newElements)
                                    .imagesLoaded(function () {
                                      $post_wrapper.masonry();
                                    });
                                } else if (
                                  $(EFPE_Wrapper_ID).hasClass(
                                    "efpe-filter-wrapper"
                                  )
                                ) {
                                  $grid
                                    .append($newElements)
                                    .isotope("appended", $newElements)
                                    .imagesLoaded(function () {
                                      $grid.isotope("layout");
                                    });
                                  efpe_item_same_height();
                                } else {
                                  var $newElements = $data.css({
                                    opacity: 0,
                                  });
                                  $(
                                    ".ta-row, .efpe-timeline-grid, .ta-collapse, .table-responsive tbody",
                                    EFPE_Wrapper_ID
                                  ).append($newElements);
                                  if (efpeAccordion.length > 0) {
                                    efpeAccordion.accordion("refresh");
                                    if (accordion_mode === "multi-open") {
                                      efpeAccordion
                                        .find(".efpe-collapse-header")
                                        .next()
                                        .slideDown();
                                      efpeAccordion
                                        .find(".efpe-collapse-header .fa")
                                        .removeClass("fa-plus")
                                        .addClass("fa-minus");
                                    }
                                  }
                                  var $newElements = $data.css({
                                    opacity: 1,
                                  });
                                }
                                $(".page-numbers", EFPE_Wrapper_ID).removeClass(
                                  "active"
                                );
                                $(".page-numbers", EFPE_Wrapper_ID).each(
                                  function () {
                                    $(
                                      ".efpe-post-pagination a[data-page=" +
                                        page +
                                        "]",
                                      EFPE_Wrapper_ID
                                    ).addClass("active");
                                  }
                                );
                                $(
                                  ".efpe-infinite-scroll-loader",
                                  EFPE_Wrapper_ID
                                ).remove();
                                if (Pagination_Type == "ajax_load_more") {
                                  $(".efpe-load-more").show();
                                }
                                $(".efpe-load-more button").attr(
                                  "efpe-processing",
                                  0
                                );
                                $(
                                  ".ta-row div[class*=efpe-item-]",
                                  EFPE_Wrapper_ID
                                )
                                  .not(".efpe-added")
                                  .addClass("animated efpeFadeIn")
                                  .one(
                                    "webkitAnimationEnd animationEnd",
                                    function () {
                                      $(this)
                                        .removeClass("animated efpeFadeIn")
                                        .addClass("efpe-added");
                                    }
                                  );
                                if (totalPage == current_page) {
                                  $(".efpe-load-more", EFPE_Wrapper_ID)
                                    .addClass("finished")
                                    .removeClass("efpe-hide");
                                  $(".efpe-load-more", EFPE_Wrapper_ID)
                                    .show()
                                    .html(EndingMessage);
                                }
                                efpe_lazyload();
                              },
                            });
                          } else {
                            $(".efpe-load-more", EFPE_Wrapper_ID)
                              .addClass("finished")
                              .removeClass("efpe-hide");
                            $(".efpe-load-more", EFPE_Wrapper_ID)
                              .show()
                              .html(EndingMessage);
                          }
                        }
                      );
                    }
                    if (Pagination_Type == "infinite_scroll") {
                      var bufferBefore = Math.abs(20);
                      $(window).scroll(function () {
                        if (
                          $(
                            ".ta-row, .ta-collapse, .efpe-timeline-grid, .table-responsive tbody",
                            EFPE_Wrapper_ID
                          ).length
                        ) {
                          var TopAndContent =
                            $(
                              ".ta-row, .ta-collapse, .efpe-timeline-grid, .table-responsive tbody",
                              EFPE_Wrapper_ID
                            ).offset().top +
                            $(
                              ".ta-row, .ta-collapse, .efpe-timeline-grid, .table-responsive tbody",
                              EFPE_Wrapper_ID
                            ).outerHeight();
                          var areaLeft = TopAndContent - $(window).scrollTop();
                          if (areaLeft - bufferBefore < $(window).height()) {
                            if (
                              $(".efpe-load-more button", EFPE_Wrapper_ID).attr(
                                "efpe-processing"
                              ) == 0
                            ) {
                              $(
                                ".efpe-load-more button",
                                EFPE_Wrapper_ID
                              ).trigger("click");
                            }
                          }
                        }
                      });
                    }
                  });
                }

                /* Preloader js */
                $(document).ready(function () {
                  $(".efpe-preloader", EFPE_Wrapper_ID).css({
                    backgroundImage: "none",
                    visibility: "hidden",
                  });
                });
                // This function added for efpe-Lazyload.
                function efpe_lazyload() {
                  var $is_find = $(".eventful__item--thumbnail img").hasClass(
                    "efpe-lazyload"
                  );
                  if ($is_find) {
                    $("img.efpe-lazyload")
                      .efpe_lazyload({ effect: "fadeIn", effectTime: 2000 })
                      .removeClass("efpe-lazyload")
                      .addClass("efpe-lazyloaded");
                  }
                }
                efpe_lazyload();
              });
          }
        };
        efpe_myScript();
      }
    );
  });
  // Select the list item
  const event_meta_address = document.querySelectorAll('.event_meta_address');
  event_meta_address.forEach((address) => {
    const spans = address.querySelectorAll('a');
  
    spans.forEach((span, index) => {
      span.textContent = span.textContent.replace(/,\s*$/, '');
  
      if (spans.length === 2 && index === 0) {
        span.textContent += ',';
      } else if (spans.length === 3 && (index === 0 || index === 1)) {
        span.textContent += ',';
      }
    });
  })
})(jQuery);
