var g_wpmode = 0;
var g_orgwidth = 0;
var g_orgheight = 0;
function showWallpaperLegacy(wallpaperurl, mode) {
  g_wpmode = mode;
  var wpstyle = document.body.style;
  if(mode <= 1) {
    wpstyle.backgroundImage = "url(" + wallpaperurl + ")";
    if(mode == 0) {
      wpstyle.backgroundPosition = "center";
      wpstyle.backgroundRepeat = "no-repeat";
      document.documentElement.style.minHeight = "100%";
    }
  }
  else {
    var elemdiv = document.createElement("div");
    elemdiv.setAttribute("id", "background-wrapper");
    var elemimg = document.createElement("img");
    elemimg.setAttribute("id", "bg-img");
    elemimg.setAttribute("src", wallpaperurl);
    elemimg.runtimeStyle.width = "auto";
    elemimg.runtimeStyle.height = "auto";
    elemimg.onload = onBgLoaded;
    if(elemimg.width > 0) {
      onBgLoaded();
    }
    elemdiv.appendChild(elemimg);
    document.body.insertBefore(elemdiv, document.body.firstChild);
  }
  function onBgLoaded() {
    // Get natural size
    g_orgwidth = elemimg.width;
    g_orgheight = elemimg.height;
    var ratio = getImageRatio();
    elemimg.width = g_orgwidth * ratio;
    elemimg.height = g_orgheight * ratio;
    elemimg.runtimeStyle.width = "";
    elemimg.runtimeStyle.height = "";

    var lowestwidth = parseInt(document.getElementById("body_wrapper").currentStyle.width);
    elemdiv.style.width = lowestwidth > document.documentElement.clientWidth ?
                          lowestwidth : document.documentElement.clientWidth;
    elemdiv.style.height = document.documentElement.clientHeight;
    elemimg.style.left = (document.documentElement.clientWidth - elemimg.width) / 2;
    elemimg.style.top = (document.documentElement.clientHeight - elemimg.height) / 2;
    document.body.onresize = onresize4ie8;
  }
}
function onresize4ie8() {
  // resize background image
  var elemdiv = document.getElementById("background-wrapper");
  var elemimg = document.getElementById("bg-img");
  var ratio = getImageRatio();
  elemimg.width = g_orgwidth * ratio;
  elemimg.height = g_orgheight * ratio;
  var lowestwidth = parseInt(document.getElementById("body_wrapper").currentStyle.width);
  elemdiv.style.width = lowestwidth > document.documentElement.clientWidth ?
                        lowestwidth : document.documentElement.clientWidth;
  elemdiv.style.height = document.documentElement.clientHeight;
  elemimg.style.left = (document.documentElement.clientWidth - elemimg.width) / 2;
  elemimg.style.top = (document.documentElement.clientHeight - elemimg.height) / 2;
}
function getImageRatio() {
  var ratio = 1.0;
  if(g_wpmode == 3) {
    ratio = Math.max(document.documentElement.clientWidth / g_orgwidth,
                     document.documentElement.clientHeight / g_orgheight);
  }
  else {
    ratio = Math.min(document.documentElement.clientWidth / g_orgwidth,
                     document.documentElement.clientHeight / g_orgheight);
  }
  return ratio;
}
