// Code adapted from http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
var nextImage = new Image();

function hideImage()
{
  document.getElementById('pagegrey').style.visibility = 'hidden';
  document.getElementById('imageframe').style.visibility = 'hidden';
}

function winD(width,height,x,y)
{
    this.width = width;
    this.height = height;
    this.x = x;
    this.y = y;
}

function getWindowDimension()
{
  // get window width height and scrolling
  var scrOfX = 0, scrOfY = 0;  
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return new winD(myWidth,myHeight,scrOfX,scrOfY);
}

function showImage(url)
{
  
  var wn = getWindowDimension(); 
  var pagegrey = document.getElementById('pagegrey'); 
  var pimage = document.getElementById('pageimage');
  var imageframe = document.getElementById('imageframe');
  imageframe.style.visibility = "hidden";
  pimage.src = "about:blank";
  nextImage = new Image();
  nextImage.src = url;
  pagegrey.style.top = wn.y+"px";
  pagegrey.style.left = wn.x+"px";
  pagegrey.style.height = wn.height+"px";
  pagegrey.style.width = wn.width+"px";
  imageframe.style.width = wn.width+"px";
  pagegrey.style.visibility="visible";
  imageframe.style.top = (wn.y + 15)+"px";
  window.setTimeout('checkImage()',20);
  return false;
}

function checkImage()
{
    var wn = getWindowDimension(); 
    var pagegrey = document.getElementById('pagegrey');
    var pimage = document.getElementById('pageimage');
    var imageframe = document.getElementById('imageframe');
    if(pagegrey.style.visibility=="hidden")
    {
       return false;
    }
    if(nextImage.complete)
    {
        pimage.src = nextImage.src;
	iwidth = nextImage.width;
	iheight = nextImage.height;
	wratio = (wn.width * 1.0) / (wn.height * 1.0);
	iratio = (iwidth * 1.0) / (iheight * 1.0);
	if(wratio > iratio)
	{
            pimage.style.height = (wn.height - 60)+"px";
	    pimage.style.width = Math.floor(((wn.height - 60)*1.0)*iratio)+"px";
	}
	else
	{
            pimage.style.width = (wn.width - 60)+"px";
	    pimage.style.height = Math.floor(((wn.width - 60)*1.0)/iratio)+"px";
	}
        imageframe.style.visibility="visible";
    }
    else
    {
        window.setTimeout('checkImage()',50);
    }
}
