var
    _settingObj = {
        forAllMatch: !0,
        forAllMatchCookie: "forAllMatchCheck",
        allMatchSetting: {
            goalWindow: !0,
            redWindow: !0,
            homeSoundId: 0,
            awaySoundId: 1,
            goalWindowCookie: "goalWindowCheck",
            redWindowCookie: "redWindowCheck",
            closeSound: function () {
                this.homeSoundId = 4;
                this.awaySoundId = 4;
            },
        },
        favMatchSetting: {
            goalWindow: !0,
            redWindow: !0,
            homeSoundId: 0,
            awaySoundId: 1,
            goalWindowCookie: "goalWindowCheck",
            redWindowCookie: "redWindowCheck",
            closeSound: function () {
                this.homeSoundId = 4;
                this.awaySoundId = 4;
            },
        },
        page: {
            all: {
                url: '/home',
                goalWindowCheck: !0,
                redCardWindowCheck: !0,
                homeSoundCheck: 0,
                awaySoundCheck: 1,
                rankCheck: !0,
                remarkCheck: !0,
                yellowCardCheck: !0,
                goalWindowCheckCookie: "goalWindowCheckAll",
                redCardWindowCheckCookie: "redCardWindowCheckAll",
                rankCheckCookie: "rankCheckAll",
                yellowCardCheckCookie: "yellowCardCheckAll",
                remarkCheckCookie: "remarkCheckAll",
                homeSoundCheckCookie: "homeSoundCheckAll",
                awaySoundCheckCookie: "awaySoundCheckAll",
                closeSound: function () {
                    this.homeSoundCheck = 4;
                    this.awaySoundCheck = 4;
                },
            },
            favorites: {
                url: '/yeu-thich',
                goalWindowCheck: !0,
                redCardWindowCheck: !0,
                homeSoundCheck: 0,
                awaySoundCheck: 1,
                rankCheck: !0,
                remarkCheck: !0,
                yellowCardCheck: !0,
                goalWindowCheckCookie: "goalWindowCheckFav",
                redCardWindowCheckCookie: "redCardWindowCheckFav",
                rankCheckCookie: "rankCheckFav",
                yellowCardCheckCookie: "yellowCardCheckFav",
                remarkCheckCookie: "remarkCheckFav",
                homeSoundCheckCookie: "homeSoundCheckFav",
                awaySoundCheckCookie: "awaySoundCheckFav",
                closeSound: function () {
                    this.homeSoundCheck = 4;
                    this.awaySoundCheck = 4;
                },
            },
            results: {
                url: '/ket-qua',
                remarkCheck: !0,
                yellowCardCheck: !0,
                yellowCardCheckCookie: "yellowCardCheckRes",
                remarkCheckCookie: "remarkCheckRes",
            },
            fixture: {
                url: '/lich-thi-dau',
                yellowCardCheck: !0,
                yellowCardCheckCookie: "yellowCardCheckFix",
            },
            settingPhone: {
                goalWindowCheck: !0,
                redCardWindowCheck: !0,
                homeSoundCheck: 0,
                awaySoundCheck: 1,
                rankCheck: !0,
                remarkCheck: !0,
                yellowCardCheck: !0,
                goalWindowCheckCookie: "goalWindowCheckPhone",
                redCardWindowCheckCookie: "redCardWindowCheckPhone",
                rankCheckCookie: "rankCheckPhone",
                yellowCardCheckCookie: "yellowCardCheckPhone",
                remarkCheckCookie: "remarkCheckPhone",
                homeSoundCheckCookie: "homeSoundCheckPhone",
                awaySoundCheckCookie: "awaySoundCheckPhone",
                closeSound: function () {
                    this.homeSoundCheck = 4;
                    this.awaySoundCheck = 4;
                },
            },
        },
        rank: !0,
        rankCookie: "TeamOrderCheck",
        yellowCard: !0,
        yellowCardCookie: "YellowCheck",
        remark: !0,
        remarkCookie: "ShowRemarkCheck",
        redCard: !0,
        oddsTrend: !0,
        showPanLu: !0,
        showCorner: !0,
        isFavTop: !0,
    };


document.addEventListener('DOMContentLoaded', function () {
    const dropdownToggle = document.querySelector('.dropdown-toggle');
    const dropdownMenu = document.querySelector('.dropdown-menu');
});
$(function () {
    const button = $('#openSettingModal');
    const modal = $('#settingsModal');
    const close = $('.close-btn');


    button.click(function () {
        const buttonOffset = button.offset();  // Get the button's position

        $("#settingselect .tab-button[data-tab='" + checkTakeNameAction() + "']").trigger("click");
        // initSettingOnMenu();

        // Set the modal's position to be the same as the button's position
        modal.css({
            'position': 'absolute', 'top': 40,  // Position below the button
            'left': buttonOffset.left - 203  // Position to the left of the button
        });

        // Toggle the modal visibility
        if (modal.css('display') === 'none') {
            modal.show();
        } else {
            modal.hide();
        }
    });

    $(window).on('resize', function () {
        const buttonOffset = button.offset();  // Get the button's position

        // Set the modal's position to be the same as the button's position
        modal.css({
            'position': 'absolute', 'top': 40,  // Position below the button
            // 'left': buttonOffset.left - 203  // Position to the left of the button
        });
    });

    $(window).click(function (event) {
        if (!$(event.target).closest('#settingsModal').length && event.target.id !== 'openSettingModal') {
            modal.hide();
        }
    });

    $('.tab-button').on('click', function () {
        var tabId = $(this).data('tab');

        $('.tab-button').removeClass('on');
        $(this).addClass('on');

        $('.tab-content').removeClass('on');
        $('#' + tabId).addClass('on');

        initSettingOnMenu();
    });
    let arrMenu = [
        ['gamerate.index', 'gamerate.detail'],
        ['football.comm', 'football.index'],
        ['basketball.coming-soon'],
        ['tennis.coming-soon'],
        ['baseball.coming-soon'],
        [],
    ];

    var activeIndex = $('#menuMainSw').data('active');

    var indexx = arrMenu.findIndex(function (routeNames) {
        return routeNames.some(item => item.includes(activeIndex));
    });

    let header = $('#header-wrapper-sp');
    let sticky = 150;

    $(window).scroll(function () {
        if (window.pageYOffset > sticky) {
            header.addClass('sticky');
        } else {
            header.removeClass('sticky');
        }
    });


    const dropdownToggle = $('.dropdown-toggle');
    const dropdownMenu = $('.dropdown-menu');

    $(document).on('click', '.dropdown-toggle', function (event) {
        event.stopPropagation();
        if ($(this).hasClass('show')) {
            dropdownToggle.removeClass('show')
            dropdownMenu.removeClass('show')
        } else {
            dropdownToggle.removeClass('show')
            dropdownMenu.removeClass('show')
            $(this).addClass('show')
            $(this).next('.dropdown-menu').addClass('show')
        }
    })


    const sixinonedropdown = $('.sixinone-dropdown');
    const selectcustom = $('.select-custom.selectDate');

    $(window).click(function (event) {
        if (selectcustom.hasClass('open')) {
            sixinonedropdown.addClass('show')
        } else {
            sixinonedropdown.removeClass('show')
        }
    });

    $(document).click(function () {
        dropdownMenu.removeClass('show')
        dropdownToggle.removeClass('show')
    })
    $('.openSettingSwiper').click(function () {
        $('.settingBar').toggleClass('closed');
        initSettings();
    });
    $(document).on('click', '.soundSetting span', function () {
        $(this).parents('.soundSetting').find('span').removeClass('on');
        $(this).addClass('on')
    });

    var eventFired = 0;
    $(window).on('resize load', function () {
        if (!eventFired) {
            if ($(window).width() <= 1000) {
                jQuery("#darkModeItem")
                    .detach()
                    .appendTo('#spSetting');
                jQuery("#chooseOddsType")
                    .detach()
                    .appendTo('#spSettingTL');

                jQuery("#chooseTimeZone")
                    .detach()
                    .appendTo('#spSettingTZ');
            } else {
                jQuery("#darkModeItem")
                    .detach()
                    .appendTo('#darkModeItemParent');
                jQuery("#chooseOddsType")
                    .detach()
                    .appendTo('#OddsTypeItemParent');
                jQuery("#chooseTimeZone")
                    .detach()
                    .appendTo('#TimeZoneItemParent');

            }
        }
    });
    $(document).on('click', '.custom-select', function () {
        if (!$(this).hasClass('only-cus')) {
            $('.custom-options').removeClass('show');
            const el = $(this).parents('.custom-select-02').find('.custom-options');
            if (el.hasClass('show')) el.removeClass('show');
            else el.addClass('show');
        }
    });
    $(document).on('click', '.custom-select3', function () {
        if (!$(this).hasClass('only-cus')) {
            $('.custom-options').removeClass('show');
            const el = $(this).parents('.custom-select-02').find('.custom-options');
            if (el.hasClass('show')) el.removeClass('show');
            else el.addClass('show');
        }
    });

    $(document).on('mouseenter', '.custom-options', function () {
        $(this).find('.custom-option.selected').removeClass('selected');
    });

    $(document).on('click', '.custom-option', function () {
        if (!$(this).hasClass('only-cus')) {
            const el = $(this).parents('.custom-select-02').find('.custom-option');
            el.removeClass('selected');
            $(this).parents('.custom-select-02').find('.custom-select').text($(this).text());
            $(this).addClass('selected');
        }
    });

    $(document).on('click', '.custom-option, .custom-options div', function () {
        $('.custom-options').removeClass('show');
    });

    window.addEventListener('mouseup', function (event) {
        var elements = document.getElementsByClassName('custom-select-02');
        var isInside = false;

        for (var i = 0; i < elements.length; i++) {
            if (event.target === elements[i] || elements[i].contains(event.target)) {
                isInside = true;
                break;
            }
        }

        if (!isInside) {
            $('.custom-options').removeClass('show');
            $('.custom-select-02').removeClass('show');
        }

        syncParentShowClass();
    });

    $(document).on('click', function (e) {
        if (e.target.className !== 'secont-half text-center w-100-i' && !e.target.id.includes('sbOddsCorner_')) {
            if (!$(e.target).closest('.livetab-corner').length) {
                $('.livetab-corner').css({
                    visibility: 'hidden',
                    display: 'none'
                });
            }
        }

        // logic click outside popup filter home page
        if (!$(e.target).closest('#showFilterBox').length && !$(e.target).closest('#showoptional2').length && !e.target.className.includes('theme-style-icon') && !(e.target.id === 'DarkModeCheck')) {
            // popup league
            if (e.target.id !== 'LeaSelBtn' && e.target.id !== 'li_FilterLea' && e.target.id !== 'LeaSelBtnImg') {
                $('#showoptional2').css({
                    visibility: 'hidden',
                });
                if ($('#showoptional2 input[name="selectAllLeague"]').is(":checked")) {
                    $('#li_FilterLea').removeClass('active on');
                }
            }
            // popup rate
            if (e.target.id !== 'CdiSelBtn' && e.target.id !== 'li_FilterCdi' && e.target.id !== 'CdiSelImg') {
                $('#showFilterBox').css({
                    visibility: 'hidden',
                });
                if ($('#showFilterBox input[name="selectAllLeague"]').is(":checked")) {
                    $('#li_FilterCdi').removeClass('active');
                }
            }
            // popup by league
            if (e.target.id !== 'dropdown-toggle' && e.target.id !== 'li_FilterTimes') {
                $('#li_FilterTimes').removeClass('active');
                $('#dropdown-toggle').removeClass('show');
                $('#dropdown-menu').removeClass('show');
            }

            
            if (e.target.id !== 'CompanySelMobileDropdown-toggle' && e.target.id !== 'li_FilterBet') {
                $('#li_FilterBet').removeClass('active');
                $('#CompanySelMobileDropdown-toggle').removeClass('show');
                $('#CompanySelMobileDropdown-menu').removeClass('show');
            }
        }
    });

    // popup league
    $(document).on('click', '#li_FilterLea', function () {
        $('#showoptional2').css('visibility', function (i, visibility) {
            return visibility === 'visible' ? 'hidden' : 'visible';
        });
        $('#li_FilterLea').toggleClass('active on');
    });

    // popup rate
    $(document).on('click', '#li_FilterCdi', function () {
        $('#showFilterBox').css('visibility', function (i, visibility) {
            return visibility === 'visible' ? 'hidden' : 'visible';
        });
        $('#li_FilterCdi').toggleClass('active');

    });

    // popup order by
    // $(document).on('click', '#li_FilterTimes, #dropdown-toggle', function () {
    //     $('#li_FilterTimes').toggleClass('active');
    //     $('#dropdown-toggle').toggleClass('show');
    //     $('#dropdown-menu').toggleClass('show');
        
    // });

    // popup select company mobile
    // $(document).on('click', '#li_FilterBet, #CompanySelMobileDropdown-toggle', function () {
    //     $('#li_FilterTimes').toggleClass('active');
    //     $('#CompanySelMobileDropdown-toggle').toggleClass('show');
    //     $('#CompanySelMobileDropdown-menu').toggleClass('show');
    // });


    function syncParentShowClass() {
        $('.custom-options').each(function () {
            const parent = $(this).parents('.custom-select-02');
            if ($(this).hasClass('show')) {
                parent.addClass('show');
            } else {
                parent.removeClass('show');
            }
        });
    }


    // Gọi hàm sau khi các sự kiện thay đổi trạng thái của .custom-options
    $(document).on('click', '.custom-select', function () {
        if (!$(this).hasClass('only-cus')) {
            const parent = $(this).parents('.custom-select-02');
            const options = parent.find('.custom-options');

            if (parent.hasClass('show') && options.hasClass('show')) {
                // Nếu cả parent và options đều có class 'show', thì đóng chúng lại
                parent.removeClass('show');
                options.removeClass('show');
            } else {
                // Nếu chưa mở, ẩn tất cả các menu khác và mở menu hiện tại
                $('.custom-options').removeClass('show');
                $('.custom-select-02').removeClass('show');
                options.addClass('show');
                parent.addClass('show');
            }
        }
    });


    $(document).on('click', '.custom-option, .custom-options div', function () {
        syncParentShowClass();
    });


    const ActionSettingObject = {
        rankCheck: (val) => {
            if (val) {
                const elementsButton = document.querySelectorAll('[id^="horder_"]');
                elementsButton.forEach(element => {
                    element.classList.remove('hidden-element');
                });
                return;
            }
            const elementsButton = document.querySelectorAll('[id^="horder_"]');
            elementsButton.forEach(element => {
                element.classList.add('hidden-element');
            });
        },
        yellowCardCheck: (val) => {
            if (val) {
                $(".yellowcard").show()
                return;
            }
            $(".yellowcard").hide()
        },
        redCardWindowCheck: (val) => {
            if (val) {
                $(".redcard").show()
                return;
            }
            $(".redcard").hide()
        },

    }

    function checkTakeAction(page) {
        let takeAction = true;
        if (page != "settingPhone"){
            const currentURL = window.location.href;
            if (currentURL.indexOf(_settingObj.page[page].url) === -1) {
                takeAction = false;
            }
            if(page == 'all'){
                takeAction = true;
                for (const key in _settingObj.page) {
                   const val = _settingObj.page[key];
                    if (currentURL.indexOf(val.url) !== -1) {
                        takeAction = false;
                        break;
                    }
                }
            }
        }

        return takeAction;
    }

    $(".goalWindowCheck, .redCardWindowCheck, .rankCheck, .remarkCheck, .yellowCardCheck").click(function () {
        const page = $(this).closest('.boxTabSetting').attr('id');
        const val = $(this).is(":checked");
        const data = $(this).data('event');
        _settingObj.page[page][data] = val;

        writeCookie(_settingObj.page[page][`${data}Cookie`], val ? 1 : 0);

        if (!checkTakeAction(page)) return;

        const action = ActionSettingObject[data];
        action && action(val);
    });

    $(".homeSoundCheck span, .awaySoundCheck span").click(function () {
        const page = $(this).closest('.boxTabSetting').attr('id');
        const selectbox = $(this).closest('.selectbox');
        const data = selectbox.data('event');
        const val = $(this).data("value");

        _settingObj.page[page][data] = val;
        writeCookie(_settingObj.page[page][`${data}Cookie`], val);

        selectbox.find('.on').removeClass('on');
        $(this).addClass('on');

        if (!checkTakeAction(page)) {
            soundInitOtherPage(page, data);

            if (val != 4) {
                _$(`audio_other_${data}`).play();
            }
            return;
        }

        soundInitPage(page, data);

        if (val != 4) {
            _$(`audio_${data}`).play();
        }

    });


});
$(window).on('load', function () {
    // document.getElementById("sloading").style.display = "none";
    var oddsTypeMb = getCookie("Odds_Type");
    var timezoneMb = getCookie("Time_Zone");

});

function initSettings() {
    getCookie(_settingObj.allMatchSetting.goalWindowCookie) != null &&
    (_settingObj.allMatchSetting.goalWindow =
        getCookie(_settingObj.allMatchSetting.goalWindowCookie) == "1");
    getCookie(_settingObj.favMatchSetting.goalWindowCookie) != null &&
    (_settingObj.favMatchSetting.goalWindow =
        getCookie(_settingObj.favMatchSetting.goalWindowCookie) == "1");
    getCookie(_settingObj.allMatchSetting.redWindowCookie) != null &&
    (_settingObj.allMatchSetting.redWindow =
        getCookie(_settingObj.allMatchSetting.redWindowCookie) == "1");
    getCookie(_settingObj.favMatchSetting.redWindowCookie) != null &&
    (_settingObj.favMatchSetting.redWindow =
        getCookie(_settingObj.favMatchSetting.redWindowCookie) == "1");
    getCookie("soundCheck") != null &&
    (_settingObj.allMatchSetting.homeSoundId = getCookie("soundCheck"));
    getCookie("soundCheck2") != null &&
    (_settingObj.allMatchSetting.awaySoundId = getCookie("soundCheck2"));
    getCookie("soundCheck") != null &&
    (_settingObj.favMatchSetting.homeSoundId = getCookie("soundCheck"));
    getCookie("soundCheck2") != null &&
    (_settingObj.favMatchSetting.awaySoundId = getCookie("soundCheck2"));
    getCookie(_settingObj.forAllMatchCookie) != null &&
    (_settingObj.forAllMatch =
        getCookie(_settingObj.forAllMatchCookie) == "0");
    var n = _settingObj.allMatchSetting;
    _settingObj.forAllMatch ||
    ((n = _settingObj.favMatchSetting),
    $("#selectbox")[0] &&
    ($("#selectbox").find("span")[0].classList.remove("on"),
        $("#selectbox").find("span")[1].classList.add("on")));
    $("#goalWindowCheck")[0] &&
    ($("#goalWindowCheck")[0].checked = n.goalWindow);
    $("#redWindowCheck")[0] && ($("#redWindowCheck")[0].checked = n.redWindow);
    initSoundSelect(n.homeSoundId, "selectsound1");
    initSoundSelect(n.awaySoundId, "selectsound2");
    getCookie(_settingObj.rankCookie) != null &&
    (_settingObj.rank = getCookie(_settingObj.rankCookie) == "1");
    $("#TeamOrderCheck")[0] &&
    ($("#TeamOrderCheck")[0].checked = _settingObj.rank);
    getCookie(_settingObj.yellowCardCookie) != null &&
    (_settingObj.yellowCard =
        getCookie(_settingObj.yellowCardCookie) == "1");
    $("#YellowCheck")[0] &&
    ($("#YellowCheck")[0].checked = _settingObj.yellowCard);
    getCookie(_settingObj.remarkCookie) != null &&
    (_settingObj.remark = getCookie(_settingObj.remarkCookie) == "1");
    $("#ShowRemarkCheck")[0] &&
    ($("#ShowRemarkCheck")[0].checked = _settingObj.remark);
}

function handleCheckedSettingObject(page, param) {
    if (_settingObj.page[page][param] == null || _settingObj.page[page][param] == undefined) return;

    const val = getCookie(_settingObj.page[page][`${param}Cookie`]);
    val && (_settingObj.page[page][param] = val == "1");

    CheckedSettingObject(page, param);
}

function handleSelectedSettingObject(page, param) {
    if (_settingObj.page[page][param] == null || _settingObj.page[page][param] == undefined) return;

    const val = getCookie(_settingObj.page[page][`${param}Cookie`]);
    val && (_settingObj.page[page][param] = val)

    SelectedSoundSettingObject(page, param);
}

function CheckedSettingObject(page, param) {
    const idElement = $(`#${page} .${param}`);
    if (idElement.length < 1) return;

    const val = _settingObj.page[page][param];
    if (val == null || val == undefined) return;

    idElement.prop('checked', val);
}


function SelectedSoundSettingObject(page, param) {
    const idElement = $(`#${page} .${param}`);

    if (idElement.length < 1) return;

    const val = _settingObj.page[page][param];
    if (val == null || val == undefined) return;

    idElement.find(".on").removeClass("on");
    idElement.find('span[data-value="' + val + '"]').addClass("on");
}

function checkTakeNameAction() {
    var currentURL = window.location.href;
    let selectedTab = 'all';

    $.each(_settingObj.page, function (tabName, val) {
        const urlPart = val.url
        if (currentURL.indexOf(urlPart) !== -1) {
            selectedTab = tabName;
            return false;
        }
    });

    return selectedTab;
}

function settingHandlePage(page) {
    handleCheckedSettingObject(page, 'goalWindowCheck');
    handleCheckedSettingObject(page, 'redCardWindowCheck');
    handleCheckedSettingObject(page, 'yellowCardCheck');
    handleCheckedSettingObject(page, 'rankCheck');
    handleCheckedSettingObject(page, 'remarkCheck');
    handleSelectedSettingObject(page, 'homeSoundCheck');
    handleSelectedSettingObject(page, 'awaySoundCheck');
}

function initSettings() {
    const dataTab = $('#settingselect .tab-button.on');

    if (dataTab.length > 0) {
        const page = dataTab.data('tab');
        if (_settingObj.page[page]) {
            settingHandlePage(page);
        }
    }
    settingHandlePage("settingPhone");


    var n = _settingObj.allMatchSetting;
    _settingObj.forAllMatch || (n = _settingObj.favMatchSetting,
    $("#selectbox")[0] && ($("#selectbox").find("span")[0].classList.remove("on"),
        $("#selectbox").find("span")[1].classList.add("on")));

    $("#goalWindowCheck")[0] && ($("#goalWindowCheck")[0].checked = n.goalWindow);
    $("#redWindowCheck")[0] && ($("#redWindowCheck")[0].checked = n.redWindow);
    initSoundSelect(n.homeSoundId, "selectsound1");
    initSoundSelect(n.awaySoundId, "selectsound2");
    getCookie(_settingObj.rankCookie) != null && (_settingObj.rank = getCookie(_settingObj.rankCookie) == "1");
    $("#TeamOrderCheck")[0] && ($("#TeamOrderCheck")[0].checked = _settingObj.rank);
    getCookie(_settingObj.yellowCardCookie) != null && (_settingObj.yellowCard = getCookie(_settingObj.yellowCardCookie) == "1");
    $("#YellowCheck")[0] && ($("#YellowCheck")[0].checked = _settingObj.yellowCard);
    getCookie(_settingObj.remarkCookie) != null && (_settingObj.remark = getCookie(_settingObj.remarkCookie) == "1");
    $("#ShowRemarkCheck")[0] && ($("#ShowRemarkCheck")[0].checked = _settingObj.remark)
}

function initSettingOnMenu() {
    initSettings();
}

function CheckSound(n) {
    var i, t;
    for (_settingObj.allMatchSetting.homeSoundId = n,
             _settingObj.favMatchSetting.homeSoundId = n,
             i = $("#selectsound1").find("span"),
             t = 0; t < i.length; t++) {
        if (i[t].attributes['data-value'].value == n) {
            i[t].classList.add("on");
            continue
        }
        i[t].classList.remove("on")
    }
    writeCookie("soundCheck", n);
    sound_init();
    n != 4 && _$("audio_home").play()
}

function CheckSound2(n) {
    var i, t;
    for (_settingObj.allMatchSetting.awaySoundId = n,
             _settingObj.favMatchSetting.awaySoundId = n,
             i = $("#selectsound2").find("span"),
             t = 0; t < i.length; t++) {
        if (i[t].attributes['data-value'].value == n) {
            i[t].classList.add("on");
            continue
        }
        i[t].classList.remove("on")
    }
    writeCookie("soundCheck2", n);
    sound_init();
    n != 4 && _$("audio_away").play()
}

function initSoundSelect(n, t) {
    var r, i;
    if ($("#" + t)[0])
        for (r = $("#" + t).find("span"),
                 i = 0; i < r.length; i++) {
            if (r[i].attributes['data-value'].value == n) {
                r[i].classList.add("on");
                continue
            }
            r[i].classList.remove("on")
        }
}

function addDivNullSound() {
    var soundHtml = `
        <div id="sound">
            <div id="soundsHome" style="display: none"></div>
            <div id="soundsAway" style="display: none"></div>
            <div id="soundsRed" style="display: none"></div>
            <div id="soundsError" style="display: none"></div>
        </div>
    `;

    $('#openSettingModal').append(soundHtml);
}

function sound_init() {
    // addDivNullSound()
    // var n = [];
    // n.push('<audio id="audio_home" style="display: none">');
    // n.push('<source src="../../assets/sound/sound' + _settingObj.allMatchSetting.homeSoundId + '.mp3" type="audio/mpeg" />');
    // n.push('<source src="../../assets/sound/sound' + _settingObj.allMatchSetting.homeSoundId + '.ogg" type="audio/ogg" />');
    // n.push("<\/audio>");
    // _$("soundsHome").innerHTML = n.join("");
    // n = [];
    // n.push('<audio id="audio_favHome" style="display: none">');
    // n.push('<source src="../../assets/sound/sound' + _settingObj.favMatchSetting.homeSoundId + '.mp3" type="audio/mpeg" />');
    // n.push('<source src="../../assets/sound/sound' + _settingObj.favMatchSetting.homeSoundId + '.ogg" type="audio/ogg" />');
    // n.push("<\/audio>");
    // _$("soundsHome").innerHTML += n.join("");
    // n = [];
    // n.push('<audio id="audio_away" style="display: none">');
    // n.push('<source src="../../assets/sound/sound' + _settingObj.allMatchSetting.awaySoundId + '.mp3" type="audio/mpeg" />');
    // n.push('<source src="../../assets/sound/sound' + _settingObj.allMatchSetting.awaySoundId + '.ogg" type="audio/ogg" />');
    // n.push("<\/audio>");
    // _$("soundsAway").innerHTML = n.join("");
    // n = [];
    // n.push('<audio id="audio_favAway" style="display: none">');
    // n.push('<source src="../../assets/sound/sound' + _settingObj.favMatchSetting.awaySoundId + '.mp3" type="audio/mpeg" />');
    // n.push('<source src="../../assets/sound/sound' + _settingObj.favMatchSetting.awaySoundId + '.ogg" type="audio/ogg" />');
    // n.push("<\/audio>");
    // _$("soundsAway").innerHTML += n.join("");
    // n = [];
    // n.push('<audio id="audio_red" style="display: none">');
    // n.push('<source src="../../assets/sound/red.mp3" type="audio/mpeg" />');
    // n.push('<source src="../../assets/sound/red.ogg" type="audio/ogg" />');
    // n.push("<\/audio>");
    // _$("soundsRed").innerHTML = n.join("");
    // _$("audio_home").load();
    // _$("audio_away").load();
    // _$("audio_red").load();
    // window.removeEventListener("touchstart", soundHandle, !0);
    // window.addEventListener("touchstart", soundHandle, !0)
}

function soundInitPage(page, param) {
    $(`#audio_${param} .soure-mp3`).attr('src', `../../assets/sound/sound${_settingObj.page[page][param]}.mp3`);
    $(`#audio_${param} .soure-ogg`).attr('src', `../../assets/sound/sound${_settingObj.page[page][param]}.ogg`);
    $(`#audio_${param}`).load();
    _$("audio_red").load();
    window.removeEventListener("touchstart", soundHandle, !0);
    window.addEventListener("touchstart", soundHandle, !0)
}


function soundHandle() {
    _$("audio_homeSoundCheck").load();
    _$("audio_awaySoundCheck").load();
    _$("audio_red").load();
    window.removeEventListener("touchstart", soundHandle)
}

function soundInitOtherPage(page, param) {
    $(`#audio_other_${param} .soure-mp3`).attr('src', `../../assets/sound/sound${_settingObj.page[page][param]}.mp3`);
    $(`#audio_other_${param} .soure-ogg`).attr('src', `../../assets/sound/sound${_settingObj.page[page][param]}.ogg`);
    $(`#audio_other_${param}`).load();
    window.removeEventListener("touchstart", soundOtherHandle, !0);
    window.addEventListener("touchstart", soundOtherHandle, !0)
}

function soundOtherHandle() {
    _$("audio_other_homeSoundCheck").load();
    _$("audio_other_awaySoundCheck").load();
    window.removeEventListener("touchstart", soundOtherHandle)
}

function takeSound(page, param) {
    const val = getCookie(_settingObj.page[page][`${param}Cookie`]);
    val && (_settingObj.page[page][param] = val);

    soundInitPage(page, param);

    if (val == '4') return;

    _$(`audio_${param}`).play().catch((error) => {
        console.log("Phát âm thanh thất bại :", error);
    });
}

function CheckSoundOnMenu(element) {
    // const soundId = element.attributes['data-value'].value;
    // const soundElements = element.parentNode.querySelectorAll("span");

    // _settingObj.allMatchSetting.homeSoundId = soundId;
    // _settingObj.favMatchSetting.homeSoundId = soundId;

    // soundElements.forEach((soundElement) => {
    //     soundElement.classList.toggle(
    //         "on",
    //         soundElement.attributes['data-value'].value == soundId
    //     );
    // });

    // writeCookie("soundCheck", soundId);
    // sound_init();

    // if (soundId != 4) {
    //     _$("audio_home").play();
    // }
}

function CheckSound2OnMenu(element) {
    // const soundId = element.attributes['data-value'].value;
    // const soundElements = element.parentNode.querySelectorAll("span");

    // _settingObj.allMatchSetting.awaySoundId = soundId;
    // _settingObj.favMatchSetting.awaySoundId = soundId;

    // soundElements.forEach(soundElement => {
    //     soundElement.classList.toggle('on', soundElement.attributes['data-value'].value == soundId);
    // });

    // writeCookie("soundCheck2", soundId);
    // sound_init();

    // if (soundId != 4) {
    //     _$("audio_away").play();
    // }
}

function CheckGoalWindow(n, page) {
    // _settingObj.page[page].goalWindow = n;
    // writeCookie(_settingObj.page[page].goalWindowCookie, n ? 1 : 0);
}

function CheckRedWindow(n, page) {
    // _settingObj[page].redWindow = n;
    // writeCookie(_settingObj[page].redWindowCookie, n ? 1 : 0);
}


function ShowTeamOrderOnMenu(element) {
    // const isChecked = element.checked;
    // const rankValue = isChecked ? 1 : 0;
    // writeCookie(_settingObj.rankCookie, rankValue);
    // _settingObj.rank = isChecked;
    // // var matchcount = 0;
    // // for (let n = 1; n <= matchcount; n++) {
    // //     if (RS[n][0] === -1) continue;
    // //     const homeOrderElement = $("#horder_" + A[n][0]);
    // //     const guestOrderElement = $("#gorder_" + A[n][0]);
    // //     if (isChecked) {
    // //         const regex = /[\u4E00-\u9FA5]/g;
    // //         if (A[n][17]) homeOrderElement.html(`[${A[n][17].toString().replace(regex, "")}]`);
    // //         if (A[n][18]) guestOrderElement.html(`[${A[n][18].toString().replace(regex, "")}]`);
    // //     } else {
    // //         homeOrderElement.html("");
    // //         guestOrderElement.html("");
    // //     }
    // // }
    // if (_settingObj.rank) {
    //     const elementsButton = document.querySelectorAll('[id^="horder_"]');
    //     elementsButton.forEach(element => {
    //         element.classList.remove('hidden-element');
    //     });
    // } else {
    //     const elementsButton = document.querySelectorAll('[id^="horder_"]');
    //     elementsButton.forEach(element => {
    //         element.classList.add('hidden-element');
    //     });
    // }
}

function showYellowCheckInSetting(target) {
    // document.getElementById(target.id).checked ? (writeCookie(_settingObj.yellowCardCookie, 1),
    //     $(".yellowcard").show()) : (writeCookie(_settingObj.yellowCardCookie, 0),
    //     $(".yellowcard").hide());
}

function ShowRemarkOnMenu(element) {
    var matchcount = 0;
    var n, t;
    if (element.checked) {
        for (_settingObj.remark = !0,
                 writeCookie(_settingObj.remarkCookie, 1),
                 n = 1; n <= matchcount; n++)
            if (CheckFilterOpt(A[n]) && hiddenID.indexOf("_" + A[n][0] + "_") == -1)
                if (hasExplain(n) && hidden_LeagueId.indexOf("_" + B[A[n][1]][0] + "_") == -1 && (A[n][22] != "" || showExplain(A[n][24], A[n][4], A[n][5]) != ""))
                    (FilterType == 0 || FilterType == 1 && parseInt(A[n][8]) > 0 || FilterType == 2 && A[n][8] == "-1" || FilterType == 3 && A[n][8] == "0" || FilterType == 4 && A[n][20] == "True") && (t = document.getElementById("tr2_" + A[n][0]),
                    t && !$("#tr1_" + A[n][0]).is(":hidden") && (t.style.display = ""));
                else
                    continue
    } else
        for (_settingObj.remark = !1,
                 writeCookie(_settingObj.remarkCookie, 0),
                 n = 1; n <= matchcount; n++)
            if (CheckFilterOpt(A[n]) && hiddenID.indexOf("_" + A[n][0] + "_") == -1)
                if (hasExplain(n) && hidden_LeagueId.indexOf("_" + B[A[n][1]][0] + "_") == -1 && (A[n][22] != "" || showExplain(A[n][24], A[n][4], A[n][5]) != ""))
                    (FilterType == 0 || FilterType == 1 && parseInt(A[n][8]) > 0 || FilterType == 2 && A[n][8] == "-1" || FilterType == 3 && A[n][8] == "0" || FilterType == 4 && A[n][20] == "True") && (t = document.getElementById("tr2_" + A[n][0]),
                    t && (t.style.display = "none"));
                else
                    continue
}


function handleShowRemarkOnMenu(isChecked, page, param) {
    var matchcount = 0;
    var n, t;
    const obj = _settingObj.page[page][param];

    const valCookie = _settingObj.page[page][`${param}Cookie`];

    if (isChecked) {
        for (_settingObj.page[page][param] = !0,
                 writeCookie(valCookie, 1),
                 n = 1; n <= matchcount; n++)
            if (CheckFilterOpt(A[n]) && hiddenID.indexOf("_" + A[n][0] + "_") == -1)
                if (hasExplain(n) && hidden_LeagueId.indexOf("_" + B[A[n][1]][0] + "_") == -1 && (A[n][22] != "" || showExplain(A[n][24], A[n][4], A[n][5]) != ""))
                    (FilterType == 0 || FilterType == 1 && parseInt(A[n][8]) > 0 || FilterType == 2 && A[n][8] == "-1" || FilterType == 3 && A[n][8] == "0" || FilterType == 4 && A[n][20] == "True") && (t = document.getElementById("tr2_" + A[n][0]),
                    t && !$("#tr1_" + A[n][0]).is(":hidden") && (t.style.display = ""));
                else
                    continue
    } else
        for (_settingObj.page[page][param] = !1,
                 writeCookie(valCookie, 0),
                 n = 1; n <= matchcount; n++)
            if (CheckFilterOpt(A[n]) && hiddenID.indexOf("_" + A[n][0] + "_") == -1)
                if (hasExplain(n) && hidden_LeagueId.indexOf("_" + B[A[n][1]][0] + "_") == -1 && (A[n][22] != "" || showExplain(A[n][24], A[n][4], A[n][5]) != ""))
                    (FilterType == 0 || FilterType == 1 && parseInt(A[n][8]) > 0 || FilterType == 2 && A[n][8] == "-1" || FilterType == 3 && A[n][8] == "0" || FilterType == 4 && A[n][20] == "True") && (t = document.getElementById("tr2_" + A[n][0]),
                    t && (t.style.display = "none"));
                else
                    continue
}

$(document).ready(function () {

    $('#rightSearchInput').on('input', function () {
        const filter = $(this).val().toLowerCase();

        const removeAccentAndWhitespace = (str) => {
            const accents = 'áàảãạâấầẩẫậăắằẳẵặéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụýỳỷỹỵ';
            const withoutAccents = 'aaaaaaaaaaaaaaaaaeeeeeeeeeeiiiiiiooooooooooooouuuuuuuuuuyyyyy';

            return str.split('').map(char => {
                const index = accents.indexOf(char);
                return index !== -1 ? withoutAccents[index] : char;
            }).join('').replace(/\s+/g, '');
        }

        if (filter == '') {
            $('#other-leagues').find('.nation, .nation-item, .nation-leagues-item').removeClass('nation-expand').show();
            $('#other-leagues .nation-leagues').hide();
            return;
        }
        $('#other-leagues .nation-leagues-item').each(function () {
            const nation = $(this).parents('.nation');
            const nationLeagues = nation.find('.nation-leagues');
            const leagueName = removeAccentAndWhitespace($(this).find('span').text().toLowerCase());
            if (leagueName.includes(removeAccentAndWhitespace(filter))) {
                nation.addClass('nation-expand').show();
                nationLeagues.fadeIn();
                $(this).show();
            } else {
                $(this).hide();
                const total = nationLeagues.find('.nation-leagues-item').length;
                const hidden = nationLeagues.find('.nation-leagues-item:hidden').length;
                if (total !== hidden) nation.addClass('nation-expand').show();
                else nation.removeClass('nation-expand').hide();
            }
        });
        $('#other-leagues .nation-item').each(function () {
            const nation = $(this).parents('.nation');
            const leagueName = removeAccentAndWhitespace($(this).find('span').text().toLowerCase());

            if (leagueName.includes(removeAccentAndWhitespace(filter))) {
                nation.addClass('nation-expand').show();
                nation.find('.nation-leagues-item').show();
                $(this).show();
            } else {
                $(this).hide();
                const nationLeagues = nation.find('.nation-leagues-item');
                const total = nationLeagues.length;
                const hidden = nationLeagues.filter(':hidden').length;
                if (total !== hidden) {
                    nation.addClass('nation-expand').show();
                    nation.find('.nation-item').show();
                } else {
                    nation.removeClass('nation-expand').hide();
                    nationLeagues.hide();
                }
            }
        });

    })
})

String.prototype.format = function () {
    var args = arguments;
    return this.replace(
        /\{([\d]+)\}/g,
        function ($1, $2) {
            return args[$2];
        }
    );
};
