var gCalendarId = null;
var gImageToLoad = null;
var gTimeoutHandle = null;
function loadImage()
{
  showModalDivs();
  centerImage( document.getElementById( 'contentLayer' ) );

  if ( !gImageToLoad )
  {
    gImageToLoad = new Image();
    gImageToLoad.src='./images/' + gCalendarId + '-18.png';
  }
  if ( gImageToLoad.complete )
  {
    afterImageLoaded();
    return;
  }
  gTimeoutHandle = setTimeout( 'loadImage()', 200 );
}

function loadFlash( name )
{
  showModalDivs();
  var flashDiv = document.getElementById( 'contentLayer' );
  flashDiv.innerHTML = closeHtml() +
    '<OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" WIDTH="623" HEIGHT="457" CODEBASE="http://active.macromedia.com/flash5/cabs/swflash.cab#version=7,0,0,0">'+
      '<PARAM NAME="movie" VALUE="./images/'+name+'.swf">'+
      '<PARAM NAME="play" VALUE="true">'+
      '<PARAM NAME="loop" VALUE="false">'+
      '<PARAM NAME="wmode" VALUE="transparent">'+
      '<PARAM NAME="quality" VALUE="low">'+
      '<EMBED SRC="./images/'+name+'.swf" WIDTH="623" HEIGHT="457" quality="low" loop="false" wmode="transparent" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></EMBED>'+
    '</OBJECT>';
  centerImage( flashDiv );
}

function afterImageLoaded()
{
  var imageDiv = document.getElementById( 'contentLayer' );
  imageDiv.innerHTML = '<span onclick="hideModalDivs()">' + closeHtml() + '<img src=\'./images/' + gCalendarId + '-18.png\'/></span>';
  centerImage( imageDiv );
}

function closeHtml()
{
  return '<span style="color:white;font-weight:bold;position:relative;left:-5px;" onclick="hideModalDivs()">x</span><br/>';
}

function centerImage( imageDiv )
{
  var imageX = documentWidth() - imageDiv.offsetWidth;
  var imageY = documentHeight() - imageDiv.offsetHeight;
  imageDiv.style.left = ( imageX / 2 ) + "px";
  imageDiv.style.top = ( imageY / 2 ) + "px";
}

function hideModalDivs()
{
  document.getElementById('darkBackgroundLayer').style.display='none';
  document.getElementById( 'contentLayer' ).style.display='none';
  document.getElementById( 'contentLayer' ).innerHTML='';
  if ( typeof( gTimeoutHandle ) == 'number' )
  {
    clearTimeout( gTimeoutHandle );
  }
  setVisibilitySelectFields( 'visible' );
}

function showModalDivs()
{
  document.getElementById('darkBackgroundLayer').style.display='block';
  document.getElementById( 'contentLayer' ).style.display='block';
  setVisibilitySelectFields( 'hidden' );
}

function documentWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && document.documentElement.clientWidth ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && document.body.clientWidth ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

function documentHeight() {
  var myHeight = 0;
  if( typeof( window.innerHeight ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && document.body.clientHeight ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function setVisibilitySelectFields( visibility )
{
  var selects = document.getElementsByTagName( "select" );
  for ( var selectIx = 0; selectIx < selects.length; selectIx++ )
  {
    selects[ selectIx ].style.visibility = visibility;
  }
}