var winhandle = null;

function fitWindowOnScreen(aWindow) {

  if (aWindow.outerWidth) {

    if ((aWindow.outerWidth  > screen.availWidth) ||
        (aWindow.outerHeight > screen.availHeight)) {

      lNewWidth  = Math.min(aWindow.outerWidth,  screen.availWidth);
      lNewHeight = Math.min(aWindow.outerHeight, screen.availHeight);

      aWindow.resizeTo(lNewWidth, lNewHeight);

      aWindow.moveTo((screen.availWidth - lNewWidth) / 2, (screen.availHeight - lNewHeight) / 2);

    }

  }
  else
  {

    if ((aWindow.document.body.offsetWidth  + 50  > screen.availWidth) ||
        (aWindow.document.body.offsetHeight + 200 > screen.availHeight)) {

      lOldOffsetWidth  = aWindow.document.body.offsetWidth;
      lOldOffsetHeight = aWindow.document.body.offsetHeight;

      aWindow.resizeTo(lOldOffsetWidth, lOldOffsetHeight);

      lBorderWidth  = lOldOffsetWidth  - aWindow.document.body.offsetWidth;
      lBorderHeight = lOldOffsetHeight - aWindow.document.body.offsetHeight;

      lOldHeight = lOldOffsetHeight + lBorderHeight;
      lOldWidth  = lOldOffsetWidth  + lBorderWidth;

      lNewWidth  = Math.min(lOldWidth,  screen.availWidth);
      lNewHeight = Math.min(lOldHeight, screen.availHeight);

      aWindow.resizeTo(lNewWidth, lNewHeight);

      aWindow.moveTo((screen.availWidth - lNewWidth) / 2, (screen.availHeight - lNewHeight) / 2);

    }
  }
}


function popUp(aURL, aImageWidth, aImageHeight) {

  var lWindowWidth  = Math.min(aImageWidth,  screen.availWidth);
  var lWindowHeight = Math.min(aImageHeight, screen.availHeight);

  if ((winhandle != null) && (! winhandle.closed)) {

    winhandle.document.body.style.overflow = 'hidden';

    winhandle.document.images["arlesimage"].src = aURL;

    if (winhandle.innerWidth) {
      deltaWidth  = lWindowWidth  - winhandle.innerWidth;
      deltaHeight = lWindowHeight - winhandle.innerHeight;
    }
    else {
      deltaWidth  = lWindowWidth  - winhandle.document.body.clientWidth;
      deltaHeight = lWindowHeight - winhandle.document.body.clientHeight;
    }

    winhandle.resizeBy(deltaWidth, deltaHeight);

    fitWindowOnScreen(winhandle);

    winhandle.document.body.style.overflow = 'auto';


  }
  else
  {
    var lOptions = 'resizable,scrollbars,width=' + lWindowWidth + ', height=' + lWindowHeight;
    winhandle = window.open("", "imagewin", lOptions);

    with (winhandle.document){

      writeln('<html><head>');
      writeln('<title>Image<\/title>');
      writeln('<meta http-equiv="imagetoolbar" content="no">');
      writeln('<\/head>');
      writeln('<body bgcolor="#000000" topmargin="0" leftmargin="0" style="margin:0px;">');
      writeln('<a href="javascript:window.close();"><img name="arlesimage" src="' + aURL + '" style="display:block" border="0"> <\/a>');
      writeln('<\/body><\/html>');

      body.style.overflow = 'auto';

      fitWindowOnScreen(winhandle);

      close();
    }
  }

  winhandle.focus();

}

function PageLoaded() {
  imagepreload = new Image;
  imagepreload.src = "gallery/images/Web%201_award2008.JPG";
}

window.onload=PageLoaded;