var popups = new Array();

function pageLoadPopup() {
  xAddEventListener(window, 'keydown', escapeFenster, false);
}

function escapeFenster(e) {
  var ev = new xEvent(e);
  if (ev.keyCode == 27) {
    var f = xFenster.focused;
    if (f && !f.minimized) f.hide();
  }
}

function checkPopup(popName) {
  return (popups[popName] && !popups[popName].closed);
}

function getPopupContentElement(popName, divId) {
  if (divId != null) return popups[popName].document.getElementById(divId);
  else return popups[popName].document.body;
}
function setPopupHtml(popName, divId, html) {
  var el = getPopupContentElement(popName, divId);
  xInnerHtml(el, html);
  popups[popName].focus();
}
function addPopupHtml(popName, divId, html, div) {
  var el = getPopupContentElement(popName, divId);
  var oHtml = xInnerHtml(el);
  if (oHtml == '') xInnerHtml(el, html);
  else xInnerHtml(el, html + div + oHtml);
  popups[popName].focus();
}

function checkFenster(xfName) {
  return eval('xFenster.instances.' + xfName + ' && !xFenster.instances.' + xfName + '.hidden');
}

function checkFenster2(xfName) {
  return eval('xFenster.instances.' + xfName);
}

function getFensterContentElement(xfName, divId) {
  if (divId != null) return eval('xGetElementById("' + divId + '", xFenster.instances.' + xfName + '.client)');
  else return eval('xFenster.instances.' + xfName + '.client');
}

//use single quotes in html
function setFensterHtml(xfName, divId, html) {
  var el = getFensterContentElement(xfName, divId);
  xInnerHtml(el, html);
  eval('xFenster.instances.' + xfName + '.show()'); //minimized?
}

function addFensterHtml(xfName, divId, html, div) {
  var el = getFensterContentElement(xfName, divId);
  var oHtml = xInnerHtml(el);
  if (oHtml == '') xInnerHtml(el, html);
  else xInnerHtml(el, html + div + oHtml);
  eval('xFenster.instances.' + xfName + '.show()'); //minimized?
}

function makeFenster(xfName, popName, divId, html, url, xfTitle, xfLeft, xfTop, xfWidth, xfHeight) {
  new xFenster(xfName, xfTitle, url, xfLeft, xfTop, xfWidth, xfHeight, xfMinW, null, 0, 1, 0, true, true, true, false, true, true, true,
    null, null, null, null, null, null, null, null, 'xfCon', 'xfClient', 'xfTBar', 'xfTBarF', 'xfSBar', 'xfSBarF', 'xfRIco', 'xfNIco',
    'xfMIco', 'xfOIco', 'xfCIco', 'Resize', 'Minimize', 'Maximize', 'Restore', 'Close');
  var xf = eval('xFenster.instances.' + xfName);
  if (html != null) {
    if (divId != null) xf.client.innerHTML = '<div id="' + divId + '" class="xfContent">' + html + '</div>';
    else xf.client.innerHTML = '<div class="xfContent">' + html + '</div>';
  }
  if (popName != null)
    xf.status('<a href="javascript:xfPopout(\'/lib/popout.html?xf=' + xfName + '\', \'' + popName + '\', ' + xfWidth + ', ' +
      xfHeight + ')">pop out</a>');
}

function xfPopout(url, winName, wWidth, wHeight) {
  var win = window.open(url, winName, "width="+wWidth+",height="+wHeight+",resizable=yes,scrollbars=yes,toolbar=no,location=no,menubar=no,status=no");
  win.focus();
  if (winName != '') popups[winName] = win;
}

function showPopup(xfName, popName) {
  var rv = false;
  if (checkPopup(popName)) {
    popups[popName].focus();
    rv = true;
  } else if (eval('xFenster.instances.' + xfName)) {
    var xfStr = 'xFenster.instances.' + xfName;
    eval('if (' + xfStr + '.minimized) ' + xfStr + '.restore()');
    eval(xfStr + '.show()');
    rv = true;
  }
  return rv;
}
