
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 showOverlay(link) {
  var outer = document.getElementById('overlayOuter');
  var inner = document.getElementById('overlayInner');
  var image = document.getElementById('overlayImage');

  
  var arrayPageSize = getPageSize();
  var arrayPageScroll = getPageScroll();

  outer.style.height = (arrayPageSize[1] + 'px');
  outer.style.display = 'block';

  // preload image
  imgPreload = new Image();

  imgPreload.onload = function() {
    image.src = link.href;

    // center lightbox and make sure that the top and left values are not negative
    // and the image placed outside the viewport
    var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
    var lightboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);
    
    inner.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
    inner.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
    
    inner.style.backgroundColor = "#eee";
    inner.style.padding = "10px";
    inner.style.borderWidth = "1px";
    inner.style.borderStyle = "solid";
    inner.style.borderColor = "#444";
  
  
    // 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 (navigator.appVersion.indexOf("MSIE")!=-1)
      pause(250);

    selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++)
      selects[i].style.visibility = "hidden";
  
    inner.style.display = 'block';

    arrayPageSize = getPageSize();
    outer.style.height = (arrayPageSize[1] + 'px');

    return false;
  }

  imgPreload.src = link.href;
}


function hideOverlay() {
  document.getElementById('overlayOuter').style.display = 'none';
  document.getElementById('overlayInner').style.display = 'none';

  selects = document.getElementsByTagName("select");
  for (i = 0; i != selects.length; i++)
    selects[i].style.visibility = "visible";
}


function createOverlay() {
  if (! document.getElementsByTagName) { return; }
  
  var anchors = document.getElementsByTagName("a");

  for (var i = 0; i < anchors.length; i++)
    if (anchors[i].getAttribute("href") && (anchors[i].getAttribute("rel") == "thumbnail"))
      anchors[i].onclick = function () {showOverlay(this); return false;}

  var b = document.getElementsByTagName("body").item(0);
  
  var outer = document.createElement("div");
  outer.setAttribute('id','overlayOuter');
  outer.onclick = function () { hideOverlay(); return false; }
  outer.style.display = 'none';
  outer.style.position = 'absolute';
  outer.style.top = '0';
  outer.style.left = '0';
  outer.style.zIndex = '100';
   outer.style.width = '100%';
  outer.style.height = '100%';
  b.insertBefore(outer, b.firstChild);
  
  var arrayPageSize = getPageSize();
  var arrayPageScroll = getPageScroll();


  var inner = document.createElement("div");
  inner.setAttribute('id','overlayInner');
  inner.style.display = 'none';
  inner.style.position = 'absolute';
  inner.style.zIndex = '200';  
  inner.onclick = function () { hideOverlay(); return false; }
  b.insertBefore(inner, outer.nextSibling);
  
  var image = document.createElement("img");
  image.setAttribute('id','overlayImage');
  image.style.border = 'none';
  image.style.clear = 'both';
  inner.appendChild(image);

}


function loadOverlay(func) {  
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
      window.onload = func;
  }
  else {
    window.onload = function() {
      oldonload();
      func();
    }
  }

}


loadOverlay(createOverlay);

