var BgOverlayDivId = 'modal_overlay_background';
var ModalOverlayDivId = 'modal_wrapper';
var ModalOverlayVisible = false;

// When the window is resized, recalculate the overlay background dimensions
function resizeBgOverlay() {
   if (ModalOverlayVisible) {
      // Get the document size
      if (window.innerHeight && window.scrollMaxY) {
      	var xScroll = (window.scrollbars.visible) ? document.body.scrollWidth : window.innerWidth;
      	xScroll += window.scrollMaxX;
      	var yScroll = document.body.scrollHeight;
      }
      else if (document.body.scrollHeight > document.body.offsetHeight) { // all but IE Mac
      	var xScroll = document.body.scrollWidth;
      	var yScroll = document.body.scrollHeight;
      }
      else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
      	var xScroll = document.body.offsetWidth;
      	var yScroll = document.body.offsetHeight;
      }
      // Get the window size
      if (self.innerHeight) {	// all except IE
      	var windowWidth = self.innerWidth;
      	var windowHeight = self.innerHeight;
      }
      else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      	var windowWidth = document.documentElement.clientWidth;
      	var windowHeight = document.documentElement.clientHeight;
      }
      else if (document.body) { // other Explorers
      	var windowWidth = document.body.clientWidth;
      	var windowHeight = document.body.clientHeight;
      }
      // Fix size when the document size is less than the window size
      var pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
      var pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;
      // Set the overlay size
      with (document.getElementById(BgOverlayDivId).style) {
         height = pageHeight + 'px';
         width = pageWidth + 'px';
      }
   }
}

// IE bug: hide all select lists when the overlay background is showing
function hideAllSelectLists(HideSelect) {
   if (navigator.appName == 'Microsoft Internet Explorer') {
      var SelectList = document.getElementsByTagName('select');
      for (var j=0;j<SelectList.length;j++) SelectList[j].style.visibility = (HideSelect) ? 'hidden' : 'visible';
   }
}

// Only show the overlay if the browser can support it
function showModalOverlay() {
   if (document.getElementById) {
      ModalOverlayVisible = true;
      resizeBgOverlay();
      hideAllSelectLists(true);
      // Show the background overlay
      with (document.getElementById(BgOverlayDivId).style) {
         display = 'block';
         visibility = 'visible';
      }
      // Show the modal div
      with (document.getElementById(ModalOverlayDivId)) {
			style.marginTop = Math.floor(offsetHeight/-2) + 'px';
         style.visibility = 'visible';
      }
   }
   return true;
}

function closeModalOverlay() {
   ModalOverlayVisible = false;
   document.getElementById(ModalOverlayDivId).style.visibility = 'hidden';
   with (document.getElementById(BgOverlayDivId).style) {
      visibility = 'hidden';
      display = 'none';
   }
   hideAllSelectLists(false);
}

// Add onresize event-handler to the window, without overwriting any existing event-handlers
if (document.getElementById) {
   if (window.addEventListener) window.addEventListener('resize', resizeBgOverlay, false);
   else if (document.addEventListener) document.addEventListener('resize', resizeBgOverlay, false);
   else if (window.attachEvent) window.attachEvent('onresize', resizeBgOverlay);
   else {
      var ExistingBgOverlayOnresizeFunction = window.onresize;
      window.onresize = (ExistingBgOverlayOnresizeFunction == null) ? resizeBgOverlay : new Function('ExistingBgOverlayOnresizeFunction();resizeBgOverlay();');
   }
}
