/*
http://simonwillison.net/2004/May/26/addLoadEvent/
*/
function addLoadEvent(func)
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function')
  {
    window.onload = func;
  }
  else
  {
    window.onload = function()
    {
      if (oldonload)
      {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(photo_setup);

function photo_setup()
{
  var elObj;
  var elObjLimit;
  var elObjReg;

  if (!document.getElementById)
  {
    return;
  }
  if (elObj = document.getElementById("footer"))
  {
    if (elObjLimit = document.getElementById("thumbnailbox"))
    {
      if (elObjReg = elObjLimit.getElementsByTagName("a"))
      {
        for (var i=0;i<elObjReg.length;i++)
        {
          var elObjHref = elObjReg[i].href;
          elObjReg[i].onclick = function () { return pictureOpen(this.href); }
//          elObjReg[i].onclick = function () { window.open(this.href); return false; }
        }
      }
    }
  }
  else
  {
    setTimeout("photo_setup()",10);
  }
}


/***************
 * image popup *
 ***************/

PWin = null;
//width should exceed widest image in gallery, and height should exceed tallest
PWinHeight = 575;
PWinWidth = 575;
//make sure this one is kept synchronised with the page margins in gallery_item.asp
PWinMargin = 5;

function pictureOpen(pname)
{
  pictureClose();

  PWin = window.open(pname+'&resize=true','PWin','width='+PWinWidth+',height='+PWinHeight+',scrollbars=0,toolbars=0,resizable=1');
  PWin.focus();
  return false;
}

function pictureClose()
{
  if (PWin)
    PWin.close();
}

function FitPic(aHasCaption) {
//iWidth and iHeight are window height and width specified in gallery.js, minus page margins
  iWidth = PWinWidth - (2 * PWinMargin);
  iHeight = PWinHeight - (2 * PWinMargin);
  iWidth = document.images[0].width - iWidth;
  iHeight = document.images[0].height - iHeight;
  if (aHasCaption) iHeight = iHeight + 20;
  window.resizeBy(iWidth, iHeight);
  self.focus();
};
