// 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;
}

// Microsoft event-handler
if (parent.location != this.location) {
  // Stop the behaviour of the x-button.
  document.getElementById('closeButton').href = "#";
  // W3C event-handler
  if (ns6 || opera) {
    document.body.addEventListener('click', clickW3C, true);
  // Internet Explorer event-handler
  } else if (ie >= 7) {
    document.body.attachEvent('onclick', clickIEnew);
  } else if (ie < 7) {
    document.body.attachEvent('onclick', clickIEold);
  }

}

function clickW3C(e){
  // e refers to the event
  e.stopPropagation();
  window.stop();
  var thisTarget = e.target;
  // defeat Safari bug
  if (thisTarget.nodeType == 3) thisTarget = thisTarget.parentNode;
  // The parent window is accessed from the popup by the "parent" property.
  // User will click on a <span>.
  if (thisTarget.tagName == "SPAN"){
    thisTarget = thisTarget.parentNode;
    while (thisTarget.href == undefined) {
      thisTarget = thisTarget.parentNode;
    }
    // Go to the selected location
    parent.location = thisTarget.href;
    document.removeChild(document.getElementById('menuPopup'));
  }
  // Remove the popup
  var menuDiv = window.parent.document.getElementById('menuDiv');
  menuDiv.parentNode.removeChild(menuDiv);
}

function clickIEnew() {
  // FOR INTERNET EXPLORER ONLY VERSION 7 OR HIGHER
  // e refers to the event
  var e = window.event;
  e.cancelBubble = true;
  var thisTarget = e.srcElement;
  // The parent window is accessed from the popup by the "parent" property.
  // User will click on a <span>.
  if (thisTarget.tagName == "SPAN"){
    do {
      thisTarget = thisTarget.parentNode;
    } while (thisTarget.href == undefined)
    // Go to the selected location
    parent.location = thisTarget.href;
  } else {
    // Remove the popup
    var menuDiv = window.parent.document.getElementById('menuDiv');
    menuDiv.parentNode.removeChild(menuDiv);
  }
}

function clickIEold() {
  // FOR INTERNET EXPLORER ONLY VERSION 6 OR LOWER
  // IE shows the propriatory popup-window
  // e refers to the event
  var e = window.event;
  e.cancelBubble = true;
  var thisTarget = e.srcElement;
  // The parent window is accessed from the popup by the "parent" property.
  // User will click on a <span>.
  if (thisTarget.tagName == "SPAN"){
    do {
      thisTarget = thisTarget.parentNode;
    } while (thisTarget.href == undefined)
    // Go to the selected location
    parent.location = thisTarget.href;
  } else {
    parent.setTimeout('iePopup.hide();', 100);
  }
}

