function calcMaxProportions(width, height, maxWidth, maxHeight) {
  if(width > maxWidth || height > maxHeight) {
    if(width / height > maxWidth / maxHeight) {
      k = maxWidth / width;
      width = width * k;
      height = height * k;
    } else {
      k = maxHeight / height;
      width = width * k;
      height = height * k;
    }
  }
  var res = new Array();
  res[0] = width;
  res[1] = height;
  return res;
}

function toggleSelectElements() {
  var els = document.getElementsByTagName("select");
  for(var i=0; i<els.length; i++) {
    if(els[i].getAttribute("prevVisibilityStyle") != null) {
      els[i].style.visibility = els[i].getAttribute("prevVisibilityStyle");
      els[i].removeAttribute("prevVisibilityStyle")
    } else {
      els[i].setAttribute("prevVisibilityStyle", els[i].style.visibility);
      els[i].style.visibility = "hidden";
    }
  }
  var els = document.getElementsByTagName("object");
  for(var i=0; i<els.length; i++) {
    if(els[i].getAttribute("prevVisibilityStyle") != null) {
      els[i].style.visibility = els[i].getAttribute("prevVisibilityStyle");
      els[i].removeAttribute("prevVisibilityStyle")
    } else {
      els[i].setAttribute("prevVisibilityStyle", els[i].style.visibility);
      els[i].style.visibility = "hidden";
    }
  }
}

function initBodyShader() {
  var el = document.createElement("DIV");
  el.className = "bodyShader";
  el.id = "bodyShader";
  el.onclick = new Function("return false;");
  document.body.appendChild(el);
}

function shadeBody() {
  toggleSelectElements();
  var el = document.getElementById("bodyShader");
  el.style.width = document.body.scrollWidth + "px";
  el.style.height = document.body.scrollHeight + "px";
  el.style.display = "block";
}

function unshadeBody() {
  toggleSelectElements();
  var el = document.getElementById("bodyShader");
  el.style.display = "none";
}



function NVL(value, defaultValue) {
  if(typeof(value) == "undefined" || value == null) {
    return defaultValue;
  }
  return value;
}

function onBodyLoad() {
  initBodyShader();
  if(typeof(HTMLArea) != "undefined" && typeof(_html_area_id) != "undefined") {
    if(_html_area_id == '') {
      HTMLArea.replaceAll();
    } else {
      HTMLArea.replace(_html_area_id);
    }
  }
}


function showMaximized(url, width, height) {
  shadeBody();
  var htmlStart =
    "<table id='previewContainer' class='previewContainer' cellpadding='0' cellspacing='0' border='0' style='position: absolute; top: 0; left: 0; visibility: hidden'>" +
    "<tr class='header'><td align='right'><a href='#' onclick='var el = document.getElementById(\"previewContainer\"); el.style.display = \"none\"; el.parentNode.removeChild(el); unshadeBody(); return false;' class='linksluitenpopup'>sluit venster</a></td></tr>" +
    "<tr><td>";
  var htmlEnd = "</td></tr></table>";
  var html;
  if(url.match(/.*\.jpg/i)) {
    html = "<img src='" + url + "' width='" + width + "' height='" + height + "' />";
  } else if(url.match(/.*\.mov/i)) {
    html =
    "<object width='" + width + "' height='" + (height + 16) + "' " +
    " classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B'" +
    " codebase='http://www.apple.com/qtactivex/qtplugin.cab'>" +
    " <param name='src' value='" + url + "'>" +
    " <param name='autoplay' value='true'>" +
    " <param name='controller' value='true'>" +
    "<embed src='" + url + "' width='" + width + "' height='" + (height + 16) + "'" +
    " autoplay='true' controller='true'" +
    " pluginspage='http://www.apple.com/quicktime/download/'></embed></object>";
  } else if(url.match(/.*\.flv/i)) {
    var base = document.getElementsByTagName("base")[0];
    html =
    "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' " +
    " width='" + width + "' height='" + (height + 20) + "'>" +
    "  <param name='movie' value='../../resources/flvplayer.swf?file=" + base.href + url + "&autostart=true'>" +
    "  <param name='autostart' value='true'>" +
    "  <param name='file' value='" + base.href + url + "'>" +
    "  <embed src='../../resources/flvplayer.swf?file=" + base.href + url + "&autostart=true' width='" + width + "' height='" + (height + 20) + "' " +
    "   pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' " +
    "   file='" + url + "' autostart='true'/>" +
    "</object>";
  } else {
    html =
    "<OBJECT width='" + width + "' height='" + (height + 45) + "'" +
    " classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95'" +
    " codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701'" +
    " standby='Loading player components...' type='application/x-oleobject'>" +
    " <param name='fileName' value='" + url + "'>" +
    " <param name='animationatStart' value='true'>" +
    " <param name='transparentatStart' value='true'>" +
    " <param name='showControls' value='true'>" +
    " <EMBED type='application/x-mplayer2' pluginspage='http://microsoft.com/windows/mediaplayer/en/download/'" +
    "   showcontrols='true' width='" + width + "' height='" + (height + 45) + "' src='" + url + "'>" +
    " </EMBED>" +
    " </OBJECT>";
  }
  document.body.innerHTML += htmlStart + html + htmlEnd;
  var el = document.getElementById('previewContainer');
  var top = Math.floor(document.body.scrollTop + (document.body.clientHeight - el.offsetHeight) / 2);
  var left = Math.floor(document.body.scrollLeft + (document.body.clientWidth - el.offsetWidth) / 2);
  if(top < 0) top = 0;
  if(left < 0) left = 0;
  el.style.top = top + "px";
  el.style.left = left + "px";
  el.style.visibility = "visible";
}