
/** launchLightbox()
 *
 *  Automatically creates a <div> for the lightbox effect and places the
 *  object referenced by contentID on top of it, centering it within the
 *  visible area of the page. The content moves accordingly if the page
 *  is scrolled while the lightbox is active.
 *
 *  Opera, Firefox, IE6, IE7, Safari
 *
**/

var disallowSubmitCase = null;

var lightBoxActive  = false;

var flashObjectList = new Array();
flashObjectList[1]  = 'nav';
flashObjectList[2]  = 'video_spotlight';
flashObjectList[3]  = 'nav_inside';


function launchLightbox(contentData) {

  var lightboxObject = document.getElementById('lightbox');

  if( !lightboxObject ) {

    var createLightbox  = document.createElement('div');
        createLightbox.setAttribute('id', 'lightbox');
        createLightbox.style.position = 'absolute';
        createLightbox.style.top      = 0 + 'px';
        createLightbox.style.left     = 0 + 'px';

    document.body.appendChild(createLightbox);

    var lightboxObject  = document.getElementById('lightbox');

  }


  if( lightboxObject ) {

    // Set the lightbox to fill the dimensions of the page
    // Later on it will be set to fill the entire document height
    var lightboxStyles              = lightboxObject.style;
    lightboxStyles.position         = 'absolute';
    lightboxStyles.top              = '0px';
    lightboxStyles.left             = '0px';
    lightboxStyles.zIndex           = '990';
    lightboxStyles.display          = 'block';
    lightboxStyles.width            = '100%';
    lightboxStyles.height           = getPageHeight() + 'px';
    lightboxStyles.backgroundColor  = '#000000';
    lightboxStyles.opacity          = '0.1';              // default is 0.7 / 70
    lightboxStyles.filter           = 'alpha(opacity=10)';

    lightboxObject.setAttribute('ondblclick', 'removeLightbox()');

    fadeObject('lightbox', 7, 20);

    // Get the object to load into the lightbox
    // If an object is passed, append it to the lightbox, else load
    // it as if it is an ID already appearing in the proper place.
    if( typeof(contentData) == 'object' ) {

      var contentObject = contentData;

      document.body.appendChild(contentObject);

    } else {

      var contentObject = document.getElementById(contentData);

    }

    if( contentObject ) {

      var contentStyle            = contentObject.style;
      contentStyle.visibility = 'hidden'; // while processing to avoid jumps
      contentStyle.position   = 'absolute';
      contentStyle.zIndex     = '991';
      contentStyle.display    = 'block';

      // Get the width and height of the content object
      var contentWidth        = contentObject.clientWidth   ? contentObject.clientWidth   : contentObject.offsetWidth;
      var contentHeight       = contentObject.clientHeight  ? contentObject.clientHeight  : contentObject.offsetHeight;

      // Get the width and height of the visible area
      var visibleWidth        = lightboxObject.clientWidth  ? lightboxObject.clientWidth  : lightboxObject.offsetWidth;
      var visibleHeight       = lightboxObject.clientHeight ? lightboxObject.clientHeight : lightboxObject.offsetHeight;
          visibleHeight       = document.documentElement.clientHeight && visibleHeight < document.documentElement.clientHeight ?
                                document.documentElement.clientHeight : visibleHeight;

      // Center the content inside of the visible page area
      var marginTop           = visibleHeight >= contentHeight ?
                                Math.round((visibleHeight - contentHeight) / 2) : 20;

      var marginLeft          = Math.round((visibleWidth - contentWidth) / 2);

      contentStyle.top        = (marginTop + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop)) + 'px';
      contentStyle.left       = marginLeft + 'px';

      // Re-adjust the lightbox height if the content element
      // has strecthed the page beyond it's original size
      var documentHeight      = getPageHeight();
      lightboxStyles.height   = (documentHeight + (contentHeight >= visibleHeight ? 20 : 0)) + 'px';

      // Show the content after all the preparations are complete
      contentStyle.visibility = 'visible';

      lightBoxActive          = true;


      // Hide all the flash when needed to fix issues for Firefox Mac
      for(var hideNumber in flashObjectList) {
        var hideThisObject = document.getElementById(flashObjectList[hideNumber]);
        if( hideThisObject ) {
          hideThisObject.style.visibility = 'hidden';
        }
      }

      if( contentData == 'submit_form' && disallowSubmitCase == true ) {
        readResponse("case|0|We're sorry but you do not meet the requirements necessary to participate in this site.");
      }

/*
      if( visibleHeight > contentHeight ) {

        window.onscroll = function() {
          contentStyle.top    = (marginTop + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop)) + 'px';

        }

      } // ! enable automatic positioning?
*/
    } // ! content object?


  } // ! lightbox object?

} // ! launchLightbox()



/** removeLightbox()
 *
 *  Hide a lightbox that has been opened with launchLightbox()
 *
**/
function removeLightbox(contentID) {

      contentID       = contentID != undefined ? contentID : 'lightbox-content';

  var lightboxObject  = document.getElementById('lightbox');
  var contentObject   = document.getElementById(contentID);

  if( lightboxObject ) {

    if( contentObject ) {

      // document.body.removeChild(contentObject);

      contentStyle          = contentObject.style;
      contentStyle.display  = 'none';
      contentStyle.zIndex   = '0';

    } // ! hide content?

    lightboxStyles            = lightboxObject.style;
    lightboxStyles.display    = 'none';
    lightboxStyles.zIndex     = '0';
    lightboxStyles.width      = '0px';
    lightboxStyles.height     = '0px';

    window.onscroll   = null; // disable scroll detector

    lightBoxActive    = false;

    // Show all the flash that was hidden to fix the Firefox Mac issues
    for(var hideNumber in flashObjectList) {
      var hideThisObject = document.getElementById(flashObjectList[hideNumber]);
      if( hideThisObject ) {
        hideThisObject.style.visibility = 'visible';
      }
    }

  } // ! hide lightbox?

} // ! removeLightbox()




/** getPageHeight()
 *
 *  Detect the height of the page
 *
**/
function getPageHeight() {

  var pageHeight  = document.body.parentNode.scrollHeight;
      pageHeight  = pageHeight > window.innerHeight ? pageHeight : window.innerHeight;
      pageHeight  = pageHeight != undefined ? pageHeight :
        (document.documentElement.scrollHeight > document.documentElement.clientHeight ?
         document.documentElement.scrollHeight : document.documentElement.clientHeight);

  return pageHeight;

} // getPageHeight()

