jQuery(document).ready(function () {
    //jQuery(document).ready(function () {
    var h3height = 0;
    jQuery(
      ".itsp_is_engage_carousel .itsp_is_engage_row .itsp_block-text .itsp_review_description"
    ).each(function () {
      if (h3height < jQuery(this).outerHeight()) {
        h3height = jQuery(this).outerHeight();
      }
    });
    jQuery(
      ".itsp_is_engage_carousel .itsp_is_engage_row .itsp_block-text .itsp_review_description"
    ).height(h3height);
    ///});
  
    jQuery(".itsp_is_events_grid_row, .itsp_is_engage_row").each(function () {
      // Cache the highest
      var highestBox = 0;
      jQuery(
        ".itsp_engage_grid_block .itsp_native_coupon img, .itsp_is_engage_row .itsp_native_coupon img",
        this
      ).each(function () {
        if (jQuery(this).height() > highestBox) {
          highestBox = jQuery(this).height();
        }
      });
      // Set the height of all those children to whichever was highest
      jQuery(
        ".itsp_engage_grid_block .itsp_native_coupon img, .itsp_is_engage_row .itsp_native_coupon img, .itsp_is_engage_slider_block .owl-item img",
        this
      ).height(highestBox);
    });
  
    jQuery(document).on(
      "click",
      ".itsp_events_pop ,.itsp_coupon_pop",
      function (e) {
        e.preventDefault();
        var pop_div = jQuery(this).attr("class").split(" ")[1];
        var src =
          "//" +
          jQuery(this)
            .attr("href")
            .replace(/^https?:\/\//, "");
        if (jQuery("#itsp_main_iframe_div").length) {
          jQuery("#itsp_main_iframe_div").html(
            '<iframe class="itsp_iframe_placeholder"  scrolling="no" src="' +
              src +
              '" style="width:100%; height: 98vh;">'
          );
          dynamaic_iframe_height();
        } else {
          jQuery(
            '<div class="modal fade" id="itsp_modal_popup_show" tabindex="-1" role="dialog" aria-labelledby="itsp_modal_popup_show" aria-hidden="true"><div class="modal-dialog modal-lg" role="document" style="margin-top: 0px; margin-bottom: 0px;"><div class="modal-content"><div class="modal-header itsp_popup_close_icon border-0 d-flex justify-content-end" style="position: absolute; right: 0px; z-index: 1050;"><button type="button" class="btn btn-link p-0 itsp_model_popup_close" data-dismiss="modal" style="font-size: 24px; color: #000; text-decoration: none; height: 45px; width: 45px; border: 1px solid #000000; border-radius: 50%; ">X</button></div><div id="itsp_main_iframe_div" style="position: relative;"><iframe class="itsp_iframe_placeholder" src="' +
              src +
              '" style="width:100%; height: 98vh;" ></iframe></div></div></div></div>'
          ).prependTo("body");
          dynamaic_iframe_height();
        }
        if (jQuery("#itsp_modal_popup_show").length) {
          jQuery("#itsp_modal_popup_show").modal("show");
        }
      }
    );
  
    jQuery(document).on("click", ".itsp_calculator_pop", function (e) {
      e.preventDefault();
      var fontsizep = jQuery(this).attr("font-size-p");
      var fontinfoh2 = jQuery(this).attr("fontinfoh2");
      var primaryColor = jQuery(this).attr("primaryColor");
      var secondaryColor = jQuery(this).attr("secondaryColor");
      var currency = jQuery(this).attr("currency");
      var total_investment = jQuery(this).attr("total_investment");
      var irr = jQuery(this).attr("total_irr");
      jQuery(
        '<div class="modal fade" id="itsp_modal_calculate_popup_show" tabindex="-1" role="dialog" aria-labelledby="itsp_modal_popup_show" aria-hidden="true">' +
          '<div class="modal-dialog modal-lg" role="document">' +
          '<div class="modal-content">' +
          '<div class="modal-header border-0 d-flex justify-content-end"><button type="button" class="btn btn-link p-0 itsp_model_popup_cal_close" data-dismiss="modal" style="font-size: 24px; color: #000; text-decoration: none;">&times;</button></div>' +
          '<div id="itsp_main_iframe_divs">' +
          '<div class="itsp_offer_popups">' +
          '<div style="box-shadow: 0px 2.83px 16.96px 0px #0000001A; padding: 20px; border-radius: 10px;">' +
          "<h3>Calculate your estimated Investment Return</h3>" +
          '<p style="font-size: ' +
          fontsizep +
          '">Discover potential earnings with our easy-to-use return calculator—quick and accurate</p>' +
          '<div class="itps_calcul_data_sections" style="display: flex; flex-direction: row; gap: 10px; margin-top: 10px;">' +
          '<div class="itps_calcul_data1" style="width: 65%; display: flex; flex-direction: column; justify-content: space-between; align-items: center; height: 450px; padding: 20px;">' +
          '<div style="display: flex; flex-direction: column; gap: 20px; width: 100%;">' +
          "<div style=\"display: flex; justify-content: space-between; width: 100%; font-size: <?php echo esc_attr($brandkit_fontinfo['p']); ?>\">" +
          "<p>Amount you want to invest</p>" +
          '<p class="itsp_calculator_rangeValues" id="itsp_investment-amount-display">' +
          currency +
          '<input type="text" id="itsp_investment-amount-input" value="' +
          total_investment +
          '" style="width: 100px; text-align: center; border: none; color: #000000;" /></p>' +
          "</div>" +
          '<div><input type="range" id="itsp_prise_range" name="itsp_prise_range" min="1" max="500000" class="itsp_prise_range" value="' +
          total_investment +
          '"></div>' +
          "</div>" +
          '<div style="display: flex; flex-direction: column; gap: 20px; width: 100%;">' +
          '<div style="display: flex; justify-content: space-between; width: 100%; font-size: ' +
          fontsizep +
          '">' +
          "<p>Time Period (yrs)</p>" +
          '<p class="itsp_calculator_rangeValues" id=""><input type="text" id="itsp_time-period-display" value="5" style="width: 72px; text-align: center; border: none; color: #000000;" />years</p>' +
          "</div>" +
          '<div><input type="range" id="itsp_time-period-range" name="itsp_time-period-range" min="0.5" max="50" class="itsp_prise_range" value="5"></div>' +
          "</div>" +
          '<div style="display: flex; flex-direction: column; gap: 20px; width: 100%;">' +
          '<div style="display: flex; justify-content: space-between; width: 100%; font-size: ' +
          fontsizep +
          '">' +
          "<p>Estimated IRR (Based on the Property)</p>" +
          '<p class="itsp_calculator_rangeValues" id=""><input type="text" id="irr-display" value="' +
          irr +
          '" style="width: 100px; text-align: center; border: none; color: #000000;" />%</p>' +
          "</div>" +
          '<div><input type="range" id="itsp_irr-range" name="itsp_irr-range" min="1" max="100" class="itsp_prise_range" value="' +
          irr +
          '"></div>' +
          "</div>" +
          '<h5 style="cursor: pointer; width: 100%; background: linear-gradient(270.08deg, ' +
          secondaryColor +
          " 0.07%, " +
          primaryColor +
          ' 98.28%); text-align: center; color: #FFFFFF; padding: 12px; border-radius: 10px;" id="itsp-calculate-btn">Calculate</h5>' +
          "</div>" +
          '<div class="itps_calcul_totalAmount" style="width: 35%; display: flex; flex-direction: column; justify-content: space-between; height: 450px; padding: 20px; background: linear-gradient(360deg, ' +
          secondaryColor +
          " 0%, " +
          primaryColor +
          ' 100%);">' +
          '<p style="color: #FFFFFF; font-size: "' +
          fontsizep +
          '">Your Estimated Investment Return</p>' +
          '<h3 style="color: #FFFFFF; font-size: ' +
          fontinfoh2 +
          ';" id="itsp_investment-return">' +
          currency +
          " " +
          " 3437513.75</h3>" +
          "</div>" +
          "</div>" +
          "</div>" +
          "</div>" +
          "</div>" +
          "</div>" +
          "</div>"
      ).prependTo("body");
      var pop_div = jQuery(this).attr("class").split(" ")[1];
      // Using .data() to get custom data attributes
      //var irr = jQuery(this).attr('total_irr');
      //jQuery('#itsp_investment-amount-display').text(currency + total_investment);
      jQuery("#irr-display").text(irr + "%");
      var investmentReturnDisplay = jQuery("#itsp_investment-return");
      if (jQuery("#itsp_modal_calculate_popup_show").length) {
        jQuery("#itsp_modal_calculate_popup_show").modal("show");
        // Add a timer before triggering the click event
        setTimeout(function () {
          jQuery("#itsp-calculate-btn").click();
        }, 100); // Delay in milliseconds (2000ms = 2 seconds)
      }
  
      const syncInputs = (rangeSelector, inputSelector) => {
        jQuery(rangeSelector).on("input", function () {
          jQuery(inputSelector).val(this.value);
        });
        jQuery(inputSelector).on("input", function () {
          jQuery(rangeSelector).val(this.value);
        });
      };
  
      var investmentRange = jQuery("#itsp_investment-amount-input");
      var timePeriodRange = jQuery("#itsp_time-period-display");
      var irrRange = jQuery("#irr-display");
      var investmentAmountDisplay = jQuery("#itsp_investment-amount-display");
  
      // syncInputs("#itsp_investment-amount-display", "#itsp_prise_range");
      syncInputs("#itsp_prise_range", "#itsp_investment-amount-input");
      syncInputs("#itsp_time-period-range", "#itsp_time-period-display");
      syncInputs("#itsp_irr-range", "#irr-display");
      var timePeriodDisplay = jQuery("#irr-display");
      var irrDisplay = jQuery("#irr-display");
  
      var investmentReturnDisplay = jQuery("#itsp_investment-return");
  
      // Update values based on range input
      // investmentRange.on('input', function() {
      //     var investmentAmount = parseInt(investmentRange.val());
      //     investmentAmountDisplay.text(currency + investmentAmount);
      // });
  
      timePeriodRange.on("input", function () {
        var timePeriod = timePeriodRange.val();
        timePeriodDisplay.text(timePeriod + " years");
      });
  
      irrRange.on("input", function () {
        var irr = irrRange.val();
        irrDisplay.text(irr + "%");
      });
  
      // Calculate function
  
      jQuery("#itsp-calculate-btn").click(function () {
        var investmentAmount = parseInt(investmentRange.val()); // Get the investment amount
        var timePeriod = parseInt(timePeriodRange.val()); // Fixed time period of 5 years
        var irr = parseFloat(irrRange.val()) / 100; // Get the IRR and convert it to decimal (e.g., 10% becomes 0.10)
        function numberFormatI18n(number, decimals = 0, locale = "en-US") {
          return new Intl.NumberFormat(locale, {
            minimumFractionDigits: decimals,
            maximumFractionDigits: decimals,
          }).format(number);
        }
        // Calculate simple interest
        var interest = investmentAmount * irr * timePeriod; // Simple Interest formula: P * R * T
        var totalAmount = numberFormatI18n(
          investmentAmount + interest,
          2,
          "en-US"
        ); // Total amount after adding interest
  
        // Display the calculated result
        //investmentReturnDisplay.text('₹' + interest.toFixed(2)); // Display only the interest
        investmentReturnDisplay.html(currency + totalAmount); // Display the total amount
      });
    });
  
    jQuery(document).on("click", ".itsp_model_popup_cal_close", function () {
      jQuery("#itsp_modal_calculate_popup_show").modal("hide");
    });
  
    jQuery(document).on("click", ".itsp_model_popup_close", function () {
      jQuery("#itsp_modal_popup_show").modal("hide");
    });
  
    jQuery(".owl_slider").owlCarousel({
      loop: false,
      margin: 10,
      nav: true,
      items: 1,
    });
    var numberslider = jQuery("#reviews_carousel").attr("nbrslider");
    var responsive =
      "responsive: {0: {items: 1},600: {items: 1},1000: {items: 1}}";
    if (numberslider == "2") {
      responsive = "responsive: {0: {items: 1},600: {items: 2},1000: {items: 2}}";
    } else if (numberslider == "3") {
      responsive = "responsive: {0: {items: 1},600: {items: 2},1000: {items: 3}}";
    }
    jQuery(".owl-carousel-review").owlCarousel({
      loop: true,
      margin: 10,
      nav: true,
      responsive,
    });
  
    var idArray = [];
    jQuery(".create_embed_widget").each(function () {
      idArray.push(this.id);
      createEmbedWidget(this.id);
    });
  
    function dynamaic_iframe_height() {
      window.onmessage = (e) => {
        var height = e.data.split("documentHeight:")[1];
        if (typeof height !== "undefined") {
          jQuery(".itsp_iframe_placeholder").css("height", height + "px");
        }
      };
    }
  });
