(function() {
'use strict';
function getById(id) { return document.getElementById(id); }
let mainfig = getById('mainfig');
let mainimg = getById('mainimg');
let mainlink = getById('mainlink');
function openCW(caption, focusLink) {
mainfig.removeChild(caption);
mainlink.tabIndex = 0;
if (focusLink) mainlink.focus();
}
function addCWListeners(caption) {
if (caption) {
caption.addEventListener('click', e => openCW(caption));
caption.addEventListener('keyup',
e => { if (e.key == 'Enter') openCW(caption, true) });
}
}
function setImage(src, href, cw) {
let caption = getById('cw');
let cwText = `cw: ${cw}`;
if (cw && caption) {
caption.innerHTML = cwText;
} else if (caption) {
openCW(caption);
} else if (cw) {
let template = document.createElement('template');
template.innerHTML = `
${cwText}
`;
let content = template.content;
addCWListeners(content.getElementById('cw'));
mainfig.insertBefore(content, mainlink);
mainlink.tabIndex = -1;
}
mainimg.src = src;
mainlink.href = href;
}
function activateButton(button) {
setImage(button.value, button.dataset.link, button.dataset.warning);
}
function setup() {
for (let button of document.querySelectorAll('#alts input')) {
button.onchange = e => { if (button.checked) activateButton(button); };
}
addCWListeners(getById('cw'));
}
window.addEventListener('load', setup);
})();