/*
 * Lightbox JS v1.5: Fullsize Image Overlays
 * by James G. Kim - http://JayG.org
 *
 * Released under a Creative Commons License
 * http://creativecommons.org/licenses/by-nc-sa/2.5/
 *
 * Credits
 *
 * Based on Greased Lightbox by Joe Lencioni (http://shiftingpixel.com),
 * Lightbox JS by Lokesh Dhakar (http://www.huddletogether.com),
 * and Flickrbox by Gavin Montague (http://www.leftbrained.co.uk)
 *
 * Loading image by Sean McBride (http://www.alwaysbeta.com)
 * Extra thanks to Justin Delegard, Sami Haahtinen, and Matt Parrett
 */

var lightbox =
{
  /*
   * images
   */
  images:
  {
    loading : 'http://gogistudio.com/wp/wp-content/plugins/wp-lightbox/loading.gif'
  },

  /*
   * messages
   */
  messages:
  {
    loading    : 'Loading Image',
    loadingSub : 'Click Anywhere to Cancel',
    quit       : 'Click to Close',
    context    : 'View Image in Its Original Context',
    error      : 'Image Unavailable'
  },

  keyLeft              : 37,
  keyRight             : 39,
  isShowing            : false,
  lastMove             : 1,
  currentImageLink     : null,
  currentImagePosition : 0,
  aspectRatio          : null,
  allImageLinks        : new Array(0),
  originalLightboxSize : null,
  objOverlay           : null,
  objLoading           : null,
  objError             : null,
  objErrorContext      : null,
  objLightbox          : null,
  objImage             : null,
  objPreload           : null,
  objPrefetch          : null,
  objCaption           : null,

  /*
   * addEventHandler(element, event, handler)
   */
  addEventHandler: function(element, event, handler)
  {
    if (('keypress' == event) && (navigator.appVersion.match(/Konqueror|Safari|KHTML/) || element.detachEvent))
    {
      event = 'keydown';
    }

    if (element.addEventListener)
    {
      element.addEventListener(event, handler, false);
    }
    else
    {
      var DOMEvent =
        function(currentTarget)
        {
          this.currentTarget   = currentTarget;
          this.preventDefault  = function() { window.event.returnValue = false; };
          this.stopPropagation = function() { window.event.cancelBubble = true; };
          this.which           = window.event.keyCode;
          this.keyCode         = window.event.keyCode;

          return this;
        };

      eval('element.on'+event+' = function() { handler(new DOMEvent(element)); };');
    }
  },

  /*
   * removeEventHandler(element, event, handler)
   */
  removeEventHandler: function(element, event, handler)
  {
    if (('keypress' == event) && (navigator.appVersion.match(/Konqueror|Safari|KHTML/) || element.detachEvent))
    {
      event = 'keydown';
    }

    if (element.removeEventListener)
    {
      element.removeEventListener(event, handler, false);
    }
    else
    {
      eval('element.on'+event+' = null;');
    }
  },

  /*
   * getPageScroll()
   * Returns array with x,y page scroll values.
   * Core code from - quirksmode.org
   */
  getPageScroll: function()
  {
    var xScroll = 0;
    var yScroll = 0;

    if (self.pageYOffset || self.pageXOffset)
    {
      xScroll = self.pageXOffset;
      yScroll = self.pageYOffset;
    }
    /* Explorer 6 Strict Mode */
    else if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft))
    {
      xScroll = document.documentElement.scrollLeft;
      yScroll = document.documentElement.scrollTop;
    }
    else if (document.body)
    {
      xScroll = document.body.scrollLeft;
      yScroll = document.body.scrollTop;
    }

    return new Array(xScroll, yScroll);
  },

  /*
   * getPageSize()
   * Returns array with page width, height and window width, height
   * Core code from - quirksmode.org
   * Edit for Firefox by pHaez
   */
  getPageSize: function()
  {
    var xScroll, yScroll;
    var windowWidth, windowHeight;
    var pageHeight, pageWidth;

    if ((window.innerHeight && window.scrollMaxY) || (window.innerWidth && window.scrollMaxX))
    {
      xScroll = window.innerWidth  + window.scrollMaxX;
      yScroll = window.innerHeight + window.scrollMaxY;
    }
    /* all but Explorer Mac */
    else if ((document.body.scrollHeight > document.body.offsetHeight) || (document.body.scrollWidth > document.body.offsetWidth))
    {
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
    }
    /* Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari */
    else
    {
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;

      if (-1 != navigator.appVersion.indexOf('MSIE'))
      {
        var marginTop    = document.body.currentStyle.marginTop;
        var marginLeft   = document.body.currentStyle.marginLeft;
        var marginRight  = document.body.currentStyle.marginRight;
        var marginBottom = document.body.currentStyle.marginBottom;

        xScroll = xScroll + parseInt(marginLeft.substring(0, marginLeft.length - 2));
        xScroll = xScroll + parseInt(marginRight.substring(0, marginRight.length - 2));
        yScroll = yScroll + parseInt(marginTop.substring(0, marginTop.length - 2));
        yScroll = yScroll + parseInt(marginBottom.substring(0, marginBottom.length - 2));
      }
    }

    /* all except Explorer */
    if (self.innerHeight || self.innerWidth)
    {
      windowWidth  = self.innerWidth;
      windowHeight = self.innerHeight;
    }
    /* Explorer 6 Strict Mode */
    else if (document.documentElement && (document.documentElement.clientHeight || document.documentElement.clientWidth))
    {
      windowWidth  = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
    }
    else
    {
      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 = windowWidth;
    }
    else
    {
      pageWidth = xScroll;
    }

    if ((self.innerWidth) && ((yScroll - windowHeight) > 20))
    {
      pageWidth   = pageWidth   - 20;
      windowWidth = windowWidth - 20;
    }

    if ((self.innerHeight) && ((xScroll - windowWidth) > 20))
    {
      pageHeight   = pageHeight   - 20;
      windowHeight = windowHeight - 20;
    }

    return new Array(pageWidth, pageHeight, windowWidth, windowHeight);
  },

  /*
   * pause(numberMillis)
   * Pauses code execution for specified time. Uses busy waiting, not good.
   * Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
   */
  pause: function(numberMillis)
  {
    var now      = new Date();
    var exitTime = now.getTime() + numberMillis;

    while (true)
    {
      now = new Date();
      if (now.getTime() > exitTime) return;
    }
  },

  /*
   * center(objToCenter, arrayPageScroll, arrayPageSize)
   * Centers the object in the page
   */
  center: function(objToCenter, arrayPageScroll, arrayPageSize)
  {
    var objTop  = arrayPageScroll[1] + ((arrayPageSize[3] - objToCenter.offsetHeight) / 2);
    var objLeft = arrayPageScroll[0] + ((arrayPageSize[2] - objToCenter.offsetWidth) / 2);

    if (objTop <= 0)
    {
      objToCenter.style.top = 0;
    }
    else
    {
      objToCenter.style.top = objTop+'px';
    }

    if (objLeft <= 0)
    {
      objToCenter.style.left = 0;
    }
    else
    {
      objToCenter.style.left = objLeft+'px';
    }
  },

  /*
   * fitToPage(arrayPageScroll, arrayPageSize)
   * Resize the overlay to fit a page.
   */
  fitToPage: function(arrayPageScroll, arrayPageSize)
  {
    var objTop  = arrayPageScroll[1] + ((arrayPageSize[3] - lightbox.objLightbox.offsetHeight) / 2);
    var objLeft = arrayPageScroll[0] + ((arrayPageSize[2] - lightbox.objLightbox.offsetWidth) / 2);

    if (objTop  < 0) objTop  = 0;
    if (objLeft < 0) objLeft = 0;

    /* if it went bigger than the page */
    if ((objLeft + lightbox.objLightbox.offsetWidth) > lightbox.objOverlay.offsetWidth)
    {
      lightbox.objOverlay.style.width = (objLeft + lightbox.objLightbox.offsetWidth)+'px';
    }

    if ((objTop + lightbox.objLightbox.offsetHeight) > lightbox.objOverlay.offsetHeight)
    {
      lightbox.objOverlay.style.height = (objTop + lightbox.objLightbox.offsetHeight)+'px';
    }
  },

  /*
   * resize(event, resizeByAmount)
   */
  resize: function(event, resizeByAmount)
  {
    event.stopPropagation();
    event.preventDefault();

    /* Resize the image. */
    if (!lightbox.aspectRatio)
    {
      lightbox.aspectRatio = lightbox.objImage.height / lightbox.objImage.width;
    }

    if (0 == resizeByAmount)
    {
      lightbox.objImage.removeAttribute('width');
      lightbox.objImage.removeAttribute('height');
      lightbox.objLightbox.style.minWidth = lightbox.objImage.width+'px';
      if (-1 != navigator.appVersion.indexOf('MSIE'))
      {
        lightbox.objLightbox.style.width = lightbox.objImage.width+'px';
      }
    }
    else
    {
      var newWidth  = lightbox.objImage.width + (lightbox.objImage.width * (resizeByAmount / 100));
      var newHeight = lightbox.aspectRatio * newWidth;

      if ((newWidth > 30) || (newHeight > 30))
      {
        lightbox.objImage.width             = newWidth;
        lightbox.objImage.height            = newHeight;
        lightbox.objLightbox.style.minWidth = newWidth+'px';
        if (-1 != navigator.appVersion.indexOf('MSIE'))
        {
          lightbox.objLightbox.style.width = newWidth+'px';
        }
      }
    }

    /* Re-center the lightbox. */
    var arrayPageSize   = lightbox.getPageSize();
    var arrayPageScroll = lightbox.getPageScroll();

    /* twice for accuracy */
    lightbox.center(lightbox.objLightbox, arrayPageScroll, arrayPageSize);
    lightbox.center(lightbox.objLightbox, arrayPageScroll, arrayPageSize);

    /* Set width and height of Overlay to take up whole page. */
    lightbox.objOverlay.style.width  = '100%';
    lightbox.objOverlay.style.height = arrayPageSize[1]+'px';

    lightbox.fitToPage(arrayPageScroll, arrayPageSize);
  },

  /*
   * moveSlide(event, moveByAmount)
   * Loads another image from allImageLinks[].
   */
  moveSlide: function(event, moveByAmount)
  {
    if (lightbox.allImageLinks.length > 1)
    {
      lightbox.lastMove = moveByAmount;

      if (-1 == (lightbox.currentImagePosition + moveByAmount))
      {
        lightbox.currentImagePosition = lightbox.allImageLinks.length;
      }

      var newSlidePosition = (lightbox.currentImagePosition + moveByAmount) % lightbox.allImageLinks.length;
      var slideToLoad      = lightbox.allImageLinks[newSlidePosition];

      lightbox.hide(event);
      lightbox.show(slideToLoad);
    }
  },

  /*
   * noImage(event)
   * Displays a nice error message when no image can be found.
   */
  noImage: function(event)
  {
    var image           = lightbox.currentImageLink.getAttribute('href');
    var arrayPageSize   = lightbox.getPageSize();
    var arrayPageScroll = lightbox.getPageScroll();

    lightbox.objError.style.visibility = 'hidden';
    lightbox.objError.style.display    = 'block';
    lightbox.objErrorContext.innerHTML = '<a href="'+image+
                                         '" style="color:#444444; background-color:transparent; '+
                                         'text-decoration:none; border-bottom:1px solid #777777;">'+
                                         lightbox.messages.context+'</a>';

    lightbox.center(lightbox.objError, arrayPageScroll, arrayPageSize);

    lightbox.objLoading.style.display  = 'none';
    lightbox.objError.style.visibility = 'visible';
  },

  /*
   * fitToWindow(event)
   * Resize an image to fit a window if too big.
   */
  fitToWindow: function(event)
  {
    var arrayPageSize   = lightbox.getPageSize();
    var arrayPageScroll = lightbox.getPageScroll();
    var diffWidth       = arrayPageSize[2] - lightbox.originalLightboxSize[0];
    var diffHeight      = arrayPageSize[3] - lightbox.originalLightboxSize[1];
    var resizeByAmount  = 0;

    event.stopPropagation();
    event.preventDefault();

    if ((diffWidth < 0) || (diffHeight < 0))
    {
      diffWidth  = arrayPageSize[2] - lightbox.objLightbox.offsetWidth;
      diffHeight = arrayPageSize[3] - lightbox.objLightbox.offsetHeight;

      if (diffWidth < diffHeight)
      {
        var newWidth   = arrayPageSize[2] - (lightbox.objLightbox.offsetWidth - lightbox.objImage.width);
        resizeByAmount = (newWidth - lightbox.objImage.width) / lightbox.objImage.width * 100;
      }
      else
      {
        if (!lightbox.aspectRatio)
        {
          lightbox.aspectRatio = lightbox.objImage.height / lightbox.objImage.width;
        }

        var newHeight  = arrayPageSize[3] - (lightbox.objLightbox.offsetHeight - lightbox.objImage.height);
        var newWidth   = newHeight / lightbox.aspectRatio;
        resizeByAmount = (newWidth - lightbox.objImage.width) / lightbox.objImage.width * 100;
      }

      if (0 != resizeByAmount)
      {
        lightbox.resize(event, resizeByAmount);
      }
      else
      {
        lightbox.center(lightbox.objLightbox, arrayPageScroll, arrayPageSize);
      }
    }
    else
    {
      lightbox.resize(event, resizeByAmount);
    }
  },

  /*
   * preloader(event)
   * preload image
   */
  preloader: function(event)
  {
    var image   = lightbox.currentImageLink.getAttribute('href');
    var caption = lightbox.currentImageLink.getAttribute('title');

    lightbox.aspectRatio  = null;
    lightbox.objImage.src = image;
    lightbox.objImage.removeAttribute('width');
    lightbox.objImage.removeAttribute('height');

    if (caption)
    {
      lightbox.objCaption.innerHTML = caption;
    }
    else
    {
      lightbox.objCaption.innerHTML = image;
    }

    /*
     * Center lightbox and make sure that the top and left values are not negative
     * and the image placed outside the viewport.
     */
    lightbox.objLightbox.style.visibility = 'hidden';
    lightbox.objLightbox.style.display    = 'block';
    lightbox.objCaption.style.display     = 'block';

    /* Save original lightbox size. */
    lightbox.originalLightboxSize = new Array(lightbox.objLightbox.offsetWidth, lightbox.objLightbox.offsetHeight);

    /* twice for accuracy */
    lightbox.fitToWindow(event);
    lightbox.fitToWindow(event);

    /*
     * A small pause between the image loading and displaying is required with IE,
     * this prevents the previous image displaying for a short burst causing flicker.
     */
    if (-1 != navigator.appVersion.indexOf('MSIE'))
    {
      lightbox.pause(250);
    }

    lightbox.objLoading.style.display     = 'none';
    lightbox.objError.style.display       = 'none';
    lightbox.objLightbox.style.visibility = 'visible';

    /* Clean it up a bit for memory's sake. */
    lightbox.removeEventHandler(lightbox.objPreload, 'load', lightbox.preloader);
    lightbox.removeEventHandler(lightbox.objPreload, 'error', lightbox.noImage);
    lightbox.objPreload.src = '';

    return false;
  },

  /*
   * show(event)
   * Preloads images. Pleaces new image in lightbox then centers and displays.
   */
  show: function(event)
  {
    /* Let ctrl+click type of actions through without lightbox. */
    if (event.metaKey || event.shiftKey || event.altKey || event.ctrlKey)
    {
      return true;
    }

    if (event.currentTarget)
    {
      event.stopPropagation();
      event.preventDefault();

      lightbox.currentImageLink = event.currentTarget;
    }
    else
    {
      lightbox.currentImageLink = event;
    }

    lightbox.isShowing        = true;

    var image           = lightbox.currentImageLink.getAttribute('href');
    var arrayPageSize   = lightbox.getPageSize();
    var arrayPageScroll = lightbox.getPageScroll();

    /* Set height of Overlay to take up whole page and show. */
    lightbox.objOverlay.style.display = 'block';
    lightbox.objOverlay.style.width   = arrayPageSize[0]+'px';
    lightbox.objOverlay.style.height  = arrayPageSize[1]+'px';

    /* Center loader and error message. */
    lightbox.objLoading.style.visibility = 'hidden';
    lightbox.objLoading.style.display    = 'block';

    lightbox.center(lightbox.objLoading, arrayPageScroll, arrayPageSize);

    lightbox.objLoading.style.visibility = 'visible';

    if (lightbox.objPreload.src != image)
    {
      lightbox.addEventHandler(lightbox.objPreload, 'load', lightbox.preloader);
      lightbox.addEventHandler(lightbox.objPreload, 'error', lightbox.noImage);
      lightbox.objPreload.src = image;
    }
    else
    {
      preloaderDone();
    }

    /* Hide select boxes as they will 'peek' through the image in IE. */
    var selects = document.getElementsByTagName('select');
    for (var i = 0; i < selects.length; i++)
    {
      selects[i].style.visibility = 'hidden';
    }

    /* Hides flash movies that peek through the overlay. */
    var objects = document.getElementsByTagName('object');
    for (var i = 0; i < objects.length; i++)
    {
      objects[i].style.visibility = 'hidden';
    }

    /* Initialize slideshow. */
    if (lightbox.allImageLinks.length > 1)
    {
      /* Set currentImagePosition. */
      findCurrentPosition: for (var i = 0; i < lightbox.allImageLinks.length; i++)
      {
        if (lightbox.allImageLinks[i] == lightbox.currentImageLink)
        {
          lightbox.currentImagePosition = i;
          break findCurrentPosition;
        }
      };

      /* Pre-fetch next image. */
      var nextImagePosition = (lightbox.currentImagePosition + lightbox.lastMove) % lightbox.allImageLinks.length;
      if (-1 == nextImagePosition)
      {
        nextImagePosition = lightbox.allImageLinks.length - 1;
      }

      var nextImage = lightbox.allImageLinks[nextImagePosition];

      lightbox.objPrefetch.src = nextImage.getAttribute('href');
    }
  },

  /*
   * hide(event)
   * Stops the preloader in case it hasn't finished and then hides all of the lightbox components.
   */
  hide: function(event)
  {
    event.stopPropagation();
    event.preventDefault();

    lightbox.isShowing = false;

    /* Stop preloader. */
    lightbox.removeEventHandler(lightbox.objPreload, 'load', lightbox.preloader);
    lightbox.removeEventHandler(lightbox.objPreload, 'error', lightbox.noImage);
    lightbox.objPreload.src = '';

    /* Show select boxes again. */
    var selects = document.getElementsByTagName('select');
    for (var i = 0; i < selects.length; i++)
    {
      selects[i].style.visibility = 'visible';
    }

    /* Show flash movies again. */
    var objects = document.getElementsByTagName('object');
    for (var i = 0; i < objects.length; i++)
    {
      objects[i].style.visibility = 'visible';
    }

    /* Hide everything. */
    lightbox.objLoading.style.display  = 'none';
    lightbox.objError.style.display    = 'none';
    lightbox.objOverlay.style.display  = 'none';
    lightbox.objLightbox.style.display = 'none';

    /* Set width and height of Overlay to restore original page size. */
    lightbox.objOverlay.style.width  = '100%';
    lightbox.objOverlay.style.height = '';
  },

  /*
   * handleKey(event)
   * Handles keypress.
   * If 'x' is pressed then it hides the lightbox.
   * If a left or right arrow is pressed it cycles through images on a page.
   */
  handleKey: function(event)
  {
    if (lightbox.isShowing)
    {
      var keycode = event.which;
      var key     = String.fromCharCode(keycode).toLowerCase();

      switch(key)
      {
        case 'x':
          lightbox.hide(event);
          break;
        case '+':
          lightbox.resize(event, 13);
          break;
        case '-':
          lightbox.resize(event, -13);
          break;
        case '0':
          lightbox.resize(event, 0);
          break;
        case 'f':
          /* twice for accuracy */
          lightbox.fitToWindow(event);
          lightbox.fitToWindow(event);
          break;
        default:
          switch(event.keyCode)
          {
            /* 'x' */
            case 88:
            case 120:
              lightbox.hide(event);
              break;
            /* '+' */
            case 107:
            case 187:
              lightbox.resize(event, 13);
              break;
            /* '-' */
            case 109:
            case 189:
              lightbox.resize(event, -13);
              break;
            /* '0' */
            case 48:
            case 96:
              lightbox.resize(event, 0);
              break;
            /* 'f' */
            case 70:
            case 102:
              /* twice for accuracy */
              lightbox.fitToWindow(event);
              lightbox.fitToWindow(event);
              break;
            /* left arrow */
            case lightbox.keyLeft:
              lightbox.moveSlide(event, -1);
              break;
            /* right arrow */
            case lightbox.keyRight:
              lightbox.moveSlide(event, 1);
              break;
          }
          break;
      }
    }
  },

  /*  
   * init()
   * Function runs on window load, going through link tags looking for rel="lightbox".
   * These links receive onclick events that enable the lightbox display for their targets.
   * The function also inserts html markup at the top of the page which will be used as a
   * container for the overlay pattern and the inline image.
   */
  init: function()
  {
    if (!document.getElementsByTagName)
    {
      return;
    }

    /* loop through all anchor tags */
    var anchors = document.getElementsByTagName('a');
    for (var i = 0; i < anchors.length; i++)
    {
      var anchor = anchors[i];

      if (anchor.getAttribute('href') && ('lightbox' == anchor.getAttribute('rel')))
      {
        lightbox.addEventHandler(anchor, 'click', lightbox.show);
        lightbox.allImageLinks.push(anchor);
      }
    }

    if (lightbox.allImageLinks.length == 0)
    {
      return;
    }

    var center =
      function(event)
      {
        if (lightbox.isShowing)
        {
          /* twice for accuracy */
          lightbox.fitToWindow(event);
          lightbox.fitToWindow(event);

          /* Set width and height of Overlay to take up whole page. */
          var arrayPageSize = lightbox.getPageSize();

          lightbox.objOverlay.style.width  = arrayPageSize[0]+'px';
          lightbox.objOverlay.style.height = arrayPageSize[1]+'px';
        }
      };

    lightbox.addEventHandler(window, 'resize', center);

    var objBody = document.getElementsByTagName('body').item(0);

    /* overlay div */
    lightbox.objOverlay = document.createElement('div');
    lightbox.objOverlay.setAttribute('id', 'lightboxOverlay');
    lightbox.objOverlay.setAttribute('title', lightbox.messages.quit);
    lightbox.addEventHandler(lightbox.objOverlay, 'click', lightbox.hide);
    lightbox.objOverlay.style.display          = 'none';
    lightbox.objOverlay.style.position         = 'absolute';
    lightbox.objOverlay.style.top              = '0';
    lightbox.objOverlay.style.left             = '0';
    lightbox.objOverlay.style.width            = '100%';
    lightbox.objOverlay.style.color            = '#FFFFFF';
    lightbox.objOverlay.style.backgroundColor  = '#000000';
    lightbox.objOverlay.style.cursor           = 'pointer';
    lightbox.objOverlay.style.zIndex           = '10000000';
    if ('undefined' != typeof (lightbox.objOverlay.style.opacity))
    {
      lightbox.objOverlay.style.opacity = '0.7';
    }
    else if ('undefined' != typeof (lightbox.objOverlay.style.filter))
    {
      lightbox.objOverlay.style.filter = 'alpha(opacity:70)';
    }
    else if ('undefined' != typeof (lightbox.objOverlay.style.KHTMLOpacity))
    {
      lightbox.objOverlay.style.KHTMLOpacity = '0.7';
    }
    else if ('undefined' != typeof (lightbox.objOverlay.style.MozOpacity))
    {
      lightbox.objOverlay.style.MozOpacity = '0.7';
    }
    else
    {
      lightbox.objOverlay.style.backgroundColor = '#444444';
    }
    objBody.insertBefore(lightbox.objOverlay, objBody.firstChild);

    /* loading div */
    lightbox.objLoading = document.createElement('div');
    lightbox.objLoading.setAttribute('id', 'lightboxLoading');
    lightbox.addEventHandler(lightbox.objLoading, 'click', lightbox.hide);
    lightbox.objLoading.style.display         = 'none';
    lightbox.objLoading.style.position        = 'absolute';
    lightbox.objLoading.style.color           = '#FFFFFF';
    lightbox.objLoading.style.backgroundColor = 'transparent';
    lightbox.objLoading.style.fontWeight      = 'bold';
    lightbox.objLoading.style.fontFamily      = '"Trebuchet MS", Tahoma, Arial, Sans-Serif';
    lightbox.objLoading.style.textAlign       = 'center';
    lightbox.objLoading.style.lineHeight      = '2em';
    lightbox.objLoading.style.zIndex          = '10000050';
    objBody.insertBefore(lightbox.objLoading, objBody.nextSibling);

    /* loading image */
    var loadingImage = document.createElement('img');
    loadingImage.setAttribute('src', lightbox.images.loading);
    loadingImage.removeAttribute('width');
    loadingImage.removeAttribute('height');
    loadingImage.style.borderStyle = 'none';
    lightbox.objLoading.appendChild(loadingImage);

    /* loading message */
    var loadingMessage = document.createElement('p');
    loadingMessage.innerHTML = lightbox.messages.loading;
    loadingMessage.style.margin          = '0';
    loadingMessage.style.padding         = '25px 0 5px 0';
    loadingMessage.style.color           = '#FFFFFF';
    loadingMessage.style.backgroundColor = 'transparent';
    loadingMessage.style.fontSize        = '45px';
    loadingMessage.style.fontWeight      = 'bold';
    loadingMessage.style.fontFamily      = '"Trebuchet MS", Tahoma, Arial, Sans-Serif';
    loadingMessage.style.textAlign       = 'center';
    loadingMessage.style.lineHeight      = '1em';
    lightbox.objLoading.appendChild(loadingMessage);

    /* loading helper message */
    var loadingHelp = document.createElement('p');
    loadingHelp.innerHTML = lightbox.messages.loadingSub;
    loadingHelp.style.margin          = '0';
    loadingHelp.style.padding         = '5px 0';
    loadingHelp.style.color           = '#FFFFFF';
    loadingHelp.style.backgroundColor = 'transparent';
    loadingHelp.style.fontSize        = '11px';
    loadingHelp.style.fontFamily      = '"Trebuchet MS", Tahoma, Arial, Sans-Serif';
    loadingHelp.style.fontWeight      = 'normal';
    loadingHelp.style.textAlign       = 'center';
    loadingHelp.style.lineHeight      = '1em';
    lightbox.objLoading.appendChild(loadingHelp);

    /* error div */
    lightbox.objError = document.createElement('div');
    lightbox.objError.setAttribute('id', 'lightboxError');
    lightbox.objError.style.display         = 'none';
    lightbox.objError.style.position        = 'absolute';
    lightbox.objError.style.padding         = '10px';
    lightbox.objError.style.color           = '#444444';
    lightbox.objError.style.backgroundColor = '#FFFFFF';
    lightbox.objError.style.border          = '1px solid #444444';
    lightbox.objError.style.fontSize        = '11px';
    lightbox.objError.style.fontFamily      = 'Verdana, Sans-Serif';
    lightbox.objError.style.textAlign       = 'center';
    lightbox.objError.style.zIndex          = '10000050';
    if ('undefined' != typeof (lightbox.objError.style.borderRadius))
    {
      lightbox.objError.style.borderRadius = '10px';
    }
    else if ('undefined' != typeof (lightbox.objError.style.MozBorderRadius))
    {
      lightbox.objError.style.MozBorderRadius = '10px';
    }
    objBody.insertBefore(lightbox.objError, objBody.nextSibling);

    /* error message */
    var errorMessage = document.createElement('p');
    lightbox.addEventHandler(errorMessage, 'click', lightbox.hide);
    errorMessage.innerHTML = lightbox.messages.error;
    errorMessage.margin                = '10px 20px';
    errorMessage.style.color           = '#444444';
    errorMessage.style.backgroundColor = 'transparent';
    errorMessage.style.borderStyle     = 'none';
    errorMessage.style.fontSize        = '45px';
    errorMessage.style.fontWeight      = 'bold';
    errorMessage.style.fontFamily      = '"Trebuchet MS", Tahoma, Arial, Sans-Serif';
    errorMessage.style.textAlign       = 'center';
    errorMessage.style.textDecoration  = 'none';
    errorMessage.style.cursor          = 'pointer';
    lightbox.objError.appendChild(errorMessage);

    /* error context link */
    lightbox.objErrorContext = document.createElement('p');
    lightbox.objErrorContext.setAttribute('id', 'lightboxErrorContext');
    lightbox.objErrorContext.style.margin          = '0';
    lightbox.objErrorContext.style.padding         = '5px 0';
    lightbox.objErrorContext.style.color           = '#444444';
    lightbox.objErrorContext.style.backgroundColor = 'transparent';
    lightbox.objErrorContext.style.fontSize        = '11px';
    lightbox.objErrorContext.style.fontWeight      = 'normal';
    lightbox.objErrorContext.style.fontFamily      = '"Trebuchet MS", Tahoma, Arial, Sans-Serif';
    lightbox.objErrorContext.style.textAlign       = 'center';
    lightbox.objErrorContext.style.lineHeight      = '1em';
    lightbox.objError.appendChild(lightbox.objErrorContext);

    /* Create lightbox div. */
    lightbox.objLightbox = document.createElement('div');
    lightbox.objLightbox.setAttribute('id', 'lightbox');
    lightbox.objLightbox.style.display         = 'none';
    lightbox.objLightbox.style.position        = 'absolute';
    lightbox.objLightbox.style.padding         = '10px';
    lightbox.objLightbox.style.color           = '#444444';
    lightbox.objLightbox.style.backgroundColor = '#FFFFFF';
    lightbox.objLightbox.style.border          = '1px solid #444444';
    lightbox.objLightbox.style.fontSize        = '11px';
    lightbox.objLightbox.style.fontFamily      = 'Verdana, Sans-Serif';
    lightbox.objLightbox.style.textAlign       = 'center';
    lightbox.objLightbox.style.zIndex          = '10000050';
    if ('undefined' != typeof (lightbox.objLightbox.style.borderRadius))
    {
      lightbox.objLightbox.style.borderRadius = '10px';
    }
    else if ('undefined' != typeof (lightbox.objLightbox.style.MozBorderRadius))
    {
      lightbox.objLightbox.style.MozBorderRadius = '10px';
    }
    objBody.insertBefore(lightbox.objLightbox, lightbox.objOverlay.nextSibling);

    /* Create empty image. */
    lightbox.objImage = document.createElement('img');
    lightbox.objImage.setAttribute('title', lightbox.messages.quit);
    lightbox.addEventHandler(lightbox.objImage, 'click', lightbox.hide);
    lightbox.objImage.setAttribute('id', 'lightboxImage');
    lightbox.objImage.style.borderStyle = 'none';
    lightbox.objImage.style.cursor      = 'pointer';
    lightbox.objLightbox.appendChild(lightbox.objImage);

    /* Create empty preloader. */
    lightbox.objPreload = document.createElement('img');
    lightbox.objPreload.setAttribute('id', 'lightboxPreload');
    lightbox.objPreload.style.display = 'none';
    objBody.insertBefore(lightbox.objPreload, objBody.firstChild);

    /* Create empty prefetcher. */
    lightbox.objPrefetch = document.createElement('img');
    lightbox.objPrefetch.setAttribute('id', 'lightboxPrefetch');
    lightbox.addEventHandler(lightbox.objPrefetch, 'error', function(event) { return false; });
    lightbox.objPrefetch.style.display = 'none';
    objBody.insertBefore(lightbox.objPrefetch, objBody.firstChild);

    /* Create empty caption. */
    lightbox.objCaption = document.createElement('div');
    lightbox.objCaption.setAttribute('id', 'lightboxCaption');
    lightbox.objCaption.style.margin = '5px 0 0 0';
    lightbox.objLightbox.appendChild(lightbox.objCaption);

    /* keytrapping */
    if (navigator.userAgent.match(/Safari/))
    {
      lightbox.keyLeft  = 63234;
      lightbox.keyRight = 63235;
    }

    lightbox.addEventHandler(document, 'keypress', lightbox.handleKey);
  }
}

/* Adds event to window.onload. */
if (window.addEventListener)
{
  window.addEventListener('load', lightbox.init, false);
}
else
{
  var oldOnload = window.onload;

  if ('function' != typeof (window.onload))
  {
    window.onload = lightbox.init;
  }
  else
  {
    window.onload =
      function()
      {
        oldOnload();
        lightbox.init();
      };
  }
}
