Event.observe(window, "resize", resizeHandler)

function resizeHandler(event) {
  resizeImg($("bg"));
}

function resizeImg(img) {

  var vpHeight = 0;
  var vpWidth = 0;
  
  var viewportwidth;
  var viewportheight;
  
  // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
  if (typeof window.innerWidth != 'undefined')
  {
    vpWidth = window.innerWidth,
    vpHeight = window.innerHeight
  }
  // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
  else if (typeof document.documentElement != 'undefined'
           && typeof document.documentElement.clientWidth != 'undefined' 
           && document.documentElement.clientWidth != 0)
  {
    vpWidth = document.documentElement.clientWidth,
    vpHeight = document.documentElement.clientHeight
  }
  else // older versions of IE
  {
    vpWidth = document.getElementsByTagName('body')[0].clientWidth,
    vpHeight = document.getElementsByTagName('body')[0].clientHeight
  }
  
  var imgHeight;
  var imgWidth;
  var ratio = img.width / img.height;
  var vpRatio = vpWidth / vpHeight;
  var topOffset = 0;
  var leftOffset = 0;
  
  if (ratio > vpRatio) {
    imgHeight = vpHeight;
    imgWidth = imgHeight * ratio;
    leftOffset = vpWidth - imgWidth;
  }
  else {
    imgWidth = vpWidth;
    imgHeight = imgWidth / ratio;
    topOffset = vpHeight - imgHeight;
  }
  
    
  img.setStyle({
    width: imgWidth + "px",
    height: imgHeight + "px",
    top: topOffset + "px",
    left: leftOffset + "px"
  })

}

