﻿/* Popup js-functies 
   openComputerwoord    aparte functie die alleen gebruikt wordt door anchors vanuit een tooltip
   popUpWin             functie op basis van 'rel' attribuut in anchors, waarde van rel="openWindow"
*/


/* POPUP 1 */
function openWindow(pageURL, w, h) {
  var left = (screen.width / 2) - (w / 2);
  var top = (screen.height / 2) - (h / 2);
  var targetWin = window.open(pageURL, 'Computerwoordenboek', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
}


/* POPUP 2*/
var newWindow = null;

//eventhandler
function addEvent(elm, evType, fn, useCapture) {
  if (elm.addEventListener) {
    elm.addEventListener(evType, fn, useCapture);
    return true;
  }
  else if (elm.attachEvent) {
    var r = elm.attachEvent('on' + evType, fn);
    return r;
  }
  else {
    elm['on' + evType] = fn;
  } 
}

//als er al een popup venster open staat, deze sluiten
function closeWin() {
  if (newWindow != null) {
    if (!newWindow.closed)
      newWindow.close();
  }
}

//popup openen
function popUpWin(url, type, strWidth, strHeight) {
  //midden van het scherm bepalen
  var strLeft = (screen.width / 2) - (strWidth / 2);
  var strTop = (screen.height / 2) - (strHeight / 2); 
  
  closeWin();
  type = type.toLowerCase();
  
  if (type == "fullscreen") {
    strWidth = screen.availWidth;
    strHeight = screen.availHeight;
  }
  
  //popup type bepalen > VS: tot nu toe wordt alleen 'standard' gebruikt
  var tools = "";
  if (type == "standard") tools = "resizable,toolbar=no,location=no,scrollbars=yes,menubar=no,width=" + strWidth + ",height=" + strHeight + ",top=" + strTop + ",left=" + strLeft + "";
  if (type == "console" || type == "fullscreen") tools = "resizable,toolbar=no,location=no,scrollbars=no,width=" + strWidth + ",height=" + strHeight + ",left=0,top=0";
  
  //daadwerkelijke popup openen met gedefinieerde instellingen
  newWindow = window.open(url, 'newWin', tools);
  newWindow.focus();
}

function doPopUp(e) {
  //default popup instellingen
  var t = "standard";
  var w = "720";
  var h = "500";
  
  //parameters uit rel attribute zoeken
  attribs = this.rel.split(" ");
  if (attribs[1] != null) { t = attribs[1]; }
  if (attribs[2] != null) { w = attribs[2]; }
  if (attribs[3] != null) { h = attribs[3]; }
  
  //popup script aanroepen
  popUpWin(this.href, t, w, h);
  
  //standaard link event uitschakelen
  if (window.event) {
    window.event.returnValue = false;
    window.event.cancelBubble = true;
  }
  else if (e) {
    e.stopPropagation();
    e.preventDefault();
  }
}

//loop door links met rel="openWindow"
function findPopUps() {
  var popups = document.getElementsByTagName("a");
  for (i = 0; i < popups.length; i++) {
    if (popups[i].rel.indexOf("openWindow") != -1) {
      //popup functionaliteit toevoegen
      popups[i].onclick = doPopUp;
      //popup indicator toevoegen aan link > VS: wordt nu niet gebruikt!
      //if (popups[i].rel.indexOf("noicon") == -1) {
      //  popups[i].style.backgroundImage = "url(pop-up.gif)";
      //  popups[i].style.backgroundPosition = "0 center";
      //  popups[i].style.backgroundRepeat = "no-repeat";
      //  popups[i].style.paddingLeft = "15px";
      //}
      // title attribuut aanvullen met popup tekst
      popups[i].title = popups[i].title + " (link opent in een nieuw venster)";
    }
  }
}

addEvent(window, 'load', findPopUps, false);
