var d8018d6852bc49e3b3e655364cf1439c = {
    // 初始化函数，设置默认折叠状态
    init: function() {
        // 隐藏词头中的重音符号 ˈ 和 ˌ
        var hyphenations = document.querySelectorAll('.hyphenation');
        hyphenations.forEach(function(hyphenation) {
            hyphenation.innerHTML = hyphenation.innerHTML.replace(/[ˈˌ]/g, '');
        });        
        
        // 为 .pron 和 .amevarpron 添加斜杠，并在相邻时添加空格
        var pronElements = document.querySelectorAll('.pron, .amevarpron');
        pronElements.forEach(function(pron) {
            // 检查是否已经添加过斜杠，避免重复添加
            if (!pron.innerHTML.startsWith('/') && !pron.innerHTML.endsWith('/')) {
                pron.innerHTML = '/' + pron.innerHTML + '/';
            }
        });

        // 检查相邻的 .pron 和 .amevarpron，并在它们之间添加空格
        var allPronContainers = document.querySelectorAll('body *'); // 遍历所有可能的父容器
        allPronContainers.forEach(function(container) {
            var prons = container.querySelectorAll('.pron, .amevarpron');
            for (var i = 0; i < prons.length - 1; i++) {
                var current = prons[i];
                var next = prons[i + 1];
            
                // 如果当前是 .pron 且下一个是 .amevarpron，并且它们之间没有空格
                if (current.classList.contains('pron') && 
                    next.classList.contains('amevarpron') && 
                    !/\s/.test(next.previousSibling?.textContent || '')) {
                
                    // 在它们之间插入一个空格
                    next.insertAdjacentHTML('beforebegin', ' ');
                }
            }
        });

        // 默认折叠所有例句和翻译
        var examples = document.querySelectorAll('.example');
        examples.forEach(function(example) {
            example.style.display = 'none';
            var expcn = example.querySelector('expcn');
            if (expcn) {
                expcn.style.display = 'none';
            }
        });
        
        var neutralSpans = document.querySelectorAll('span.neutral');
        neutralSpans.forEach(function(span) {
        span.innerHTML = span.innerHTML.replace(/[:/\$]/g, ''); // 移除冒号、斜杠和美元符号
        });

        // 为词头(.hyphenation)添加点击事件
        var hyphenations = document.querySelectorAll('.hyphenation');
        hyphenations.forEach(function(hyphenation) {
            hyphenation.style.cursor = 'pointer';
            
            // 长按相关状态变量
            var isLongPress = false;
            var longPressTimer;
            var hasTextSelection = false;
            
            // 鼠标/触摸按下事件
            hyphenation.addEventListener('mousedown', function(e) {
                isLongPress = false;
                hasTextSelection = false;
                longPressTimer = setTimeout(function() {
                    isLongPress = true;
                }, 200);
            });
            
            hyphenation.addEventListener('touchstart', function(e) {
                isLongPress = false;
                hasTextSelection = false;
                longPressTimer = setTimeout(function() {
                    isLongPress = true;
                }, 200);
            }, {passive: true});
            
            // 鼠标/触摸释放事件
            hyphenation.addEventListener('mouseup', function(e) {
                clearTimeout(longPressTimer);
                // 检查是否有文本被选中
                var selection = window.getSelection();
                hasTextSelection = selection.toString().length > 0;
                
                // 如果是长按且有文本选择，则阻止默认行为
                if (isLongPress && hasTextSelection) {
                    e.preventDefault();
                    return;
                }
                
                // 否则触发切换
                if (!isLongPress) {
                    d8018d6852bc49e3b3e655364cf1439c.toggleAllExamplesUnderHyphenation(this);
                }
            });
            
            hyphenation.addEventListener('touchend', function(e) {
                clearTimeout(longPressTimer);
                // 检查是否有文本被选中
                var selection = window.getSelection();
                hasTextSelection = selection.toString().length > 0;
                
                // 如果是长按且有文本选择，则阻止默认行为
                if (isLongPress && hasTextSelection) {
                    e.preventDefault();
                    return;
                }
                
                // 否则触发切换
                if (!isLongPress) {
                    d8018d6852bc49e3b3e655364cf1439c.toggleAllExamplesUnderHyphenation(this);
                }
            });
            
            hyphenation.addEventListener('mouseleave', function() {
                clearTimeout(longPressTimer);
            });
        });

        // 为英文释义(.def)和中文释义(.defcn)添加点击事件（仅当有相关例句时）
        var definitions = document.querySelectorAll('.def, .defcn');
        definitions.forEach(function(def) {
            // 检查该释义下是否有相关例句
            if (d8018d6852bc49e3b3e655364cf1439c.hasRelatedExamples(def)) {
                def.style.cursor = 'pointer';
                
                // 长按相关状态变量
                var isLongPress = false;
                var longPressTimer;
                var hasTextSelection = false;
                
                // 鼠标/触摸按下事件
                def.addEventListener('mousedown', function(e) {
                    isLongPress = false;
                    hasTextSelection = false;
                    longPressTimer = setTimeout(function() {
                        isLongPress = true;
                    }, 200);
                });
                
                def.addEventListener('touchstart', function(e) {
                    isLongPress = false;
                    hasTextSelection = false;
                    longPressTimer = setTimeout(function() {
                        isLongPress = true;
                    }, 200);
                }, {passive: true});
                
                // 鼠标/触摸释放事件
                def.addEventListener('mouseup', function(e) {
                    clearTimeout(longPressTimer);
                    // 检查是否有文本被选中
                    var selection = window.getSelection();
                    hasTextSelection = selection.toString().length > 0;
                    
                    // 如果是长按且有文本选择，则阻止默认行为
                    if (isLongPress && hasTextSelection) {
                        e.preventDefault();
                        return;
                    }
                    
                    // 否则触发切换
                    if (!isLongPress) {
                        d8018d6852bc49e3b3e655364cf1439c.toggleRelatedExamples(this);
                    }
                });
                
                def.addEventListener('touchend', function(e) {
                    clearTimeout(longPressTimer);
                    // 检查是否有文本被选中
                    var selection = window.getSelection();
                    hasTextSelection = selection.toString().length > 0;
                    
                    // 如果是长按且有文本选择，则阻止默认行为
                    if (isLongPress && hasTextSelection) {
                        e.preventDefault();
                        return;
                    }
                    
                    // 否则触发切换
                    if (!isLongPress) {
                        d8018d6852bc49e3b3e655364cf1439c.toggleRelatedExamples(this);
                    }
                });
                
                def.addEventListener('mouseleave', function() {
                    clearTimeout(longPressTimer);
                });
            }
        });

        // 默认折叠所有知识栏（除了.f2nbox）并添加点击事件
        var knowledgeBoxes = document.querySelectorAll('.collobox, .thesbox, .usagebox, .grambox, .f2nbox');
        knowledgeBoxes.forEach(function(box) {
            var heading = box.querySelector('.heading');
            if (heading) {
                heading.style.cursor = 'pointer';
                
                // 仅当不是.f2nbox时才折叠内容
                if (!box.classList.contains('f2nbox')) {
                    var sections = box.querySelectorAll('.section, .expl, .compareword, .gramexa');
                    sections.forEach(function(section) {
                        section.style.display = 'none';
                    });
                }
                
                // 添加点击事件（使用事件委托提高性能）
                heading.addEventListener('click', function(e) {
                    e.stopPropagation();
                    d8018d6852bc49e3b3e655364cf1439c.toggleKnowledgeBox(this);
                });
            }
        });
    },
    
    // 切换词头下所有例句的显示状态
    toggleAllExamplesUnderHyphenation: function(hyphenation) {
        // 获取词头所在的容器（entry, group或phrvbentry）
        var container = hyphenation.closest('.entry, .group, .phrvbentry') || hyphenation.parentNode;
        
        // 获取容器中的所有例句
        var examples = container.querySelectorAll('.example');
        
        // 检查是否有任何例句是隐藏的
        var anyHidden = false;
        examples.forEach(function(example) {
            if (example.style.display === 'none' || !example.style.display) {
                anyHidden = true;
            }
        });
        
        // 切换所有例句的显示状态
        examples.forEach(function(example) {
            example.style.display = anyHidden ? 'block' : 'none';
            var expcn = example.querySelector('expcn');
            if (expcn) {
                expcn.style.display = anyHidden ? 'block' : 'none';
            }
        });
    },
    
    // 检查释义是否有相关例句
    hasRelatedExamples: function(definition) {
        var container = definition.closest('.entry, .group, .phrvbentry') || definition.parentNode;
        var nextDefStart = this.findNextDefinitionStart(definition);
        var currentElement = definition;
        
        while (currentElement && currentElement !== nextDefStart) {
            if (currentElement.classList.contains('def') || currentElement.classList.contains('defcn')) {
                // 跳过其他释义元素
                currentElement = currentElement.nextElementSibling;
                continue;
            }
            
            // 检查当前元素是否是例句或包含例句
            if (currentElement.classList.contains('example') || 
                currentElement.querySelector('.example')) {
                return true;
            }
            
            currentElement = currentElement.nextElementSibling;
        }
        
        return false;
    },
    
    // 切换相关例句的显示状态
    toggleRelatedExamples: function(definition) {
        // 获取当前释义所在的容器（entry, group或phrvbentry）
        var container = definition.closest('.entry, .group, .phrvbentry') || definition.parentNode;
        
        // 找到下一个释义或词性词条的起点
        var nextDefStart = this.findNextDefinitionStart(definition);
        
        // 获取当前释义到下一个释义之间的所有例句
        var examples = [];
        var currentElement = definition;
        
        while (currentElement && currentElement !== nextDefStart) {
            if (currentElement.classList.contains('def') || currentElement.classList.contains('defcn')) {
                // 跳过其他释义元素
                currentElement = currentElement.nextElementSibling;
                continue;
            }
            
            // 收集当前元素中的例句
            var currentExamples = currentElement.querySelectorAll('.example');
            if (currentExamples.length > 0) {
                examples.push(...currentExamples);
            }
            
            // 检查当前元素是否是例句
            if (currentElement.classList.contains('example')) {
                examples.push(currentElement);
            }
            
            currentElement = currentElement.nextElementSibling;
        }
        
        // 如果没有找到例句，尝试在整个容器中查找
        if (examples.length === 0) {
            examples = container.querySelectorAll('.example');
        }
        
        // 切换例句显示状态
        var anyHidden = false;
        examples.forEach(function(example) {
            if (example.style.display === 'none' || !example.style.display) {
                anyHidden = true;
            }
        });
        
        examples.forEach(function(example) {
            example.style.display = anyHidden ? 'block' : 'none';
            var expcn = example.querySelector('expcn');
            if (expcn) {
                expcn.style.display = anyHidden ? 'block' : 'none';
            }
        });
    },
    
    // 找到下一个释义或词性词条的起点
    findNextDefinitionStart: function(currentElement) {
        var nextElement = currentElement.nextElementSibling;
        
        while (nextElement) {
            // 如果是下一个词性词条
            if (nextElement.classList.contains('pos') || 
                nextElement.classList.contains('group') || 
                nextElement.classList.contains('phrvbentry') ||
                nextElement.classList.contains('hyphenation')) {
                return nextElement;
            }
            
            // 如果是下一个释义
            if (nextElement.classList.contains('def') || nextElement.classList.contains('defcn')) {
                // 检查是否是当前释义的中英文配对
                if (!currentElement.classList.contains(nextElement.classList[0])) {
                    return nextElement;
                }
            }
            
            nextElement = nextElement.nextElementSibling;
        }
        
        return null;
    },
    
    // 知识栏切换函数
    toggleKnowledgeBox: function(heading) {
        var box = heading.closest('.collobox, .thesbox, .usagebox, .grambox, .f2nbox');
        if (!box) return;
        
        // 获取所有可能的内容区域
        var sections = box.querySelectorAll('.section, .expl, .compareword, .gramexa');
        var isHidden = sections[0] && (sections[0].style.display === 'none' || !sections[0].style.display);
        
        // 切换显示状态
        sections.forEach(function(section) {
            section.style.display = isHidden ? 'block' : 'none';
        });
        
        // 更新箭头指示器（如果有）
        var arrow = box.querySelector('.arrow');
        if (arrow) {
            arrow.innerHTML = isHidden ? '▼' : '▶';
        }
    },
    
    // 保持原有辅助函数不变
    toggle: function (expandable) {
        var expParent = expandable.parentNode;
        var img = expParent.querySelector('img');
        if (img !== null) {
            if (img.addEventListener) {
                img.addEventListener('click', function (event) {
                    event.stopPropagation();
                });
            } else {
                img.attachEvent('onclick', function (event) {
                    event.cancelBubble = false;
                });
            }
        }
        var target = expParent.querySelector('.content');
        if (target !== null) {
            if (target.addEventListener) {
                target.addEventListener('click', function (event) {
                    event.stopPropagation();
                });
            } else {
                target.attachEvent('onclick', function (event) {
                    event.cancelBubble = false;
                });
            }
        }
        var arrow = expParent.querySelector('span.arrow');
        if (arrow !== null) {
            if (target.style.display == 'block') {
                target.style.display = 'none';
                arrow.innerHTML = '\u25BA';
            } else {
                target.style.display = 'block';
                arrow.innerHTML = '\u25BC';
            }
        }
    },
    showAtLink: function (ele) {
        if (ele.nextSibling.style.display == 'block') {
            ele.nextSibling.style.display = 'none';
        } else {
            ele.nextSibling.style.display = 'block';
        }
    },
    toggleImg: function (ele) {
        if (ele.style.maxHeight == 'none') {
            ele.style.maxHeight = '4em';
        } else {
            ele.style.maxHeight = 'none';
        }
    }
};

// 使用DOMContentLoaded替代onload确保更快执行
document.addEventListener('DOMContentLoaded', function() {
    d8018d6852bc49e3b3e655364cf1439c.init();
});