// IE code from MSDN

var iePopup; // IE popup-window is global

// Detect browser
var browserinfos = navigator.userAgent;
var ie = (document.all && !browserinfos.match(/Opera/) ? getInternetExplorerVersion() : -1)
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all && !browserinfos.match(/Opera/); // also Firefox
var opera = browserinfos.match(/Opera/);

// Detect the IE rendering engine, from msdn
function getInternetExplorerVersion() {
  var rv = -1;
  // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(browserinfos) != null)
      rv = parseFloat(RegExp.$1);
  }
  return rv;
}

// Code file contents are passed to this function after it is downloaded.
function onDone(src){
  // Clear the popup-windows
  iePopup.document.close();
  iePopup.document.write();
  // Write the contents to the popup-window
  iePopup.document.close();
  iePopup.document.write(src);
  // Change the style
  var iePopupBody = iePopup.document.body;
  iePopupBody.style.backgroundColor = "rgb(255,255,255)";
  iePopupBody.style.border = "solid rgb(0,128,128) 1px";
}

function showPopup(x, y, menuWidth, menuHeight, popupFile){
  if (ns6 || opera) {
    // FOR OTHER BROWSERS
    // shows an <object> with the menu
    // create the popup with a <div> and an <object> with the menu
    var menuDiv = document.createElement('div');
    menuDiv.id = "menuDiv"
    menuDiv.style.backgroundColor = "transparent";
    menuDiv.style.border = 0;
    menuDiv.style.position = "absolute";
    menuDiv.style.left = parseInt(x) + "px";
    menuDiv.style.top = parseInt(y) + "px";
    menuDiv.style.width = parseInt(menuWidth) + "px";
    menuDiv.style.height = parseInt(menuHeight) + "px";
    menuDiv.style.visibility = "visible";
    // create a inline-element <object> containing the menu
    var menuObject = document.createElement('object');
    menuObject.id = "menuObject";
    menuObject.canHaveHTML = true;
    menuObject.style.border = "1px";
    menuObject.style.borderColor = "rgb(0,128,128)";
    menuObject.style.borderStyle = "solid";
    menuObject.style.width = "100%";
    menuObject.style.height = "100%";
    menuObject.data = popupFile;
    // put both elements in the DOM hierarchy and make them visible
    window.parent.scrollTo(0, 0);
    document.body.appendChild(menuDiv);
    menuDiv.appendChild(menuObject);

  } else if (ie >= 7) {
    // FOR INTERNET EXPLORER ONLY VERSION 6 OR LOWER
    // shows an <object> with the menu
    // create the popup with a <div> and an <object> with the menu
    var menuDiv = document.createElement('div');
    menuDiv.id = "menuDiv"
    menuDiv.style.backgroundColor = "transparent";
    menuDiv.style.border = 0;
    menuDiv.style.position = "absolute";
    menuDiv.style.left = parseInt(x) + "px";
    menuDiv.style.top = parseInt(y) + "px";
    menuDiv.style.height = menuHeight + "px";
    menuDiv.style.width = menuWidth + "px";
    menuDiv.style.visibility = "visible";
    // create a inline-element <object> containing the menu
    var menuObject = document.createElement('iframe');
    menuObject.id = "menuObject";
    menuObject.frameBorder = "no";
    menuObject.style.border = "1px";
    menuObject.style.borderColor = "rgb(0,128,128)";
    menuObject.style.borderStyle = "solid";
    menuObject.style.height = "100%";
    menuObject.style.width = "100%";
    menuObject.src = popupFile;
    // put both elements in the DOM hierarchy and make them visible
    window.parent.scrollTo(0, 0);
    document.body.appendChild(menuDiv);
    menuDiv.appendChild(menuObject);

  } else if (ie < 7) {
    // FOR INTERNET EXPLORER ONLY VERSION 7 OR HIGHER
    // IE shows the propriatory popup-window with the menu.
    // Create a IE popup-window when not already present
    if (!iePopup) {
      iePopup = window.createPopup();
    }
    // startDownload is a member of the download default behavior.
    // The callback function pointer (second parameter) specifies a
    // function. When a file downloads successfully, the file contents
    // are passed as the parameter to onDone().
    dwn.startDownload(popupFile, onDone);
    iePopup.show(parseInt(x), parseInt(y), menuWidth, menuHeight, document.body);
  }
}