/*!
* @file
* @brief    BibTeX formatted bibliography plug-in for Joomla, JavaScript functions
* @author   Levente Hunyadi
* @version  1.2.0
* @remarks  Copyright (C) 2009-2011 Levente Hunyadi
* @remarks  Licensed under GNU/GPLv3, see https://www.gnu.org/licenses/gpl-3.0.html
* @see      https://hunyadi.info.hu/projects/bibtex
*/

window.addEventListener('DOMContentLoaded', function () {
    function addBlockToggle(linkClass, blockClass) {
        document.querySelectorAll(linkClass).forEach(function (elem) {
            // find element that contains linked block (e.g. BibTeX code or abstract text)
            var block;
            for (var parent = elem.parentElement; parent != null && block == null; parent = parent.parentElement) {
                block = parent.querySelector(blockClass);
            };
            if (block != null) {
                // register click event to show/hide linked block (e.g. BibTeX code or abstract text)
                elem.addEventListener('click', function (event) {
                    // toggle linked block display
                    if (block.style.getPropertyValue('display') == 'none') {
                        block.style.removeProperty('display');
                    } else {
                        block.style.setProperty('display', 'none');
                    }

                    // suppress event propagation
                    event.preventDefault();
                });
            }
        });

        document.querySelectorAll(blockClass).forEach(function (elem) {
            elem.style.setProperty('display', 'none');
        });
    }

    addBlockToggle('a.bibtex-link-code', 'pre.bibtex-code');
    addBlockToggle('a.bibtex-link-abstract', 'span.bibtex-abstract');
});