(function (Joomla) {
    window.jSelectEdocmanDetails = function (id, itemid, title, btnClass) {
		if(typeof itemid === 'undefined')
		{
			itemid = Joomla.getOptions('defaultItemid');
		}
        var editor = Joomla.getOptions('EDEditor');
        var tag = '<a class=\"' + btnClass + '\" href='+'\"index.php?option=com_edocman&amp;view=document&amp;id='+id+'&amp;Itemid=' + itemid + '\">'+title+'</a>';
        window.parent.Joomla.editors.instances[editor].replaceSelection(tag);

        if (Joomla.getOptions('isJoomla4')) {
            window.parent.Joomla.Modal.getCurrent().close();
        } else {
            window.parent.jModalClose();
        }
    };

    window.jSelectEdocmanDownload = function (id, itemid, title, btnClass) {
		if(typeof itemid === 'undefined')
		{
			itemid = Joomla.getOptions('defaultItemid');
		}
        var editor = Joomla.getOptions('EDEditor');
        var tag = '<a class=\"' + btnClass + '\"  href='+'\"index.php?option=com_edocman&amp;task=document.download&amp;id='+id+'&amp;Itemid=' + itemid + '\">'+title+'</a>';
        window.parent.Joomla.editors.instances[editor].replaceSelection(tag);

        if (Joomla.getOptions('isJoomla4')) {
            window.parent.Joomla.Modal.getCurrent().close();
        } else {
            window.parent.jModalClose();
        }
    };

	window.jSelectEdocmanView = function (id, itemid, title, btnClass) {
		if(typeof itemid === 'undefined')
		{
			itemid = Joomla.getOptions('defaultItemid');
		}
        var editor = Joomla.getOptions('EDEditor');
        var tag = '<a class=\"' + btnClass + '\"  href='+'\"index.php?option=com_edocman&amp;task=document.viewdoc&amp;id='+id+ '&amp;Itemid=' + itemid + '\">'+title+'</a>';
        window.parent.Joomla.editors.instances[editor].replaceSelection(tag);

        if (Joomla.getOptions('isJoomla4')) {
            window.parent.Joomla.Modal.getCurrent().close();
        } else {
            window.parent.jModalClose();
        }
    };

	window.jSelectEdocmanShortcode = function (id, title) {
		
        var editor = Joomla.getOptions('EDEditor');
        var tag = '{edocmanlink ' + id + '}';
        window.parent.Joomla.editors.instances[editor].replaceSelection(tag);

        if (Joomla.getOptions('isJoomla4')) {
            window.parent.Joomla.Modal.getCurrent().close();
        } else {
            window.parent.jModalClose();
        }
    };

    document.addEventListener('DOMContentLoaded', function () {
        var functionName;
        // Get the elements
        var elements = document.querySelectorAll('.select-link');

        for (var i = 0, l = elements.length; l > i; i++) {
            // Listen for click event
            elements[i].addEventListener('click', function (event) {
                event.preventDefault();
                functionName = event.target.dataset.function;
                window[functionName](event.target.dataset.id, event.target.dataset.itemid, event.target.dataset.title, event.target.dataset.btnclass);
            })
        }
    });
})(Joomla);
