var loadingImage = '/images/lightbox/loading.gif';
var closeButton = '/images/lightbox/close.gif';

function getPageScroll() {

  var yScroll;

  if (self.pageYOffset) {
    yScroll = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop){  // Explorer 6 Strict
    yScroll = document.documentElement.scrollTop;
  } else if (document.body) {// all other Explorers
    yScroll = document.body.scrollTop;
  }

  arrayPageScroll = new Array('',yScroll)
  return arrayPageScroll;
}

function getPageSize() {

  var xScroll, yScroll;

  if (window.innerHeight && window.scrollMaxY) {
    xScroll = document.body.scrollWidth;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }

  var windowWidth, windowHeight;
  if (self.innerHeight) { // all except Explorer
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    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;
  }

  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
  return arrayPageSize;
}

function pause(numberMillis) {
  var now = new Date();
  var exitTime = now.getTime() + numberMillis;
  while (true) {
    now = new Date();
    if (now.getTime() > exitTime)
      return;
  }
}

function showLoadingImage(fCallback) {

  // prep objects
  var objOverlay = $$$('overlay1');
  var objLightbox = $$$('lightbox1');
  var objLoadingImage = $$$('loadingImage1');

  // set callback if available
  if (fCallback) {
    objOverlay.onclick = fCallback;
    objLoadingImage.onclick = fCallback;
  } else {
    objOverlay.onclick = hideLightbox;
    objLoadingImage.onclick = hideLightbox;
  }

  var arrayPageSize = getPageSize();
  var arrayPageScroll = getPageScroll();

  // center loadingImage if it exists
  if (objLoadingImage) {
    objLoadingImage.style.top =
      (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
    objLoadingImage.style.left =
      (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
    objLoadingImage.style.display = 'block';
  }

  // set height of Overlay to take up whole page and show
  objOverlay.style.height = (arrayPageSize[1] + 'px');
  objOverlay.style.display = 'block';

}

function showLightbox(sContent, sTitle, fCallback) {

  // prep objects
  var objOverlay = $$$('overlay1');
  var objLightbox = $$$('lightbox1');
  var objLightboxTitle = $$$('lightbox_title');
  var objLightboxContent = $$$('lightbox_content');
  var objLoadingImage = $$$('loadingImage1');

  // set callback if available
  if (fCallback) {
    objOverlay.onclick = fCallback;
    objLoadingImage.onclick = fCallback;
  } else {
    objOverlay.onclick = hideLightbox;
    objLoadingImage.onclick = hideLightbox;
  }

  var arrayPageSize = getPageSize();
  var arrayPageScroll = getPageScroll();

  if (sTitle) {
    objLightboxTitle.innerHTML = sTitle;
  }
  objLightboxContent.innerHTML = sContent;

  var lightboxWidth = (500 > (arrayPageSize[0] - 50)) ? (arrayPageSize[0] - 50) : 500;
  var lightboxHeight = (500 > (arrayPageSize[0] - 50)) ? (arrayPageSize[3] - 50) : 500;

  objLightbox.style.width = lightboxWidth + "px";
  objLightboxContent.style.maxHeight = lightboxHeight + "px";

  var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] / 2)  - (lightboxHeight / 2));
  var lightboxLeft = (arrayPageSize[0] / 2) - (lightboxWidth / 2);

  objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
  objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";

  if (objLoadingImage) {
    objLoadingImage.style.display = 'none';
  }

  // Hide select boxes as they will 'peek' through the image in IE
  selects = document.getElementsByTagName("select");
  for (i = 0; i != selects.length; i++) {
    selects[i].style.visibility = "hidden";
  }

  objLightbox.style.display = 'block';

  // After image is loaded, update the overlay height as the new image might have
  // increased the overall page height.
  arrayPageSize = getPageSize();
  objOverlay.style.height = (arrayPageSize[1] + 'px');

}

function hideLightbox() {
  // get objects
  objOverlay = $$$('overlay1');
  objLightbox = $$$('lightbox1');

  // hide lightbox and overlay
  objOverlay.style.display = 'none';
  objLightbox.style.display = 'none';

  // make select boxes visible
  selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
    selects[i].style.visibility = "visible";
  }

}

function initLightbox() {

  var objBody = document.getElementsByTagName("body").item(0);

  // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
  var objOverlay = document.createElement("div");
  objOverlay.setAttribute('id','overlay1');
  objOverlay.onclick = function () {callback(); return false;}
  objOverlay.style.display = 'none';
  objOverlay.style.position = 'absolute';
  objOverlay.style.top = '0';
  objOverlay.style.left = '0';
  objOverlay.style.zIndex = '90';
  objOverlay.style.width = '100%';
  objBody.insertBefore(objOverlay, objBody.firstChild);

  var arrayPageSize = getPageSize();
  var arrayPageScroll = getPageScroll();

  // create loading image
  var objLoadingImage = document.createElement("img");
  objLoadingImage.src = loadingImage;
  objLoadingImage.setAttribute('id','loadingImage1');
  objLoadingImage.style.position = 'absolute';
  objLoadingImage.style.zIndex = '150';
  objOverlay.appendChild(objLoadingImage);

  // create lightbox div, same note about styles as above
  var objLightbox = document.createElement("div");
  objLightbox.setAttribute('id','lightbox1');
  objLightbox.style.display = 'none';
  objLightbox.style.position = 'absolute';
  objLightbox.style.zIndex = '100';
  objBody.insertBefore(objLightbox, objOverlay.nextSibling);

  var objLightboxTitle = document.createElement("div");
  objLightboxTitle.setAttribute('id','lightbox_title');
  objLightbox.appendChild(objLightboxTitle);

  var objLightboxContent = document.createElement("div");
  objLightboxContent.setAttribute('id','lightbox_content');
  objLightbox.appendChild(objLightboxContent);

}