var resizeType, orig_height, orig_width;

function Init() { orig_height = document.thispix.height; orig_width = document.thispix.width; }

function Select(which) {
  if (which == "fit") {
      resizeType = "fit";
      document.pixFormTop.sizeSelectTop.options[0].selected = 1;
      document.pixFormBot.sizeSelectBot.options[0].selected = 1;
  }
  else if (which == "orig") {
      resizeType = "orig";
      document.pixFormTop.sizeSelectTop.options[1].selected = 1;
      document.pixFormBot.sizeSelectBot.options[1].selected = 1;
  }
}

function ResizePhoto(size) {
  var winHeight, winWidth, newHeight, newWidth;

  if ( window.innerHeight ) { winHeight = window.innerHeight-70; winWidth = window.innerWidth-60; }
  else if ( document.body.clientHeight ) { winHeight = document.body.clientHeight-70; winWidth = document.body.clientWidth-60; }
  else { return; }

  if (size=="orig") {
      newHeight = orig_height;
      newWidth  = orig_width;
      Select("orig");
  }
  else if (size=="fit") {
      if (winWidth < 200) { winWidth = 200; }
      if (winHeight < 200) { winHeight = 200; }
      if (winWidth > 800) { winWidth = 800; }
      if (winHeight > 600) { winHeight = 600; }

      var ratio = winWidth / document.thispix.width;
      if (winHeight / document.thispix.height < ratio)
          ratio = winHeight / document.thispix.height;

      newWidth  = document.thispix.width  * ratio;
      newHeight = document.thispix.height * ratio;

      Select("fit");
  }
  else {
      fitWindowRatio = winWidth / orig_width;
      if (winHeight / orig_height < fitWindowRatio)
          fitWindowRatio = winHeight / orig_height;

      if (fitWindowRatio < 1) {
          Select("fit");
          newWidth  = orig_width  * fitWindowRatio;
          newHeight = orig_height * fitWindowRatio;
      }
      else {
          Select("orig");
          newWidth  = orig_width;
          newHeight = orig_height;
      }
  }

  document.thispix.width  = newWidth;
  document.thispix.height = newHeight;

  if (document.frame1) { document.frame1.width  = newWidth; }
  if (document.frame3) { document.frame3.height = newHeight; }
  if (document.frame4) { document.frame4.height = newHeight; }
  if (document.frame6) { document.frame6.width  = newWidth; }
}