/**
Core script to handle the entire theme and core functions
**/
var Frost = (function () {
  ("use strict");

  var screenWidth = $(window).width();

  // Calculate 45 days from today for the countdown
  var getLaunchDate = function () {
    var daysToAdd = 45;
    var today = new Date();
    var launchDate = new Date(today);
    launchDate.setDate(today.getDate() + daysToAdd);

    var monthNames = [
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December",
    ];

    return (
      launchDate.getDate() +
      " " +
      monthNames[launchDate.getMonth()] +
      " " +
      launchDate.getFullYear()
    );
  };

  var WebsiteLaunchDate = getLaunchDate(); // Always 45 days from now

  /* Countdown Style 1 - Dynamic 45-day countdown */
  var handleCountDown = function (launchDate) {
    if ($(".countdown").length) {
      $(".countdown").countdown({ date: launchDate }, function () {
        $(".countdown").text("we are live");
      });
    }
  };

  /* Countdown Style 2 (Optional - if needed) */
  var handleFinalCountDown = function () {
    var launchDate = $(".countdown-timer").data("date") || WebsiteLaunchDate;

    if ($(".countdown-timer").length > 0) {
      var startTime = new Date().getTime();
      var currentTime = new Date().getTime();
      var endTime = new Date(launchDate).getTime();

      $(".countdown-timer").final_countdown({
        start: startTime / 1000,
        end: endTime / 1000,
        now: currentTime / 1000,
        selectors: {
          value_seconds: ".clock-seconds .val",
          canvas_seconds: "canvas-seconds",
          value_minutes: ".clock-minutes .val",
          canvas_minutes: "canvas-minutes",
          value_hours: ".clock-hours .val",
          canvas_hours: "canvas-hours",
          value_days: ".clock-days .val",
          canvas_days: "canvas-days",
        },
        seconds: {
          borderColor: $(".type-seconds").attr("data-border-color"),
          borderWidth: "5",
        },
        minutes: {
          borderColor: $(".type-minutes").attr("data-border-color"),
          borderWidth: "5",
        },
        hours: {
          borderColor: $(".type-hours").attr("data-border-color"),
          borderWidth: "5",
        },
        days: {
          borderColor: $(".type-days").attr("data-border-color"),
          borderWidth: "5",
        },
      });
    }
  };

  /* Rest of your existing functions (unchanged) */
  var handleCustomScroll = function () {
    /* all available option parameters with their default values */
    if ($(".content").length > 0) {
      if ($(".content").hasClass("scroll-off")) {
        if (screenWidth > 900) {
          $(".content").mCustomScrollbar({
            setWidth: false,
            setHeight: false,
            axis: "y",
          });
        }
      } else {
        $(".content").mCustomScrollbar({
          setWidth: false,
          setHeight: false,
          axis: "y",
        });
      }
    }
  };

  var kenburnSlider = function () {
    if ($("#kenburn").length > 0) {
      $("#kenburn").slippry({
        transition: "kenburns",
        useCSS: true,
        speed: 8000,
        pause: 3000,
        auto: true,
        preload: "visible",
        autoHover: false,
      });
    }
  };

  var parallex = function () {
    if ($(".parallax").length > 0) {
      var currentX = "";
      var currentY = "";
      var movementConstant = 0.015;
      $(document).on("mousemove", function (e) {
        if (currentX == "") currentX = e.pageX;
        var xdiff = e.pageX - currentX;
        currentX = e.pageX;
        if (currentY == "") currentY = e.pageY;
        var ydiff = e.pageY - currentY;
        currentY = e.pageY;
        $(".parallax div").each(function (i, el) {
          var movement = (i + 1) * (xdiff * movementConstant);
          var movementy = (i + 1) * (ydiff * movementConstant);
          var newX = $(el).position().left + movement;
          var newY = $(el).position().top + movementy;
          $(el).css("left", newX + "px");
          $(el).css("top", newY + "px");
        });
      });
    }
  };

  var handleSideBarMenu = function () {
    jQuery(".openbtn").on("click", function () {
      jQuery(".about-sidebox").addClass("active");
    });
    jQuery(".closebtn").on("click", function () {
      jQuery(".about-sidebox").removeClass("active");
    });
    jQuery(".map-btn").on("click", function () {
      jQuery(".map-box").toggleClass("active");
    });

    jQuery(".menu-btn a").on("click", function () {
      jQuery(".menuleft").addClass("active");
    });
    jQuery(".menuleft .close-btn").on("click", function () {
      jQuery(".menuleft").removeClass("active");
    });
  };

  /* WOW ANIMATION ============ */
  var wow_animation = function () {
    if ($(".wow").length > 0) {
      var wow = new WOW({
        boxClass: "wow", // animated element css class (default is wow)
        animateClass: "animated", // animation css class (default is animated)
        offset: -200, // distance to the element when triggering the animation (default is 0)
        mobile: false, // trigger animations on mobile devices (true is default)
      });
      wow.init();
    }
  };

  var handlePlaceholderAnimation = function () {
    if (jQuery(".dezPlaceAni").length) {
      $(".dezPlaceAni input, .dezPlaceAni textarea").on("focus", function () {
        $(this).parents(".form-group, .news-box").addClass("focused");
      });

      $(".dezPlaceAni input, .dezPlaceAni textarea").on("blur", function () {
        var inputValue = $(this).val();
        if (inputValue == "") {
          $(this).removeClass("filled");
          $(this).parents(".form-group, .news-box").removeClass("focused");
        } else {
          $(this).addClass("filled");
        }
      });
    }
  };

  /* Masonry Box ============ */
  var masonryBox = function () {
    /* masonry by */
    if (jQuery("#masonry, .masonry").length) {
      var self = $("#masonry, .masonry");
      if (jQuery(".card-container").length) {
        self.imagesLoaded(function () {
          self.masonry({
            gutterWidth: 15,
            isAnimated: true,
            itemSelector: ".card-container",
          });
        });
      }
    }
    /* masonry by end */
  };

  /* Light Gallery ============ */
  var lightGallery = function () {
    if ($("#lightgallery, .lightgallery").length > 0) {
      $("#lightgallery, .lightgallery").lightGallery({
        selector: ".check-km",
        loop: true,
        thumbnail: true,
        exThumbImage: "data-exthumbimage",
      });
    }
  };

  var reposition = function () {
    var modal = jQuery(this),
      dialog = modal.find(".modal-dialog");
    modal.css("display", "block");

    /* Dividing by two centers the modal exactly, but dividing by three 
		 or four works better for larger screens.  */
    dialog.css(
      "margin-top",
      Math.max(0, (jQuery(window).height() - dialog.height()) / 2)
    );
  };

  var heartBlast = function () {
    $(".heart").on("click", function () {
      $(this).toggleClass("heart-blast");
    });
  };

  var handleDzformScroll = function () {
    if (jQuery(".dzform-scroll").length > 0) {
      const qs = new PerfectScrollbar(".dzform-scroll");
      qs.isRtl = false;
    }
  };

  /* Function ============ */
  return {
    init: function () {
      heartBlast();
      masonryBox();
      handleDzformScroll();
      wow_animation();
      handleCountDown(WebsiteLaunchDate); // Initialize 45-day countdown
      handleFinalCountDown(); // Optional (if using final_countdown)
      handleSideBarMenu();
      handlePlaceholderAnimation();
      lightGallery();
      kenburnSlider();
      parallex();
      $(".modal").on("show.bs.modal", reposition);
    },
    load: function () {
      handleCustomScroll();
    },
    resize: function () {
      setTimeout(function () {
        masonryBox();
      }, 2000);
      handleFinalCountDown();
    },
  };
})();

/* Document.ready Start */
$(document).ready(function () {
  Frost.init();
});

/* Window Resize START */
$(window).on("resize", function () {
  Frost.resize();
});

/* Window Load START */
$(window).on("load", function () {
  Frost.load();
  setTimeout(function () {
    $("#loading-area").remove();
  }, 0);
});
