


document.addEventListener('DOMContentLoaded', function() {
    const player = document.querySelector('.video-player video');
    if (!player) return;

    
    
    var playerHovered = false;
    var playerBox = player.closest('.video-player') || player;
    playerBox.addEventListener('mouseenter', function(){ playerHovered = true; });
    playerBox.addEventListener('mouseleave', function(){ playerHovered = false; });

    
    document.addEventListener('keydown', function(e) {
        var ae = document.activeElement;
        if (ae && (ae.tagName === 'INPUT' || ae.tagName === 'TEXTAREA' || ae.tagName === 'SELECT' || ae.tagName === 'BUTTON' || ae.tagName === 'A' || ae.isContentEditable)) return;
        if (!playerHovered && !playerBox.contains(ae)) return;

        switch(e.key) {
            case ' ':
            case 'k':
                e.preventDefault();
                if (player.paused) {
                    player.play();
                } else {
                    player.pause();
                }
                break;

            case 'f':
                e.preventDefault();
                if (document.fullscreenElement) {
                    document.exitFullscreen();
                } else {
                    player.requestFullscreen();
                }
                break;

            case 'm':
                e.preventDefault();
                player.muted = !player.muted;
                break;

            case 'ArrowLeft':
                e.preventDefault();
                player.currentTime = Math.max(0, player.currentTime - 10);
                break;

            case 'ArrowRight':
                e.preventDefault();
                player.currentTime = Math.min(player.duration, player.currentTime + 10);
                break;

            case 'ArrowUp':
                e.preventDefault();
                player.volume = Math.min(1, player.volume + 0.1);
                break;

            case 'ArrowDown':
                e.preventDefault();
                player.volume = Math.max(0, player.volume - 0.1);
                break;
        }
    });

    
    const savedVolume = localStorage.getItem('player_volume');
    if (savedVolume !== null) {
        player.volume = parseFloat(savedVolume);
    }

    player.addEventListener('volumechange', function() {
        localStorage.setItem('player_volume', player.volume);
    });
});
