var modalPageCovers = new Object();
var modalCount = 0;

function showModal(div)
{
  if (div.style.display != 'block')
    modalCount++;
    
  var modalPageCover = modalPageCovers[div.id];
  
  // move the div to the body if necessary
  var body = document.getElementsByTagName('body')[0];
  
  if (div.parentNode != body)
  {
    // add a placeholder so we can move the div back when the modal is closed
    var ph_div = document.createElement('div');
    ph_div.id = div.id + '-modal-placeholder';
    div.parentNode.insertBefore(ph_div, div);
    body.appendChild(div);
  }
  
  if (!modalPageCover)
  {
    modalPageCover = document.createElement('div');
    modalPageCover.style.position = 'absolute';
    modalPageCover.style.top = '0';
    modalPageCover.style.left = '0';
    modalPageCover.style.width = '9999px';
    modalPageCover.style.height = '9999px';
    modalPageCover.style.zIndex = '1000';
    
    modalPageCovers[div.id] = modalPageCover;
    
    div.parentNode.insertBefore(modalPageCover, div);
    
    var bg = 'rgba(0, 0, 0, 0.5)';
    if (arguments.length >= 2)
    {
      if (('' + arguments[1]).substring(0, 3) == 'rgb')
        bg = arguments[1];
      else if (isNaN(parseInt(arguments[1])))
        bg = 'url(' + arguments[1] + ')';
      else
        bg = 'rgba(0, 0, 0, ' + (parseInt(arguments[1]) / 100) + ')';
    }
    
    try {
      modalPageCover.style.background = bg;
    }
    catch(e)
    {
      modalPageCover.style.background = '#666666';
    }
  }
  
  
  modalPageCover.style.top = '0';
  modalPageCover.style.display = 'block';
  var html = document.getElementsByTagName('html')[0];
  html.style.height = '100%';
  html.style.overflow = 'hidden';

  div.style.position = 'absolute';
  div.style.left = '50%';
  div.style.display = 'block';
  div.style.visibility = 'visible';
  div.style.zIndex = '1000';
  div.style.top = (getScrollY() + (getWinHeight() / 2) - (div.clientHeight / 2)) + 'px';
  div.style.marginLeft = '-' + (div.clientWidth / 2) + 'px';
  
  modalPageCovers[div.id] = modalPageCover;
}

function hideModal(div)
{
  if (div.style.display != 'none')
    modalCount--;
    
  if (modalCount < 0) modalCount = 0;
    
  var modalPageCover = modalPageCovers[div.id];
  if (modalPageCover)
  {
    modalPageCover.style.display = 'none';
  }
  if (div)
  {
    div.style.display = 'none';
    div.style.visibility = 'hidden';
    if (modalCount == 0) {
      var html = document.getElementsByTagName('html')[0];
      html.style.overflow = '';
    }
    
    // move div back to its original location!
    var div_ph = document.getElementById(div.id + '-modal-placeholder');

    if (div_ph)
    {
      div_ph.parentNode.insertBefore(div, div_ph);
      div_ph.parentNode.removeChild(div_ph);
    }
  }
}
  
      function getWinHeight() 
      {
        if (window.innerHeight) return window.innerHeight - 18;
      	else if (document.documentElement && document.documentElement.clientHeight) 
      		return document.documentElement.clientHeight;
      	else if (document.body && document.body.clientHeight) 
      		return document.body.clientHeight;
        return 0;
      }
      
      function getScrollY()
      {
        if (typeof window.pageYOffset == "number") return window.pageYOffset;
        else if (document.documentElement && document.documentElement.scrollTop)
      		return document.documentElement.scrollTop;
      	else if (document.body && document.body.scrollTop) 
      		return document.body.scrollTop; 
      	else if (window.scrollY) return window.scrollY;
        return 0;
      }

