// source --> https://eternize360.com.br/wp-content/plugins/wpvr-pro/admin/js/wpvr-public.js?ver=6.9.4 
(function($) {
  'use strict';
  var owl;
  /**
   * All of the code for your public-facing JavaScript source
   * should reside in this file.
   *
   * Note: It has been assumed you will write jQuery code here, so the
   * $ function reference has been prepared for usage within the scope
   * of this function.
   *
   * This enables you to define handlers, for when the DOM is ready:
   *
   * $(function() {
   *
   * });
   *
   * When the window is loaded:
   *
   * $( window ).load(function() {
   *
   * });
   *
   * ...and/or other possibilities.
   *
   * Ideally, it is not considered best practise to attach more than a
   * single DOM-ready or window-load handler for a particular page.
   * Although scripts in the WordPress core, Plugins and Themes may be
   * practising this, we should strive to set a better example in our own work.
   */
  // var owl;
  $(document).ready(function () {

    // Hide the VR mode title initially
    $(".vr-mode-title").hide();

    $('.wpvr-cardboard').each(function () {
      let $container = $(this);
      let thiscar = $container.find('.vrowl-carousel');
      let nextBtn = $container.find('.wpvr_slider_nav .owl-next');
      let prevBtn = $container.find('.wpvr_slider_nav .owl-prev');

      // Initialize Owl Carousel with autoWidth enabled
      let owl = thiscar.owlCarousel({
        margin: 18,
        autoWidth: false,
        items: 5,
        responsive: {
          0: {
            items: 2
          },
          480: {
            items: 3
          },
          768: {
            items: 3
          },
          1024: {
            items: 4
          },
          1200: {
            items: 5
          }
        },
        dots: false,
        nav: true,
        mouseDrag: false,
        touchDrag: true,
        pullDrag: false,
        freeDrag: false,
        smartSpeed: 300,
      });



      // Navigation buttons: next and previous
      nextBtn.on('click', function () {
        owl.trigger('next.owl.carousel');
      });

      prevBtn.on('click', function () {
        owl.trigger('prev.owl.carousel');
      });

      // Handle clicked item styling and default selection
      thiscar.on('initialized.owl.carousel refreshed.owl.carousel', function () {
        let owlItems = thiscar.find('.owl-item');

        // If no item is marked clicked, set the first one
        if (owlItems.length > 0 && !owlItems.hasClass('clicked')) {
          owlItems.removeClass('clicked');
          owlItems.first().addClass('clicked');
        }

        // Add click event to mark selected item
        owlItems.off('click').on('click', function () {
          owlItems.removeClass('clicked');
          $(this).addClass('clicked');
        });
      });

      // Manually trigger the initialized event after initial setup
      thiscar.trigger('initialized.owl.carousel');

      // Refresh carousel after a short delay to fix layout issues
      setTimeout(function () {
        owl.trigger('refresh.owl.carousel');
      }, 100);
    });
  });


  $('.customNextBtn').on('click', function() {
    owl.trigger('next.owl.carousel');
  });

  // With this (also fixing the method call)
  $('.owl-prev').on('click', function() {
    owl.trigger.prev();
  });

  // With this (also fixing the method call)
  $('.owl-next').on('click', function() {
    owl.trigger.next();
  });


  $(document).on('click', '.scene-navigation-list', function () {
    var sceneId = $(this).attr('scene_id');

    // Remove previous 'active' class from nav items and add to the clicked one
    $('.scene-navigation-list').removeClass('active');
    $(this).addClass('active');

    // Remove 'clicked' from all owl-items
    $('.owl-item').removeClass('clicked');

    // Find the <img> with id like 'sceneId_gallery_...' and then go up to its .owl-item
    var selector = 'img.scctrl[id^="' + sceneId + '_gallery_"]';

    $(selector).closest('.owl-item').addClass('clicked');
  });

})(jQuery);


function wpvrhotspot(hotSpotDiv, hotspotData) {
  let args = hotspotData.on_click_content;

  if(args && args.trim() !== '') {
    var hasTextContent = args.replace(/<[^>]*>/g, '').trim() !== '';
    var hasMediaContent = args.match(/<(img|video|audio|iframe|embed|object)\b[^>]*>/i);
    var hasOtherContent = args.replace(/<(p|br|div|span)\b[^>]*\/?>/gi, '').trim() !== '';

    if(hasTextContent || hasMediaContent || hasOtherContent) {
      var target = hotSpotDiv.target;
      var idgetter = jQuery(target).parent().parent().attr('id');
      var pano_id = idgetter.replace('pano','');

      // Handle audio controls
      var music = jQuery('#vrAudio' + pano_id).attr('data-autoplay');
      var player = jQuery('#audio_control' + pano_id).attr('data-play');

      if (music == "on" || (player == 'on')) {
        jQuery('#vrAudio' + pano_id).get(0).pause();
        jQuery('.wpvrvolumeicon' + pano_id).removeClass('fas fa-volume-up');
        jQuery('.wpvrvolumeicon' + pano_id).addClass('fas fa-volume-mute');
      }

      var argst = args.replace(/\\/g, '');

      var $ = jQuery;
      var div = document.createElement('div');
      div.innerHTML = argst.trim();

      if ($(div).find('.fluentform').length) {
        var id = $(div).find('.fluentform form').attr('data-form_id');
        var form_class = '.fluentform_wrapper_' + id + ':first';

        $(hotSpotDiv.target).parent().siblings('.wpvr-hotspot-tweak-contents-wrapper')
            .find('.ff-message-success, .ff-errors-in-stack').show();
        $(hotSpotDiv.target).parent().siblings('.wpvr-hotspot-tweak-contents-wrapper')
            .find('.fluentform').hide();
        $(hotSpotDiv.target).parent().siblings('.wpvr-hotspot-tweak-contents-wrapper')
            .find(form_class).show();
        $(hotSpotDiv.target).parent().siblings('.wpvr-hotspot-tweak-contents-wrapper')
            .fadeIn();

        $(div).find('.fluentform').show();
      } else {

        $(hotSpotDiv.target).parent().siblings(".custom-ifram-wrapper")
            .find('.custom-ifram').html(argst);
        $(hotSpotDiv.target).parent().siblings(".custom-ifram-wrapper").fadeIn();


        $('.wpvr-product-container p.add_to_cart_inline a.button')
            .wrap('<span class="wpvr-cart-wrap"></span>');
        $('.wpvr-product-container p.add_to_cart_inline a.button')
            .attr("target", "_blank");
      }

      $(hotSpotDiv.target).parent().parent(".pano-wrap").addClass("show-modal");
      if ($('.rtec').length && !$('.rtec').hasClass('rtec-initialized')) {
        rtecInit();
      }
    }
  }

  // Analytics tracking (always run if hotspotData exists)
  if(  hotspotData && typeof hotspotData === 'object' && Object.keys(hotspotData).length > 0) {
    jQuery.ajax({
      url: wpvrAnalyticsObj.ajaxUrl,
      type: "POST",
      data: {
        action: "store_scene_hotspot_data",
        scene_id: hotspotData.scene_id,
        tour_id: hotspotData.tour_id,
        type: 'hotspot',
        hotspot_id: hotspotData.hotspot_id || "",
        user_agent: navigator.userAgent,
        device_type: getDeviceType() || "desktop",
        nonce: wpvrAnalyticsObj.nonce,
        session_id: wpvrAnalyticsObj.session_id,
      },
      success: function (response) {
        // Handle success if needed
      },
      error: function (error) {
        // Handle error if needed
      }
    });
  }

}

function wpvrtooltip(hotSpotDiv, args) {
  if(args && args.trim() !== '') {
    var hasTextContent = args.replace(/<[^>]*>/g, '').trim() !== '';
    var hasMediaContent = args.match(/<(img|video|audio|iframe|embed|object)\b[^>]*>/i);
    var hasOtherContent = args.replace(/<(p|br|div|span)\b[^>]*\/?>/gi, '').trim() !== '';

    if(hasTextContent || hasMediaContent || hasOtherContent) {
      hotSpotDiv.classList.add('custom-tooltip');
      var p = document.createElement('p');
      args = args.replace(/\\/g, "");
      p.innerHTML = args;
      hotSpotDiv.appendChild(p);
      var tooltipWidth = p.scrollWidth;
      var tooltipHeight = p.scrollHeight;
      var hotspotWidth = hotSpotDiv.offsetWidth;
      p.style.marginLeft = -(tooltipWidth - hotspotWidth) / 2 + 'px';
      p.style.marginTop = -tooltipHeight - 12 + 'px';
    }
  }
}

jQuery(document).ready(function($) {

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

    var pano_id = $(this).attr('data-id');

    $(this).parent('.wpvr-hotspot-tweak-contents-wrapper').find('.ff-message-success, .ff-errors-in-stack').hide();
    $(this).parent('.wpvr-hotspot-tweak-contents-wrapper').fadeOut();
    $(this).parent(".custom-ifram-wrapper").fadeOut();
    $(this).parents(".pano-wrap").removeClass("show-modal");

    $('.vr-iframe').attr('src', '');
    if ($('#wpvr-video').length != 0) {
      $('#wpvr-video').get(0).pause();
    }

     $(this).parent(".custom-ifram-wrapper").find('.custom-ifram').empty();

  });

  $(".elementor-tab-title").on("click", function(e) {
    $('audio').each(function() {
      $(this).get(0).pause();
    });
  });

});

jQuery(document).ready(function($) {
  $('.cp-logo-ctrl').on('click', function() {
    $(this).toggleClass('show');
  });

  $('.vrbounce').on('click', function() {
    window.dispatchEvent(new Event('resize'));
  });
});

jQuery(document).ready(function($) {

  $('#player_audio').on('click', function() {

    if (this.paused == false) {
      this.pause();
      alert('music paused');
    } else {
      this.play();
      alert('music playing');
    }
  });

});


jQuery(document).ready(function($) {
  // Check if the wpvr_public object exists
  if (typeof wpvr_public != "undefined") {
    var notice_active = wpvr_public.notice_active;
    var notice = wpvr_public.notice;

    // Check if notices are enabled
    if (notice_active == "true") {
      if (!$.cookie("wpvr_mobile_notice")) {
        if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
          if ($(".pano-wrap")[0]) {
            $('body').append("<div class='wpvr-mobile-notice'><p>" + notice + "</p> <span class='notice-close'><i class='fa fa-times'></i></span></div>");
          }
        }
      }
    }

    // Set up event handler using the recommended .on() method
    $('.wpvr-mobile-notice .notice-close').on('click', function() {
      $('.wpvr-mobile-notice').fadeOut();
      $.cookie('wpvr_mobile_notice', 'true');
    });



    
  }

});


/**
 * Determines the user's device type based on their user agent string.
 * @returns {string} Device type: "mobile", "tablet", or "desktop"
 */
function getDeviceType() {
  const userAgent = navigator.userAgent.toLowerCase();

  // Check for mobile devices first
  if (/mobile|android|iphone|ipod|blackberry|iemobile|opera mini/i.test(userAgent)) {
    return "mobile";
  }
  // Check for tablets (note that some tablets may also match mobile patterns)
  else if (/tablet|ipad/i.test(userAgent)) {
    return "tablet";
  }
  // Default to desktop for all other devices
  else {
    return "desktop";
  }
};