/**
 * Local background image selection for Hangit plugin
 * Allows users to select a background image from their local machine without server upload
 */
document.addEventListener('DOMContentLoaded', function() {
    // Get the file input and the background image elements
    const fileInput = document.getElementById('hangit-local-background');
    const backgroundImage = document.querySelector('.showroom-background-img');
    const chooseButton = document.getElementById('hangit-choose-local');
    
    if (fileInput && backgroundImage) {
        // When the user clicks the button, trigger the file input
        if (chooseButton) {
            chooseButton.addEventListener('click', function(e) {
                e.preventDefault();
                fileInput.click();
            });
        }
        
        // When a file is selected, display it as the background
        fileInput.addEventListener('change', function() {
            const file = this.files[0];
            
            if (file && file.type.match('image.*')) {
                const reader = new FileReader();
                
                reader.onload = function(e) {
                    // Update the background image with the selected file
                    backgroundImage.src = e.target.result;
                    
                    // Don't close the dialog, just update the background
                };
                
                // Read the image file as a data URL
                reader.readAsDataURL(file);
            }
        });
    }
});
