document.write('<scr' + 'ipt type="text/javascript" src="js/jquery-bgiframe.js"><\/scr' + 'ipt>');

jQuery.extend({
  msg: {
    sDivBg: '<div id="msgDivBg"><div id="msgDivContent"><\/div><\/div>',
    
    sDivMsg: '<div id="msgDivMsg" style="padding:1em;" onresize="jq.msg.centralizarMensagem();"><div id="msgDivMsgCorpo"><\/div><div id="msgDivMsgBotoes" style="text-align:center; margin:0.5em"><\/div><\/div>',
    
    bRemover: true,

    sLarguraMsg: '500px',

    sMsgConfirm: 'Carregando ...',

    sBotoesConfirm: '<input type="button" class="formButtons" id="msgBtnSim" value="{SIM}" style="text-align:center"> <input type="button" class="formButtons" id="msgBtnNao" value="{NAO}" style="text-align:center">',

    sBotoesMsg: '<p style="text-align:center; margin:0.5em"><input class="formButtons" type="button" id="msgBtnOk" value="{LABEL}" style="text-align:center"><\/p>',
    
    obterBotoesConfirm: function() {
      var sLabelSim = 'Sim';
      var sLabelNao = 'Não';

      if (arguments[0] != undefined) {
        sLabelSim = arguments[0];
      }

      if (arguments[1] != undefined) {
        sLabelNao = arguments[1];
      }

      return this.sBotoesConfirm.replace('{SIM}', sLabelSim).replace('{NAO}', sLabelNao);
    },
    
    obterBotoesAlerta: function() {
      var sLabel = 'OK';

      if (arguments[0] != undefined && arguments[0] != '') {
        sLabel = arguments[0];
      }

      return this.sBotoesMsg.replace('{LABEL}', sLabel);
    },
    
    atualizaTamanho: function() {
      if (jQuery('#msgDivBg').length > 0) {
       
        jQuery.msg.getSizes();
        
        jQuery('#msgDivBg')
          .html('<!--'+jQuery.msg.selfWidth+'-->')
          .css({
            'width': jQuery.msg.selfWidth+'px',
            'height': jQuery.msg.selfHeight+'px'
          });
        
        if (!jQuery.browser.msie) {
          jq('#msgDivMsg')
            .css({
              'top': (jQuery.msg.selfHeight/2)+'px',
              'left': ((jQuery.msg.selfWidth/2) - (parseInt(jQuery.msg.sLarguraMsg)/2))+'px'
            });          
        }
        
      }
    },
    
    getSizes: function() {
      if (self.innerWidth) {
        selfWidth = self.innerWidth;
        selfHeight = self.innerHeight;
      }
      else if (document.documentElement && document.documentElement.clientWidth) {
        selfWidth = document.documentElement.clientWidth;
        selfHeight = document.documentElement.clientHeight;
      }
      else if (document.body) {
        selfWidth = document.body.clientWidth;
        selfHeight = document.body.clientHeight;
      }

      this.selfWidth = selfWidth;
      this.selfHeight = selfHeight;
    },
    
    inicializaJanelas: function() {

      // Obtendo os tamanhos do documento
      this.getSizes();

      var bIe6 = jQuery.browser.msie;// && (jQuery.browser.version == "6.0");

      // Adicionando as divs ao documento
      if (jQuery('#msgDivBg').length == 0) {

        var oCSSbg = {
          'position': 'fixed',
          'background': '#666',
          'width': this.selfWidth+'px',
          'height': this.selfHeight+'px',
          'top': 0,
          'left': 0,
          'z-index': 1000
        };

        if (bIe6) {
          oCSSbg.filter = 'alpha(opacity=20)';
        } else {
          oCSSbg.opacity = '.20';
        }

        jQuery(this.sDivBg)
          .appendTo('body')
          .css(oCSSbg)
          .bgiframe();

        jQuery('#msgDivContent')
          .css({
            'width': this.selfWidth+'px',
            'height': this.selfHeight+'px',
            'top': 0,
            'left': 0,
            'cursor': 'wait'
          });

        // Insere no evento do BODY a função pra redimensionar o fundo
        jQuery(window).resize(jQuery.msg.atualizaTamanho);

      }

      var iScrollLeft = (bIe6 ? (document.documentElement.scrollLeft || document.body.scrollLeft) : 0) + (this.selfWidth / 2 - (parseInt(this.sLarguraMsg) / 2));

      // Se ja existe a div da msg, remove ela
      var bCriarMsg = jQuery('#msgDivMsg').length == 0;
      if (bCriarMsg) {
        jQuery(this.sDivMsg)
          .appendTo('body')
          .css({
            'position': 'fixed',
            'background': '#fff',
            'border': '1px solid #000000',
            'top': 400,
            'left': iScrollLeft + 'px',
            'width': this.sLarguraMsg,
            'z-index': 1020
          });
      } else {
        jQuery('#msgDivMsgCorpo').html('');
        jQuery('#msgDivMsgBotoes').html('');

        jQuery('#msgDivMsg')
          .css({
            'top': 400,
            'left': iScrollLeft + 'px',
            'width': this.sLarguraMsg
          });
      }

      if(bCriarMsg && bIe6) {
        oDivBg = jQuery('#msgDivBg');
        oDivMsg = jQuery('#msgDivMsg');
        oDivBg = oDivBg.css('position', 'absolute')[0];
        oDivMsg = oDivMsg.css('position', 'absolute')[0];

        oDivBg.style.setExpression('top',"(_=(document.documentElement.scrollTop || document.body.scrollTop))+'px'");
        oDivBg.style.setExpression('left',"(_=(document.documentElement.scrollLeft || document.body.scrollLeft))+'px'");

        oDivMsg.style.setExpression('top', "(_=(document.documentElement.scrollTop || document.body.scrollTop)) + ((document.documentElement.clientHeight || document.body.clientHeight) / 2 - 100) + 'px'");
        oDivMsg.style.setExpression('left', "(_=(document.documentElement.scrollLeft || document.body.scrollLeft)) + ((document.documentElement.clientWidth || document.body.clientWidth) / 2 - " + (oDivMsg.offsetWidth / 2) + ") + 'px'");
      }
    },
    
    centralizarMensagem: function() {
      bIe6 = jQuery.browser.msie;// && (jQuery.browser.version == "6.0");

      var iAlturaEl = jQuery('#msgDivMsg')[0].offsetHeight;
      var iTop;

      if (iAlturaEl > this.selfHeight) {
        
        iTop = 0;
        jQuery('#msgDivMsg')
          .css({
            'overflow-y': 'scroll',
            'height': (jQuery.msg.selfHeight - 25) + 'px',
            'top': (bIe6 ? (document.documentElement.scrollTop || document.body.scrollTop) : 0) + 10
          })
          .each(function() {
            if(bIe6) {
              this.style.setExpression('top', "(_=(document.documentElement.scrollTop || document.body.scrollTop)) + 10 + 'px'");
            }
          });
      } else {
        
        iTop = (this.selfHeight - iAlturaEl) / 2;
        oDivMsg = jQuery('#msgDivMsg').css('top', (bIe6 ? (document.documentElement.scrollTop || document.body.scrollTop) : 0) + iTop)[0];

        if(bIe6) {
          oDivMsg.style.setExpression('top', "(_=(document.documentElement.scrollTop || document.body.scrollTop)) + ((document.documentElement.clientHeight || document.body.clientHeight) / 2 - " + (iAlturaEl / 2) + ") + 'px'");
        }
      }
    },
    
    incluiCorpo: function (sHtml, sLabelBotaoSim, sLabelBotaoNao, bExibeBotoes) {
      jQuery('#msgDivMsg #msgDivMsgCorpo').html(sHtml);
      
      if (bExibeBotoes) {
        jQuery('#msgDivMsg #msgDivMsgBotoes').html(jQuery.msg.obterBotoesConfirm(sLabelBotaoSim, sLabelBotaoNao));
      } else {
        jQuery('#msgDivMsg #msgDivMsgBotoes').html(jQuery.msg.obterBotoesAlerta(sLabelBotaoSim));
      }
      
      jQuery.msg.centralizarMensagem();      
    },

    alerta: function (sHtml, fCallbackOk) {
      var sLabelBotaoOk = 'OK';
     
      fnCallbackExibicao = function() { return true; };
      
      if (arguments[2] != undefined) {
        sLabelBotaoOk = arguments[2];
      }
      
      jQuery.msg.inicializaJanelas();
      jQuery.msg.incluiCorpo(sHtml, sLabelBotaoOk, '', false);
      
      jQuery('#msgBtnOk').click(function() {
        bRetorno = fCallbackOk();
        
        if (bRetorno == undefined || bRetorno == true) {
          jQuery.msg.bRemover = true;
          jQuery.msg.removerJanelas();
        }
        
      });
    },
    
    /*confirmar : function (sHtml, fCallbackSim) {
      var sLabelBotaoSim = 'Sim';
      
      
      
    },*/
    
    removerJanelas: function() {
  		if (this.bRemover) {
	  		jQuery('#msgDivBg').remove();
	      jQuery('#msgDivMsg').remove();
  		}
  	}
    
  }
});