var win = null;

var useLayer = true;

var loadingImageUrl = "/art/arrows.gif";

var preload = new Image();
preload.src = loadingImageUrl;

function showImage( imgsrc, width, height, title )
{
  if( useLayer && typeof( def_layer ) != "undefined" && getwindowwidth() > width && getwindowheight() > height )
  {
    showImageLayer2( imgsrc, width, height, title )
  }
  else
  {
    showImageWindow( imgsrc, width, height, title )
  }
}

function showImageWindow( imgsrc, width, height, title )
{
  if( win != null && !win.closed )
  {
    win.close();
  }

  win = window.open( "", "IMAGEPOP", "width=" + width + ",height=" + height );

  win.document.open( "text/html" );

  win.document.write( '<html><head><title>' +
                      title +
                      '</title><style type="text/css">body{background:white;margin:0px}</style></head><body onload="self.focus()"><img onclick="self.close()" src="' +
                      imgsrc +
                      '" title="' +
                      title + ' (click to close)"/></body></html>' );

  win.document.close();

  return false;
}

function showImageLayer( imgsrc, width, height, title )
{
  closeImageLayer();

  var imageLayer = getElt( "imageLayer" );

  if( imageLayer == null )
  {
    imageLayer = document.createElement( "div" );
    imageLayer.id = "imageLayer";

    imageLayer.style.position   = "absolute";
    imageLayer.style.display    = "none";
    imageLayer.style.overflow   = "hidden";
    imageLayer.style.border     = "1px solid #000000";
    imageLayer.style.background = "#000000 url('" + loadingImageUrl + "') no-repeat 50% 50%";
    imageLayer.style.zIndex     = "10";

    document.body.appendChild( imageLayer );
  }

  imageLayer.innerHTML = '<div style="background:url(\'' + imgsrc + '\') no-repeat 50% 50%" onclick="closeImageLayer()" style="visibility:hidden"><img onload="showImageLayerImage()" src="/art/blank.gif" title="' +
                         title + ' (click to close)" width="' + width + '" height="' + height + '"/></div><div style="background:#e0e0e0;font-weight:bold;padding:4px 2px 4px 2px;border-top:1px solid #000000">' +
                         title +
                         '</div>';

  imageLayer.style.width = width + "px";
  imageLayer.style.top  = ( Math.max( 0, getwindowheight() - height ) / 4 + getpagescrolly() ) + "px";
  imageLayer.style.left = ( Math.max( 0, 993  - width  ) / 2 ) + "px";				// Changed to always centre in content

  backLayer( imageLayer );

  imageLayer.style.display = "";
}

function showImageLayer2( imgsrc, width, height, title )
{
  closeImageLayer();

  imgsrc = imgsrc + "?__cb=" + new Date().getTime();	// Safari onload trick

  var imageLayer = getElt( "imageLayer" );

  if( imageLayer == null )
  {
    imageLayer = document.createElement( "div" );
    imageLayer.id = "imageLayer";

    imageLayer.style.position   = "absolute";
    imageLayer.style.display    = "none";
    imageLayer.style.overflow   = "hidden";
    imageLayer.style.border     = "1px solid #000000";
    imageLayer.style.background = "#000000 url('" + loadingImageUrl + "') no-repeat 50% 50%";
    imageLayer.style.zIndex     = "10";

    imageLayer.onclick = closeImageLayer;

    document.body.appendChild( imageLayer );

    imageLayer.innerHTML = '<img style="visibility:hidden" onload="sizeImageLayerImage()" src="' + imgsrc + '" title="' +
                           title + ' (click to close)" width="240" height="240"/><div id="imageLayerTitle" style="background:#e0e0e0;font-weight:bold;padding:4px 2px 4px 2px;border-top:1px solid #000000">' +
                           title +
                           '</div>';

    imageLayer.style.width = "240px";
    imageLayer.style.top  = ( Math.max( 0, getwindowheight() - 240 ) / 4 + getpagescrolly() ) + "px";
    imageLayer.style.left = ( Math.max( 0, 993  - 240  ) / 2 ) + "px";				// Changed to always centre in content
  }
  else
  {
    imageLayer.firstChild.style.visibility = "hidden";

    imageLayer.firstChild.src   = imgsrc;
    imageLayer.firstChild.title = title + " (click to close)";

    getElt( "imageLayerTitle" ).innerHTML = title
  }

  backLayer( imageLayer );

  imageLayer.style.display = "";
}

function sizeImageLayerImage()
{
  var imageLayer = getElt( "imageLayer" );

  if( imageLayer != null )
  {
    var tmpImg = new Image();
    tmpImg.src = imageLayer.firstChild.src;

    imageLayer.style.top  = ( Math.max( 0, getwindowheight() - tmpImg.height ) / 4 + getpagescrolly() ) + "px";
    imageLayer.style.left = ( Math.max( 0, 993  - tmpImg.width  ) / 2 ) + "px";				// Changed to always centre in content

    imageLayer.firstChild.width  = tmpImg.width;
    imageLayer.firstChild.height = tmpImg.height;

    imageLayer.style.width = tmpImg.width + "px";

    backLayer( imageLayer );

    setTimeout( "showImageLayerImage()", 10 );
  }
}

function showImageLayerImage()
{
  var imageLayer = getElt( "imageLayer" );

  if( imageLayer != null )
  {
    imageLayer.firstChild.style.visibility = "visible";
  }
}

function closeImageLayer()
{
  var imageLayer = getElt( "imageLayer" );

  if( imageLayer != null )
  {
    imageLayer.style.display  = "none";

    unBackLayer( imageLayer );
  }
}
