var DownloadManagerButton = function () {};

/**
 * Ouvre le dialog de téléchargement pour un document donné
 * @param {string} documentId  - ID du document
 * @param {string} token       - Token base64 du document
 * @param {string} documentName - Nom du document (pour le titre)
 */
DownloadManagerButton.OpenDialog = function (documentId, token, documentName) {

    Dialog.open('', {"title": documentName,
        "app": "DownloadManager",
        "class": "DialogAdminDownloadManager",
        "method": "DownLoadDocument",
        "params": documentId
    });
};

/**
 * Ferme le dialog de téléchargement
 * @param {string} documentId - ID du document
 */
DownloadManagerButton.CloseDialog = function (documentId) {

    var overlay = document.getElementById("dmb-dialog-" + documentId);

    if (overlay) {
        overlay.style.display = "none";
    }
};

/**
 * Ferme tous les dialogs ouverts (ex : clic sur l'overlay)
 */
DownloadManagerButton.CloseAll = function () {

    var overlays = document.querySelectorAll(".dmb-dialog-overlay");

    overlays.forEach(function (overlay) {
        overlay.style.display = "none";
    });
};

// Fermeture sur clic en dehors du dialog
document.addEventListener("click", function (e) {
    if (e.target && e.target.classList.contains("dmb-dialog-overlay")) {
        DownloadManagerButton.CloseAll();
    }
});
