﻿// MaskMessage
function closeMask()
{
    document.body.removeChild(document.getElementById('containerMM'));
}

// Fonction d'affichage d'un web message dans le style d'une alerte javascript standard
// link : Lien a appliquer sur le bouton OK, si rien n'est spécifié l'action du bouton est de fermer le webmessage
function printWMAlert(title, text, link) {
    
    if (link == undefined) {
        link = "javascript:closeMask();";
    }

    var content;

    content = '<img src="images/mask_message/top_380px.png" style="position: relative;" />' +
                        '<div style="width: 398; background-image:url(\'../images/mask_message/bg_380px.png\')">' +
                            '<div style="padding-left: 20px; padding-right:20px; text-align:center;">';
    if (title != "")
        content += '<h2 style="margin: 0px 0px 12px 5px;">' + title + '</h2>';
    
    content +=              text +
                            '<br /><a href="'+link+'"><img src="images/boutons/bt_ok.png" style="border-style:none; margin-top:12px;"/></a>' +
                        '</div>' +
                        '</div>' +
                        '<img src="images/mask_message/bottom_380px.png" />';
    printMaskMessage(content, 398, 250, 0)
}

// Evolution de printWMAlert (affichage de deux boutons, choix de leur libellé et action)
// Rappel : l'action pour fermer la fenetre est closeMask();
// Les actions doivent etre en javascript (pour faire un lien : window.location.href='myurl'; return false; )
function printWMDialBox(title, text, textBt1, actionBt1, textBt2, actionBt2) {

    var content;

    content = '<img src="images/mask_message/top_380px.png" style="position: relative;" />' +
                        '<div style="width: 398; background-image:url(\'../images/mask_message/bg_380px.png\')">' +
                            '<div style="padding-left: 25px; padding-right:25px; text-align:left;">';
    if (title != "")
        content += '<h2 style="margin: 0px 0px 12px 0px;">' + title + '</h2>';

    content += text + '<br />';
    if (textBt2 != "") {
        content += '<div class="button btForward" style="margin-top:12px; margin-bottom:2px; float:right;" onclick="' + actionBt2 + '">' +
                   '<span class="lPart">' + textBt2 + '</span><span class="rPart">&nbsp;</span>' +
               '</div>';
    }
    if (textBt1 != "") {
        content += '<div class="button btBackward" style="margin-top:12px; margin-bottom:2px; float:right;" onclick="' + actionBt1 + '">' +
                   '<span class="lPart">&nbsp;</span><span class="rPart">' + textBt1 + '</span>' +
               '</div>';

    }

    content +=  '<div style="clear:both"></div>' +
                '</div>' +
                '</div>' +
                '<img src="images/mask_message/bottom_380px.png" />';
    printMaskMessage(content, 398, 250, 0)
}

function printMaskMessage(contentText, contentWidth, contentHeight, btClose)
{
    var parent = document.body;
    var container = document.createElement("div");
    var mask = document.createElement("div");
    var content = document.createElement("div");
    var btnClose = document.createElement("img");
    
    //Le container.
    container.id = "containerMM";
    container.style.position = "absolute";
    container.className = "Ccontainer";
    container.style.zIndex = "100";
    
    // Le mask
    mask.id = "mask";
    mask.style.width = document.body.clientWidth;
    mask.style.height = document.body.clientHeight;
    mask.style.position = "fixed";
    mask.style.top = "0px";
    mask.style.left = "0px";
    mask.style.opacity = "0.7"
    mask.style.filter = "alpha(opacity=70)"
    mask.style.zIndex = "100";
    mask.style.backgroundColor = 'black';
    
    //Le content
    content.id = 'WMContent'
    content.innerHTML = contentText;
    content.style.width = contentWidth;
    content.style.height = contentHeight;
    content.style.zIndex = "101";
    
    if (navigator.userAgent.indexOf('MSIE') != -1)
    {
        content.style.position = "absolute";
        content.style.top = mask.style.pixelHeight/2-content.style.pixelHeight/2+"px";
        content.style.left = mask.style.pixelWidth/2-content.style.pixelWidth/2+"px";
        content.style.background = "url(null) fixed"; // Evite le sautillement.
    }
    else
    {
        content.style.position = "fixed";
        content.style.top = parseInt(mask.style.height)/2-parseInt(content.style.height)/2+"px";
        content.style.left = parseInt(mask.style.width)/2-parseInt(content.style.width)/2+"px";
    }
    
    // Le bouton de fermeture.
    if (btClose)
    {
        if (btClose == 1)
            btnClose.src = "images/boutons/bt_white_cross.png"
        else
            btnClose.src = "images/boutons/bt_black_cross.png"
        btnClose.style.width = "11px";
        btnClose.style.height = "12px";
        btnClose.style.position = "absolute";
        btnClose.style.top = "10px";
        btnClose.style.right = "10px";
        //btnClose.innerHTML = '<span style="margin-left: 2px;">X</span>'
        btnClose.style.margin = "0px";
        //btnClose.style.fontWeight = "bold";
        //btnClose.style.backgroundColor = 'black';
        btnClose.style.border = "none";
        //btnClose.style.color = "white"
        btnClose.onclick = closeMask;
        btnClose.style.cursor = "pointer";
        btnClose.style.zIndex = "100";
        content.appendChild(btnClose);
    }
    
    parent.appendChild(container);
    container.appendChild(mask);
    container.appendChild(content);
}

function webMessages(messages)
{
    this.messages = messages;
}


webMessages.prototype.print = function(index)
{
    var nav = "";
        
    if ( this.messages.length - 1 > index && index == 0 )
        nav = '<img src="images/boutons/bt_suivant.gif" style="margin-right: 20px; cursor: pointer;" onclick="closeMask(); wms.print(' + (index+1) + ')" />';
    else if ( this.messages.length - 1 > index && index > 0 )
        nav = '<img src="images/boutons/bt_precedent_fleche.gif" style="margin-right: 5px; cursor: pointer;" onclick="closeMask(); wms.print(' + (index-1) + ')" /><img src="images/boutons/bt_suivant.gif" style="margin-right: 20px; cursor: pointer;" onclick="closeMask(); wms.print(' + (index+1) + ')" />';
    else if ( this.messages.length - 1 == index && index > 0 )
        nav = '<img src="images/boutons/bt_precedent_fleche.gif" style="margin-right: 5px; cursor: pointer;" onclick="closeMask(); wms.print(' + (index-1) + ')" /><img src="images/boutons/bt_terminer.gif" style="margin-right: 20px; cursor: pointer;" onclick="closeMask();" />';
    else
        nav = '<img src="images/boutons/bt_terminer.gif" style="margin-right: 20px; cursor: pointer;" onclick="closeMask();" />';
    
    printMaskMessage('<img src="images/web_message/haut.png" /><div class="wmsContent"><p class="wmsTitle">' + this.messages[index][0] + '</p><p class="wmsBody">' + this.messages[index][1]  + '</p></div><div class="wmsNav">' + nav + '</div><img src="images/web_message/bas.png" />', 366, 300, 0)
}
