document.addEventListener('DOMContentLoaded', function() {
var donateButton = document.getElementById('donateButton');
var card = document.getElementById('card');

donateButton.addEventListener('click', function(event) {
  event.stopPropagation();
  if (card.classList.contains('card-out')) {
    card.classList.remove('card-out');
    card.classList.add('card-in');
  } else {
    card.classList.remove('card-in');
    card.classList.add('card-out');
  }
});

window.addEventListener('click', function(event) {
  if(event.target === card || card.contains(event.target)) {
    return;
  }
  
  if (card.classList.contains('card-in')) {
    card.classList.remove('card-in');
    card.classList.add('card-out');
  }
});

var cryptoWallets = [
    {
        symbol: 'XMR',
        name: 'Monero',
        address: '49r2aeun8DtV5VqZpZSwRpS83WfUWEaLt4NG8HJwwVkbiT1vSsXTXrPFKkKTdc6MPX9iezbTidNPvhGZKCnM1338TfK6Hgi',
    },
    {
        symbol: 'LTC',
        name: 'Litecoin',
        address: 'LRAm7h5XENknfYEpbdVsvyGY8D6MiXGTDV',
    },
    {
        symbol: 'BTC',
        name: 'Bitcoin',
        address: 'bc1qkv97ajh7f0a72l9rsjd3fmmly5q5uywr06q3x0',
    }
];

var walletsContainer = document.getElementById('wallets');
var qrImage = document.getElementById('qrImage');
var donateTitle = document.getElementById('donateTitle');
var donateWarn = document.getElementById('donateWarn');
var walletAddress = document.getElementById('walletAddress');
var copyButton = document.getElementById('copyButton');

function selectWallet(symbol) {
    var wallet = cryptoWallets.find(function (w) {
        return w.symbol === symbol;
    });
    if (!wallet) return;

    var buttons = Array.from(walletsContainer.children);
    buttons.forEach(function (button) {
        button.classList.remove('active');
        if (button.textContent === symbol) {
            button.classList.add('active');
        }
    });

    qrImage.src = 'https://api.qrserver.com/v1/create-qr-code/?size=180x180&data=' + wallet.address;
    donateTitle.textContent = 'Donate ' + wallet.name;
    donateWarn.textContent = 'Send only ' + wallet.name + ' (' + wallet.symbol + ') to this deposit address. Sending any other coin or token to this address may result in the loss of your donation. Thanks!';
    walletAddress.value = wallet.address;
    copyButton.textContent = 'Copy';
}

function copyText() {
    var input = document.createElement('input');
    document.body.appendChild(input);
    input.value = walletAddress.value;
    input.select();
    document.execCommand('copy');
    document.body.removeChild(input);
    copyButton.textContent = 'Copied';
}

cryptoWallets.forEach(function (wallet) {
    var button = document.createElement('button');
    button.textContent = wallet.symbol;
    button.addEventListener('click', function () {
        selectWallet(wallet.symbol);
    });
    walletsContainer.appendChild(button);
});

copyButton.addEventListener('click', copyText);

selectWallet(cryptoWallets[0].symbol);
});