﻿/* ===========================================================================
CÓDIGO RESPONSÁVEL PELO OBJETO DE MENSAGENS (Gaia.UI.WebControl.Message)
PROGRAMADOR: RAFAEL PIRES DE FREITAS
DATA: 27/07/2006
ULTIMA REVISAO: 21/11/2007 09:53 [RAFAEL PIRES DE FREITAS]
============================================================================ */

// ENUMS ----------------------------------------------------------------------

// MessageButtons
function MessageButtons()
{
    this.RetryCancel = 1;
    this.Ok = 2;
    this.OkCancel = 3;
}
var MessageButtons = new MessageButtons();

// MessageIcon
function MessageIcon()
{
    this.Error = 1;
    this.Information = 2;
    this.Warning = 3;
}
var MessageIcon = new MessageIcon();

// END ENUMS ------------------------------------------------------------------

// METODOS --------------------------------------------------------------------

// MessageAction
function MessageAction()
{
    this.Ok;
    this.Retry;
    this.Cancel;
}

// CLASSES --------------------------------------------------------------------
function MessageIcons()
{
    this.IconError = new String();
    this.IconInformation = new String();
    this.IconWarning = new String();

}

function Message()
{
    // Campos
    this._messageIcons = MessageIcons,
    this._lock         = document.getElementById("msg_mensagem_lock");
    this._dialog       = document.getElementById("msg_mensagem");
    this._ok           = document.getElementById("msg_mensagem_ok");
    this._retry        = document.getElementById("msg_mensagem_retry");
    this._cancel       = document.getElementById("msg_mensagem_cancel");
    this._icon         = document.getElementById("msg_mensagem_icon");
    this._loadingImage = document.getElementById("msg_mensagem_loadingImage");
    this._text         = document.getElementById("msg_mensagem_text");
    this._selects      = document.getElementsByTagName("select");
    this._selectsStyle = new Array();
    this.LoadingImage  = "";
    this.IsActive      = false;
    this.ActiveMode    = "";
    this.Enabled       = true;
        
    // Metodos
    // Exibe uma mensagem para o usuário.
    this.Show = function(icon, buttons, text, action, description)
    {
        this.LockScreen();
        
        this._icon.style.display = "inline";
        this._loadingImage.style.display = "none";
            
        if(buttons == MessageButtons.RetryCancel)
        {
            this._ok.style.display = "none";
            this._retry.style.display = "block";
            this._cancel.style.display = "block";
            this._retry.onclick = function(){Message.LockScreen(); action.Retry();}
            //this._cancel.onclick = function(){action.Cancel();};
            this._cancel.onclick = function(){Message.Hide();}; //Inserido por Marcilio em 12/01/2007
        }
        else if(buttons == MessageButtons.Ok)
        {
            this._ok.style.display = "inline";
            this._retry.style.display = "none";
            this._cancel.style.display = "none";
            if(action != null)
                this._ok.onclick = function(){action.Ok();}
            else
                this._ok.onclick = function(){Message.Hide();};
        }
        else if(buttons == MessageButtons.OkCancel)
        {
            this._ok.style.display = "inline";
            this._retry.style.display = "none";
            this._cancel.style.display = "inline";
            this._ok.onclick = function(){action.Ok();}
            this._cancel.onclick = function(){action.Cancel();};
        }
        
        if(icon == MessageIcon.Error)
            this._icon.src = this._messageIcons.IconError;
        else if(icon == MessageIcon.Information)
            this._icon.src = this._messageIcons.IconInformation;
        else if(icon == MessageIcon.Warning)
            this._icon.src = this._messageIcons.IconWarning;
            
        if(description != null)
            this._icon.onclick = function(){alert(description);};
        
        // definindo valores da caixa de mensagem
        this._text.removeChild(this._text.firstChild);
        this._text.appendChild(document.createTextNode(text));
        
        // formatando a caixa de mensagem
        this._dialog.style.zIndex = "1000";
        this.Resize();
        this._dialog.style.visibility = "visible";
        this._dialog.style.display = "block";
        
        this.ActiveMode = "Show";
    };
    
    // Oculta a mensagem enviada para o usuário.
    this.Hide = function()
    {
        this._lock.style.visibility = "hidden";
        this._lock.style.display = "none";
        this._dialog.style.visibility = "hidden";
        this._dialog.style.display = "none";
        
        for(var x = 0; x < this._selectsStyle.length; x++)
            this._selects[x].style.visibility = this._selectsStyle[x];
            
        while(this._selectsStyle.length > 0)
            this._selectsStyle.pop();
            
        this.ActiveMode = "";
        this.IsActive = false;
    };
    
    this.Resize = function()
    {
        this._dialog.style.top = document.documentElement.scrollTop + ((document.documentElement.clientHeight / 2) - (this._dialog.style.height.substring(0, this._dialog.style.height.indexOf("px")))/2) + "px";
        this._dialog.style.left = document.documentElement.scrollLeft + ((document.documentElement.clientWidth / 2) - (this._dialog.style.width.substring(0, this._dialog.style.width.indexOf("px")))/2) + "px";
        this._lock.style.width = document.documentElement.scrollWidth + "px";
        this._lock.style.height = document.documentElement.scrollHeight + "px";
    };
    
    // Trava a tela impedindo interações do usuário como sistema.
    this.LockScreen = function()
    {
        window.onresize = function(){if(Message.IsActive)Message.Resize();};
        window.onscroll = function(){if(Message.IsActive && Message.ActiveMode == "Show"){Message.Resize();}};
    
        this.IsActive = true;
        this.ActiveMode = "LockScreen";
        
        // oculta os objetos (select) para que não fiquem sobrepondo os div's criados
        if(this._selectsStyle.length == 0)
        {
            for(var x = 0; x < this._selects.length; x++)
            {
                this._selectsStyle.push(this._selects[x].style.visibility);
                this._selects[x].style.visibility = "hidden";
            }
        }
        
        // formantando o bloqueio de tela
        this._lock.style.zIndex = "999";
        this._lock.style.left = "0px";
        this._lock.style.top = "0px";
        this._lock.style.visibility = "visible";
        this._lock.style.display = "block";
        
        // formatando a caixa de mensagem
        this._dialog.style.zIndex = "1000";
        this.Resize();
        this._ok.style.display = "none";
        this._retry.style.display = "none";
        this._cancel.style.display = "none";
        this._text.removeChild(this._text.firstChild);
        this._text.appendChild(document.createTextNode("Aguarde enquanto a sua solicitação é processada..."));
        this._icon.style.display = "none";
        this._loadingImage.src = this.LoadingImage;
        this._loadingImage.style.display = "inline";
        this._dialog.style.visibility = "visible";
        this._dialog.style.display = "block";
    };
}
var Message = new Message();
