function getWindowHeight() {
  var windowHeight=0;
  if (typeof(window.innerHeight)=='number') {
    windowHeight=window.innerHeight;
  } else {
    if (document.documentElement&&
	document.documentElement.clientHeight) {
      windowHeight=document.documentElement.clientHeight;
    } else {
      if (document.body&&document.body.clientHeight) {
	windowHeight=document.body.clientHeight;
      }
    }
  }
  return windowHeight;
}

function fixHeightOfTheImage()
{
  var img = document.getElementById("imageimg");
  var div = document.getElementById("imagediv");
  var ratio = img.offsetHeight / img.offsetWidth;
  var current = div.offsetTop + div.offsetHeight;
  var target = getWindowHeight()
  var height = Math.max(480,img.offsetHeight + (target - current)) -10;
  var width = Math.max(640,div.offsetWidth - 10);

  height = height - (height % 10)
  width = width - (width % 10)

  img.src = "./scale?h=" + height + ";w=" + width;
  
  /* Set a cookie with the values */
  c = escape ("h=" + height + ";w=" + width);
  var date = new Date();
  date.setTime(date.getTime()+31536000000);
  var expires = "; expires="+date.toGMTString();
  document.cookie = "imgsize=" + c + expires + "; path=/";
}
window.onresize = fixHeightOfTheImage;
window.onload = fixHeightOfTheImage;

