// Script Property of MileOne Autogroup. Contact Marketing before attempting to make changes.

// ------------------------------  LET IT SNOW  ------------------------------ //

// Define the excluded domains

const moExcludeDomains = [
    "www.hallauto.com",
    "www.hallfreshstart.com",
    "www.hallacuravirginiabeach.com",
    "www.hallchryslerjeep.net",
    "www.hallhonda.com",
    "www.hallhondaelizabethcity.com",
    "www.hallhyundaielizabethcity.com",
    "www.hallfordelizabethcity.com",
    "www.hallmazda.com",
    "www.hallnissanofvirginiabeach.com",
    "www.halltoyotaelizabethcity.com",
    "www.halltoyotavirginiabeach.com",
    "www.heritagehyundaibaltimore.com",
    "www.lexusofwilmington.com",
    "www.motorworldgrouphyundai.com",
    "www.motorworldlexus.com",
    "www.genesisofowingsmills.com",
    "www.genesisofwilkesbarre.com",
    "www.bmwofcatonsville.com",
    "www.bmwoffortwashington.com",
    "www.bmwofsilverspring.com",
    "www.porschesilverspring.com",
    "www.mileonejobs.com",
    "www.mileonecares.com"
];

const moCurrentDomain = window.location.hostname;
const moCurrentPage = window.location.pathname; 
const moCurrentURL = window.location.href; 

// Define the start and end dates for the functionality
const moStartDate = new Date("2024-12-02T08:30:00");
const moEndDate = new Date("2025-02-24T00:00:00");
const moCurrentDate = new Date();

let moActive = true;
let moLetItSnow = false;

if (moCurrentDate >= moStartDate && moCurrentDate <= moEndDate) {
    moLetItSnow = true;
}

if (!moExcludeDomains.includes(moCurrentDomain) && moLetItSnow && moActive) {
    if (document.readyState === "loading") {
        // Wait for DOM to finish loading
        document.addEventListener("DOMContentLoaded", moSnowStorm);
    } else {
        // DOMContentLoaded has already fired
        moSnowStorm();
    }
} else {
    console.error(
        "Snow functionality is not allowed on this domain or is outside the allowed date range."
    );
}

// Function to dynamically load the script
function moSnowStorm() {
    const script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "https://mc7ll47vh9gf4ns71pxlm0-kb03q.pub.sfmc-content.com/xp22lr2tnjo";
    document.head.appendChild(script);
}

//  ------------------------------  DYNAMIC EXP DATE LOADER ------------------------------  //
  
setTimeout(function(){
  
  const moExpScript = document.createElement("script");
  moExpScript.src = "https://mc7ll47vh9gf4ns71pxlm0-kb03q.pub.sfmc-content.com/5yjwasjk401";
  document.body.appendChild(moExpScript);
                                                              
}, 150);
                                                              
//  ------------------------------  INTERNAL STYLING LOADER  ------------------------------  //


        function addCssLink(cssUrl) {
            if (cssUrl) {
                var file = document.createElement("link");
                file.setAttribute("rel", "stylesheet");
                file.setAttribute("type", "text/css");
                file.setAttribute("href", cssUrl);
                document.head.appendChild(file);
            }
        }
        function loadLOWButton(url, callback, specificURL) {
                                                              
          if (window.location.href === specificURL) {
            var script = document.createElement("script");
            script.type = "text/javascript";
            script.src = url;
            document.body.appendChild(script);
          }
        }
                                                              
        var currentURL = window.location.href;
        var domain = currentURL.match(/:\/\/(www\.)?(.[^/]+)/)[2];

        if (domain.includes("mazda")) {
            addCssLink("https://mc7ll47vh9gf4ns71pxlm0-kb03q.pub.sfmc-content.com/rglfa55pnqs");
        } else if (domain.includes("audi")) {
            addCssLink("https://mc7ll47vh9gf4ns71pxlm0-kb03q.pub.sfmc-content.com/z1vyrvww22i");
        } else if (domain.includes("bmw")) {
            addCssLink("https://mc7ll47vh9gf4ns71pxlm0-kb03q.pub.sfmc-content.com/ujfqgz05jlx");
        } else if (domain.includes("genesis")) {
            addCssLink("https://mc7ll47vh9gf4ns71pxlm0-kb03q.pub.sfmc-content.com/e44adzirjtg");
        } else if (domain.includes("nissan")) {
            addCssLink("https://mc7ll47vh9gf4ns71pxlm0-kb03q.pub.sfmc-content.com/1w5lib2sdgi");
        } else if (domain.includes("heritagevw")) {
            addCssLink("https://mc7ll47vh9gf4ns71pxlm0-kb03q.pub.sfmc-content.com/erscyrvg5ei");
        } else if (domain.includes("wilmington")) {
                                                              
            loadLOWButton('https://mc7ll47vh9gf4ns71pxlm0-kb03q.pub.sfmc-content.com/31sk3ao3jle', null, 'https://www.lexusofwilmington.com/performance-protection/index.htm');
            addCssLink("https://mc7ll47vh9gf4ns71pxlm0-kb03q.pub.sfmc-content.com/pg3d1yfrf1c");

        } else if (domain.includes("mercedes")) {
            addCssLink("https://mc7ll47vh9gf4ns71pxlm0-kb03q.pub.sfmc-content.com/15yhzztwtih");
        } else {
            addCssLink("https://mc7ll47vh9gf4ns71pxlm0-kb03q.pub.sfmc-content.com/bhhw4vv2lnf");
        }

                                                           

//  ------------------------------  SCROLL BUTTON CONTROLLER  ------------------------------  //


        var scrollToTopBtn = document.querySelector(".mo-scrollToTopBtn");
        var rootElement = document.documentElement;

        function handleScroll() {
            // Do something on scroll
            var scrollTotal = rootElement.scrollHeight - rootElement.clientHeight;
            if (rootElement.scrollTop / scrollTotal > 0.7) {
                // Show button
                scrollToTopBtn.classList.add("mo-showBtn");
            } else {
                // Hide button
                scrollToTopBtn.classList.remove("mo-showBtn");
            }
        }

        function scrollToTop() {
            // Scroll to top logic
            rootElement.scrollTo({
                top: 0,
                behavior: "smooth"
            });
        }

        scrollToTopBtn.addEventListener("click", scrollToTop);
        document.addEventListener("scroll", handleScroll);


//  ------------------------------  TABBED WIDGET CONTROLLER  ------------------------------  //

        $(function () {
            var activeIndex = $('.active-tab').index(),
                $contentlis = $('.tabs-content li'),
                $tabslis = $('.tabs li');

            // Show content of active tab on load
            $contentlis.eq(activeIndex).show();

            $('.tabs').on('click', 'li', function (e) {
                var $current = $(e.currentTarget),
                    index = $current.index();

                $tabslis.removeClass('active-tab');
                $current.addClass('active-tab');
                $contentlis.hide().eq(index).show();
            });
        });
