var gcsMask = null;
var gcsMsgBox = null;
var gcsImage = null;
var gcsTitle = null;
var gcsText = null;
var gcsToolbar = null;
var gcsBtnOK = null;

function csOnMsgBoxOK(aEvent)
  {
  aEvent.preventDefault();
  aEvent.stopPropagation();
  
  gcsMask.setVisible(false, { duration: 0.10 });
  gcsMsgBox.setVisible(false, { duration: 0.10 });
  }

function csMsgBox(aIcon, aTitle, aText)
  {
  if (!gcsMask)
    {
    gcsMask = Ext.DomHelper.append(
      document.body, 
      {
      id: 'dvMask',
      cls: 'clsMask'
      },
      true);
    
    gcsMsgBox = Ext.DomHelper.append(
      document.body,
      {
      id: 'dvMsgBox',
      cls: 'clsMsgBox',
      cn: 
        [
          {
          cls: 'clsMsgBoxTop',
          cn:
            [
              { cls: 'clsMsgBoxBkg' },
              { tag: 'img' },
              { tag: 'h1' },
              { cls: 'clsMsgBoxText' },
              {
              cls: 'clsMsgBoxToolbar', 
              cn: 
                [
                  { cls: 'clsMsgBoxToolbarBottom' }
                ]
              }
            ]
          },
          { cls: 'clsMsgBoxBottom' }
        ]
      },
      true);

    gcsImage = Ext.select('#dvMsgBox img');
    gcsTitle = Ext.select('#dvMsgBox h1');
    gcsText = Ext.select('#dvMsgBox div.clsMsgBoxText');
    gcsToolbar = Ext.select('#dvMsgBox .clsMsgBoxToolbar');

    gcsBtnOK = gcsToolbar.insertFirst(
      {
      cls: 'clsMsgBoxButton',
      id: 'btMsgBoxButtonOK',
      cn: 
        [
          {
          tag: 'a',
          href: '#',
          cn: 
            [
              {
              tag: 'span',
              html: 'OK'
              }
            ]
          }
        ]
      });

    gcsBtnOK.on('click', csOnMsgBoxOK, this);
    }

  gcsImage.set({src: '/_images/forms/icons/' + aIcon});
  Ext.DomHelper.overwrite(gcsTitle.item(0), aTitle);
  Ext.DomHelper.overwrite(gcsText.item(0), aText);
 
  var ps = csGetPageSize();
  gcsMask.setWidth(ps[0]);
  gcsMask.setHeight(ps[1]);

  var so = csGetScrollOffsets();
  gcsMsgBox.setX((ps[0] - 530) / 2);
  gcsMsgBox.setY(so[1] + 200);

  gcsMask.fadeIn({ duration: 0.15, endOpacity: 0.80 });
  gcsMsgBox.setVisible(true, { duration: 0.15 });

  gcsBtnOK.focus();
  }

function csGetScrollOffsets()
  {
  var x = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;
  var y = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
  return [x, y];
  }

function csGetPageSize() 
  {
  var xScroll, yScroll;
    
  if (window.innerHeight && window.scrollMaxY) 
    {
    xScroll = window.innerWidth + window.scrollMaxX;
    yScroll = window.innerHeight + window.scrollMaxY;
    }
  else if (document.body.scrollHeight > document.body.offsetHeight)
    {
    // All but Explorer Mac.
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
    }
  else
    {
    // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari.
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
    }
    
  var windowWidth, windowHeight;
    
  if (self.innerHeight)
    {
    // All except Explorer.
    if (document.documentElement.clientWidth)
      windowWidth = document.documentElement.clientWidth; 
    else
      windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
    }
  else if (document.documentElement && document.documentElement.clientHeight)
    {
    // Explorer 6 Strict Mode.
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
    }
  else if (document.body)
    {
    // Other Explorers.
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
    }  
    
  // For small pages with total height less then height of the viewport.
  if (yScroll < windowHeight)
    pageHeight = windowHeight;
  else 
    pageHeight = yScroll;
  
  // For small pages with total width less then width of the viewport.
  if (xScroll < windowWidth)
    pageWidth = xScroll;    
  else
    pageWidth = windowWidth;

  return [pageWidth, pageHeight];
  }
  

