﻿/* DON'T fsck with the order of the flwg vars
   opera/konqueror/safari are schizo: they think they are xPloder, Konqueror and/or Mozilla
*/
   var isOpera  = window.opera ? true : false;
   var isSafari = (!isOpera && navigator.userAgent.toLowerCase().indexOf('applewebkit') != -1 && navigator.userAgent.toLowerCase().indexOf('khtml') != -1) ? true : false;
   var isKonq   = (!isOpera && !isSafari && (navigator.vendor == 'KDE' || (document.childNodes && !document.all && !navigator.taintEnabled))) ? true : false;
   var isDOM    = (!isOpera && document.getElementById && document.createElement) ? true : false;
   var isIE     = (!isOpera && !isKonq && isDOM && document.all) ? true : false;
   var isGecko  = (!isOpera && navigator.product && (navigator.product.toLowerCase() == 'gecko') && (navigator.userAgent.toLowerCase().indexOf('khtml') == -1)) ? true : false;

function sendToDevNull(e)
{
   if (top.isIE)
   {
      if (event.srcElement)   // we're being passed an xPloder type event already
      {
         event.returnValue  = false;
         event.cancelBubble = true;
      }
      else                    // convert e to an xPloder type event
      {
         getXploderEvent().returnValue  = false;
         getXploderEvent().cancelBubble = true;
      }

      return;
   }

   e.preventDefault(); 
   e.stopPropagation();
}

   // tnx to Simon Willison <http://www.sitepoint.com/blog-post-view.php?id=171578>
function addLoadEvent(fn)
{
   var oldonload = window.onload;
   if (typeof window.onload != 'function')
      window.onload = fn;
   else
      window.onload = function() { oldonload(); fn(); };
}

function addResizeEvent(fn)
{
   var oldonresize = window.onresize;
   if (typeof window.onresize != 'function')
      window.onresize = fn;
   else
      window.onresize = function() { oldonresize(); fn(); };
}

function getKey(key)
{
   switch(key)
   {
      case  27: return 'escape';

      case 112: return 'f1';
      case 113: return 'f2';
      case 114: return 'f3';
      case 115: return 'f4';
      case 116: return 'f5';
      case 117: return 'f6';
      case 118: return 'f7';
      case 119: return 'f8';
      case 120: return 'f9';
      case 121: return 'f10';
      case 122: return 'f11';
      case 123: return 'f12';

      case  19: return 'pause';
      case  44: return 'printscreen';
      case 145: return 'scrolllock';

      case   8: return 'backspace';
      case  45: return 'insert';
      case  46: return 'delete';
      case  36: return 'home';
      case  35: return 'end';
      case  33: return 'pageup';
      case  34: return 'pagedown';

      case  37: return 'left';
      case  38: return 'up';
      case  39: return 'right';
      case  40: return 'down';

      case  13: return 'enter';
      case  20: return 'capslock';
      case   9: return 'tab';
      case  32: return 'space';

      default:  return null;
   }
}

   // tnx to David Andersson <http://liorean.web-graphics.com/>
function getIFrameDocRef(ref)
{
   return (typeof ref.contentDocument != 'undefined') ? ref.contentDocument :
          (typeof ref.contentWindow   != 'undefined') ? ref.contentWindow.document :
          (typeof ref.document        != 'undefined') ? ref.document : null;
}

function getXploderEvent()
{
/* xPloder's event belongs to the frame rather than the top window
 * so first I have to figger out the calling frame [sigh]
 */
   for (var i = 0; i < top.frames.length; i++)
      if (top.frames[i].event)
         return top.frames[i].event;

   return window.event;
}

   // flwg functions tnx to PPK <http://www.quirksmode.org/js/findpos.html>
function findPosX(obj)
{
   var curleft = 0;
   if (obj.offsetParent)
   {
      while (obj.offsetParent)
      {
         curleft += obj.offsetLeft;
         obj = obj.offsetParent;
      }
   }
   else if (obj.x)
      curleft += obj.x;

   return curleft;
}

function findPosY(obj)
{
   var curtop = 0;
   if (obj.offsetParent)
   {
      while (obj.offsetParent)
      {
          curtop += obj.offsetTop;
          obj = obj.offsetParent;
      }
   }
   else if (obj.y)
      curtop += obj.y;

   return curtop;
}

function getScreenX()
{
   return window.left ? window.left : screenX;
}

function getScreenY()
{
   return window.top ? window.top : screenY;
}

function getOuterWidth(windowObj)
{
   if (!windowObj)
      windowObj = self;
// all except Explorer
   if (windowObj.outerWidth)
      return windowObj.outerWidth;
// Explorer 6 Strict Mode
   else if (windowObj.document.documentElement && windowObj.document.documentElement.offsetWidth)
      return windowObj.document.documentElement.offsetWidth;
// other Explorers
   else if (windowObj.document.body)
      return windowObj.document.body.offsetWidth;
}

function getOuterHeight(windowObj)
{
   if (!windowObj)
      windowObj = self;
// all except Explorer
   if (windowObj.outerHeight)
      return windowObj.outerHeight;
// Explorer 6 Strict Mode
   else if (windowObj.document.documentElement && windowObj.document.documentElement.offsetHeight)
      return windowObj.document.documentElement.offsetHeight;
// other Explorers
   else if (windowObj.document.body)
      return windowObj.document.body.offsetHeight;
}

   // flwg 6 functions tnx to PPK <http://www.quirksmode.org/viewport/compatibility.html>
function getInnerWidth(windowObj)
{
   if (!windowObj)
      windowObj = self;
// all except Explorer
   if (windowObj.innerWidth)
      return windowObj.innerWidth;
// Explorer 6 Strict Mode
   else if (windowObj.document.documentElement && windowObj.document.documentElement.clientWidth)
      return windowObj.document.documentElement.clientWidth;
// other Explorers
   else if (windowObj.document.body)
      return windowObj.document.body.clientWidth;
}

function getInnerHeight(windowObj)
{
   if (!windowObj)
      windowObj = self;
// all except Explorer
   if (windowObj.innerHeight)
      return windowObj.innerHeight;
// Explorer 6 Strict Mode
   else if (windowObj.document.documentElement && windowObj.document.documentElement.clientHeight)
      return windowObj.document.documentElement.clientHeight;
// other Explorers
   else if (windowObj.document.body)
      return windowObj.document.body.clientHeight;
}

function getPageXOffset(windowObj)
{
   if (!windowObj)
      windowObj = self;
// all except Explorer
   if (windowObj.pageXOffset)
      return windowObj.pageXOffset;
// Explorer 6 Strict
   else if (windowObj.document.documentElement && windowObj.document.documentElement.scrollLeft)
      return windowObj.document.documentElement.scrollLeft;
// all other Explorers
   else if (windowObj.document.body)
      return windowObj.document.body.scrollLeft;
}

function getPageYOffset(windowObj)
{
   if (!windowObj)
      windowObj = self;
// all except Explorer
   if (windowObj.pageYOffset)
      return windowObj.pageYOffset;
// Explorer 6 Strict
   else if (windowObj.document.documentElement && windowObj.document.documentElement.scrollTop)
      return windowObj.document.documentElement.scrollTop;
// all other Explorers
   else if (windowObj.document.body)
      return windowObj.document.body.scrollTop;
}

function getPageWidth(windowObj)
{
// all but Explorer Mac
   if (document.body.offsetWidth < document.body.scrollWidth)
      return document.body.scrollWidth;
// Explorer Mac; would also work in Explorer 6 Strict, Mozilla and Safari
   else
      return document.body.offsetWidth;
}

function getPageHeight(windowObj)
{
// all but Explorer Mac
   if (document.body.offsetHeight < document.body.scrollHeight)
      return document.body.scrollHeight;
// Explorer Mac; would also work in Explorer 6 Strict, Mozilla and Safari
   else
      return document.body.offsetHeight;
}

   // flwg 2 function tnx to PPK <http://www.quirksmode.org/js/events_properties.html>
function getMouseX(e)
{
   if (!e)
      getXPloderEvent();
   if (e.pageX)
      mouseX = e.pageX;
   else if (e.clientX)
      mouseX = e.clientX + getPageXOffset();
   return mouseX;
}

function getMouseY(e)
{
   if (!e)
      getXPloderEvent();
   if (e.pageY)
      mouseY = e.pageY;
   else if (e.clientY)
      mouseY = e.clientY + getPageYOffset();
   return mouseY;
}

   // flwg 2 functions prevent popups and tooltips from disappearing under canvas edges
function fitObjInCanvasWidth(Xpos, objWidth)
{
   var totalWidth = Xpos + objWidth;
   var pageWidth  = getPageXOffset() + getInnerWidth() - 25;   // correct for vrt scrollbar

   var newXpos = Xpos;
   if (pageWidth < totalWidth)
      newXpos = pageWidth - objWidth;                          // correct for right underlap
   if (newXpos < getPageXOffset())
      newXpos = getPageXOffset() + 5;                          // correct for left underlap
   return newXpos;
}

function fitObjInCanvasHeight(Ypos, objHeight)
{
   var totalHeight = Ypos + objHeight;
   var pageHeight  = getPageYOffset() + getInnerHeight() - 25; // correct for hor scrollbar

   var newYpos = Ypos;
   if (pageHeight < totalHeight)
      newYpos = pageHeight - objHeight;                        // correct for bottom underlap
   if (newYpos < getPageYOffset())
      newYpos = getPageYOffset() + 5;                          // correct for top underlap
   return newYpos;
}

function resizeFonts()
{
   document.body.style.fontSize = (getInnerWidth(top) <= 800) ? '70%' : '100%';
}

function hideMovieWindowHack()
{
/* This is easily the weirdest bit of code I have ever written
 *
 * We test for the absence of 'html' since this routine throws an error
 * in xPloder when loading the index aka 'www.meldpuntdoven.nl'
 *
 * xPloder also needs the check for 'movieWindow' or it throws
 * an 'unknown error' #84
 *
 * Ff doesn't need the 'moviewindow' check but, since iFrames fsck up
 * its history [see http://bugzilla.mozilla.org/show_bug.cgi?id=237717],
 * using the back button only empties the iFrame but doesn't hide it
 */
   if (document.location.href.indexOf('html') == -1)
      return;

   if (isIE && document.getElementById('movieWindow'))
      top.hideMovieWindow();
   else
      top.hideMovieWindow();
};

// we do this here since this file gets loaded into every document in the site
   addLoadEvent(resizeFonts);
   addResizeEvent(resizeFonts);
   window.onunload = hideMovieWindowHack;

