/* Minification failed. Returning unminified contents.
(12,1): run-time error CSS1019: Unexpected token, found '$'
(12,2): run-time error CSS1019: Unexpected token, found '('
(12,12): run-time error CSS1031: Expected selector, found '('
(12,12): run-time error CSS1025: Expected comma or open brace, found '('
(113,2): run-time error CSS1019: Unexpected token, found ')'
(120,1): run-time error CSS1019: Unexpected token, found '$'
(120,2): run-time error CSS1019: Unexpected token, found '('
(120,12): run-time error CSS1031: Expected selector, found '('
(120,12): run-time error CSS1025: Expected comma or open brace, found '('
(140,2): run-time error CSS1019: Unexpected token, found ')'
(142,1): run-time error CSS1019: Unexpected token, found '$'
(142,2): run-time error CSS1019: Unexpected token, found '('
(142,11): run-time error CSS1031: Expected selector, found ')'
(142,11): run-time error CSS1025: Expected comma or open brace, found ')'
(192,2): run-time error CSS1019: Unexpected token, found ')'
(198,1): run-time error CSS1019: Unexpected token, found '$'
(198,2): run-time error CSS1019: Unexpected token, found '('
(198,11): run-time error CSS1031: Expected selector, found ')'
(198,11): run-time error CSS1025: Expected comma or open brace, found ')'
(276,2): run-time error CSS1019: Unexpected token, found ')'
 */
/* =====================================================================================================
   JS File Index:
   1. Header Modules
   2. Footer Modules
   3. Trending Products Modile
   ===================================================================================================== */

/* =====================================================================================================
   1. Header Modules
   ===================================================================================================== */

$(function () {
    $(".search-button").on("click", function () {
        if ($(window).width() < 768) {
            $(".search-button").show();
        } else {
            $(".search-button").hide();
        }
        $(".search-bar-container").fadeIn(100);
    })

    function closeSearchBarContainer() {
        $(".search-bar-container").fadeOut(100, function () {
            if (!($(window).width() < 768)) {
                $(".search-button").show();
            }
        });
    }

    $(".search-bar-remove").on("click", function () {
        closeSearchBarContainer();
    })

    $(window).on("orientationchange", function () {
        adjustSidebarHeight();
    })


    function adjustSidebarHeight() {
        // Calculate the SidenavHeight height
        let SidenavHeight = $(window).height() - $("header").outerHeight();
        // Set the sidebar height dynamically
        $(".sidenav").css({
            height: `${SidenavHeight}px`
        });
    }

    $(window).resize(function () {
        if ($(".search-bar-container").is(":visible") && $(window).width() < 768) {
            $(".search-button").show();
        } else if ($(".search-bar-container").is(":visible") && $(window).width() >= 768) {
            $(".search-button").hide();
        }

        //Close the sidebar when the window width is greater than or equal to 768px
        if ($(window).width() >= 768) {
            closeSidebar();
        }

        adjustSidebarHeight();
    })

    //Function to open sidebar
    function openSidebar() {
        $(".overlay-sidebar").css({ "display": "block" });
        $("#mySidenav").css({ "width": "55vw" });
        $("body").css({ "overflow": "hidden" });
        $(".mobile-menu-button i").removeClass("bi bi-list").addClass("bi bi-x");
        $('header').addClass("open-sidebar");
        $(window).scrollTop(0);
    }

    //Function to close sidebar
    function closeSidebar() {
        $(".overlay-sidebar").css({ "display": "none" });
        $("#mySidenav").css({ "width": "0px" });
        $("body").css({ "overflow": "" });
        $('header').removeClass("open-sidebar");
        $(".mobile-menu-button i").removeClass("bi bi-x").addClass("bi bi-list");
    }

    // Onclick to mobile menu button open or close the sidebar
    $(".mobile-menu-button").on("click", function () {
        if ($("#mySidenav").width() > 0) {
            closeSidebar();
        } else {
            openSidebar();
        }
        adjustSidebarHeight();
    })

    $('body').on('click', function (event) {
        // If the click is outside the sidebar and the sidebar is open, then close the sidebar
        if (!$(event.target).closest("#mySidenav").length && $("#mySidenav").width() > 0) {
            closeSidebar();
        }
        // If the click is outside the search-bar-container and the search-bar-container is visible, then close the search-bar-container
        else if (!$(event.target).closest(".search-bar-container , .search-button").length && $(".search-bar-container").is(":visible") && $(window).width() < 768) {
            closeSearchBarContainer();
        }
    });

    //Onclick to close top panel hide the top header
    $(".close-top-panel").on("click", function () {
        sessionStorage.setItem("header-top-closed", true)
        $(".header-top").hide();
    })

    if (!sessionStorage.getItem("header-top-closed")) {
        $(".header-top").show();
    }

})


/* =====================================================================================================
   2. Footer Modules
   ===================================================================================================== */

$(function () {
    $('.collapsible-accordion div.btn.btn-link').on('click', function () {
        var icon = $(this).find('div.bi');
        var content = $(this).next(".footer-links.accordion-content");
        if (content.hasClass("open")) {
            content.css("height", "0px");
            content.removeClass("open");
            content.css("height", "");
            icon.removeClass('bi-chevron-up').addClass('bi-chevron-down');
        } else {
            $(".footer-links.accordion-content").not(content).removeClass("open");
            var contentHeight = content.prop('scrollHeight');
            content.css("height", contentHeight);
            content.addClass("open");
            icon.removeClass('bi-chevron-down').addClass('bi-chevron-up');
            $(".footer-links.accordion-content").not(content).css("height", "0px");
            $(".footer-links.accordion-content").not(content).css("height", "");
            $(".collapsible-accordion div.btn.btn-link> div.bi").not(icon).removeClass("bi-chevron-up").addClass("bi-chevron-down")
        }
    });
})

$(document).ready(function () {
    function ajaxCallForWatching(listingId, ajaxCallUrl, buttonreference) {
        try {
            if (listingId && ajaxCallUrl) {
                $.ajax({
                    url: ajaxCallUrl,
                    type: "GET",
                    data: { id: listingId },
                    success: function (data) {
                    },
                    error: function (error) {
                        console.error(error);
                    },
                    complete: function () {
                        buttonreference.prop("disabled", false);
                    }
                });
            }
        }
        catch (e) {
            console.error(e);
        }
    }
    $(document).on("click", ".addRemoveWatchList", function (event) {
        event.preventDefault();
        let isauthenticated = aweUserName;
        let btn = $(this);
        let listingId = $(btn).closest("[data-watch-listingid]").attr("data-watch-listingid");
        if (listingId) {
            if (!isauthenticated) {
                const watchListObject = { "ListingId": listingId };
                $.cookie("addToWatchListData", JSON.stringify(watchListObject), { path: "/", expires: 1 });
                const currentWindowUrl = window.location.href;
                const returnUrl = new URL(currentWindowUrl).pathname + new URL(currentWindowUrl).search;
                window.location.href = "/Account/LogOn?returnUrl=" + encodeURIComponent(returnUrl);
            }
            else {
                btn.prop('disabled', true);
                // if watching then call remove from watchlist
                if (btn.hasClass("userWatching")) {
                    ajaxCallForWatching(listingId, "Listing/RemoveWatchAsync", btn);
                }
                // remove from watchlist
                else if (btn.hasClass("notWatching")) {
                    ajaxCallForWatching(listingId, "Listing/AddWatchAsync", btn);
                }
            }
        }

    });
});

/* =====================================================================================================
   3. Trending Products Modile
   ===================================================================================================== */

$(document).ready(function () {
    //Slick Slider Initialization
    var slickSlider = $('.trending-products-cards.slick-slider');

    if (slickSlider.length) {
        slickSlider.slick({
            dots: false,
            infinite: false,
            slidesToShow: 4,
            slidesToScroll: 1,
            responsive: [
                {
                    breakpoint: 1260,
                    settings: {
                        slidesToShow: 3,
                        slidesToScroll: 1
                    }
                },
                {
                    breakpoint: 1050,
                    settings: {
                        slidesToShow: 2,
                        slidesToScroll: 1
                    }
                },
                {
                    breakpoint: 575,
                    settings: {
                        slidesToShow: 1,
                        slidesToScroll: 1
                    }
                }
            ]
        });
    }


    // Chips for filters
    $(document).on("click", ".removeChip", function (event) {
        let value = $(this).data("value");
        let property = $(this).data("property");
        let isBooleanField = $(this).data("boolean");
        let isPriceProperty = $(this).data("high-low-price");
        let isCategory = $(this).data("category");
        let isRegion = $(this).data("region");
        let isBuyNowMakeOffer = $(this).data("buynow-makeoffer")
        let propertyExists = false;
        if (isBuyNowMakeOffer) {
            $(`input[name="buyNowOrOffer"][value="all"]`).prop("checked", true);
            propertyExists = true;
        }
        else if (property && isBooleanField) {
            $(`input[name="${property}"]`).val("");
            propertyExists = true;
        }
        else if (property && value) {
            $(`input[name="${property}"][value="${value}"]`).prop("checked", false);
            propertyExists = true;
        }
        else if (isPriceProperty) {
            $("#PriceLow").val("");
            $("#PriceHigh").val("");
            propertyExists = true;
        }
        else if (isCategory) {
            $("#CategoryID").val(9);
            propertyExists = true;
        }
        else if (isRegion) {
            $("#RegionID").val("");
            propertyExists = true;
        }

        if (propertyExists) {
            $(this).closest(".chip").remove();
            $("#customSubmitForm").submit();
        }
    });
});
