﻿/*    Copyrights 2000-2008, Allways Information Systems BV    *
 **************************************************************/
function fPreloadImages() {
  var d = document,
      i, j, a;
  if (d.images) {
    if (!d.MM_p) {
      d.MM_p = [];
    }
    j = d.MM_p.length;
    a = fPreloadImages.arguments;
    for(i = 0; i < a.length; i++) {
      if (a[i].indexOf("#") != 0) {
        d.MM_p[j] = new Image();
        d.MM_p[j++].src = a[i];
      }
    }
  }
}

var qv = {};

qv.Event = {
  add: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.addEventListener) {
      obj.addEventListener(etype, fp, cap);
    }
    else {
      if (obj.attachEvent) {
        obj.attachEvent("on" + etype, fp);
      }
    }
  }, 

  remove: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.removeEventListener) {
      obj.removeEventListener(etype, fp, cap);
    }
    else { 
      if (obj.detachEvent) {
        obj.detachEvent("on" + etype, fp);
      }
    }
  }, 
   
  augmentEvent: function(e) { 
    e = e ? e: window.event;
    if (!e.target) {
      e.target = e.srcElement;
    }
    if (!e.preventDefault) {
      e.preventDefault = function () { 
        e.returnValue = false;
        return false; 
      };
    }
    if (!e.stopPropagation) {
      e.stopPropagation = function () { 
        e.cancelBubble = true; 
      };
    }
    return e;
  },
    
  getTarget: function(e) {
    e = qv.Event.augmentEvent(e); 
    var tar = e.target; 
    if (tar.nodeType !== 1) {
      tar = tar.parentNode; // safari...
    }
    return tar;
  }
      
};

qv.Rotator = function (id, speed, path, pTransform, pauseOnHover) {
  var elImg = document.getElementById(id),
      index;
  
  if (!elImg) {
    return;
  }
  
  //set properties
  this.id = id; 
  this.speed = speed || 5000;
  this.path = path || "";
  this.counter = 0; 
  this.timer = 0; 
  this.imgs = []; 
  this.addLinkBehavior(elImg, pauseOnHover);
  this.pTransform = pTransform && typeof elImg.filters != "undefined";
  index = qv.Rotator.collection.length; 
  qv.Rotator.collection[index] = this;
  this.animString = "qv.Rotator.collection[" + index + "]";
};

qv.Rotator.collection = [];
qv.Rotator.resumeRotationAfter = 1000; // onmouseout resume rotation after ...ms

// mouse events pause/resume
qv.Rotator.prototype.addLinkBehavior = function(elImg, pauseOnHover) { 
  if ( elImg.parentNode && elImg.parentNode.tagName.toUpperCase() === "A" ) {
    var link = elImg.parentNode;
    this.link = link;
    if (pauseOnHover) {
      qv.Event.add(link, "mouseover", qv.Rotator.pause);
      qv.Event.add(link, "mouseout", qv.Rotator.resume);
    }
  }
};

// so instance can be retrieved by id (as well as by looping through col)
qv.Rotator.getInstanceById = function(id) {
  var len = qv.Rotator.collection.length, obj, i;
  for (i = 0; i < len; i += 1) {
    obj = qv.Rotator.collection[i];
    if (obj.id && obj.id === id ) {
      return obj;
    }
  }
  return null;
};

qv.Rotator.prototype.addImages = function() { // preloads images
  var img, i;
  for (i = 0; arguments[i]; i += 1) {
    img = new Image();
    img.src = this.path + arguments[i];
    this.imgs[this.imgs.length] = img;
  }
};

qv.Rotator.prototype.rotate = function() {
  clearTimeout(this.timer); 
  this.timer = null;
  var elImg = document.getElementById(this.id);

  if ( this.counter < this.imgs.length - 1 ) {
    this.counter += 1;
  }
  else {
    this.counter = 0;
  }
  
  if ( this.pTransform ) {
    this.doImageTrans(elImg);
  } 
  else {
    elImg.src = this.imgs[this.counter].src;
  }
  
  this.swapAlt(elImg);
  this.setLink();
  this.timer = setTimeout( this.animString + ".rotate()", this.speed);   
};

qv.Rotator.prototype.doImageTrans = function(elImg) {
  elImg.style.filter = "blendTrans(duration=1)";
  if (elImg.filters.blendTrans) {
    elImg.filters.blendTrans.Apply();
  }
  elImg.src = this.imgs[this.counter].src;
  elImg.filters.blendTrans.Play(); 
};

qv.Rotator.prototype.swapAlt = function(elImg) {
  if ( !elImg.setAttribute ) {
    return;
  }
  if ( this.alt && this.alt[this.counter] ) {
    elImg.setAttribute("alt", this.alt[this.counter]);
  }
  if ( this.title && this.title[this.counter] ) {
    elImg.setAttribute("title", this.title[this.counter]);
  }
};

qv.Rotator.prototype.handleLinkClick = function () {
  return false;
};

qv.Rotator.prototype.setLink = function() {
  var sHref = "",
      oSelf = this;

  if ( this.actions && this.link && (this.actions[this.counter] !== "") && typeof this.actions[this.counter] === "string" ) {
    sHref = this.actions[this.counter]; //make local obj to use in click handler (closure)
    this.link.href = sHref;
    this.link.style.cursor = "pointer";
    qv.Event.remove(oSelf.link, "click", oSelf.handleLinkClick);
  }
  else {
    this.link.href = "#";
    this.link.style.cursor = "default";
    qv.Event.add(oSelf.link, "click", oSelf.handleLinkClick);
  }
};

// Start rotation for all instances 
qv.Rotator.start = function() {
  var len = qv.Rotator.collection.length, 
      obj, i;
  
  for (i = 0; i < len; i += 1) {
    obj = qv.Rotator.collection[i];
    if ( obj && obj.id ) {
      obj.timer = setTimeout( obj.animString + ".rotate()", obj.speed);
    }
  }
};

// Stop rotation for all instances 
qv.Rotator.stop = function() {
  var len = qv.Rotator.collection.length,
      obj, i;
      
  for (i = 0; i < len; i += 1) {
    obj = qv.Rotator.collection[i];
    if ( obj ) { 
      clearTimeout(obj.timer); 
      obj.timer = null; 
    }
  }
};

// stopping/starting (onmouseover/out)
qv.Rotator.pause = function(e) {	
  e = qv.Event.augmentEvent(e);
  var id = e.target.id,
      obj = qv.Rotator.getInstanceById(id);
  
  if ( obj ) { 
    clearTimeout( obj.timer ); 
    obj.timer = null;
  }
};

qv.Rotator.resume = function(e) {
  e = qv.Event.augmentEvent(e);
  var id = e.target.id,
      obj = qv.Rotator.getInstanceById(id);
  
  if ( obj && obj.id ) {
    obj.timer = setTimeout( obj.animString + ".rotate()", qv.Rotator.resumeRotationAfter );
  }
};

// calls constructor, addImages, adds actions, etc.
qv.Rotator.setup = function () {
  if (!document.getElementById) {
    return;
  }
  var i, j, rObj, r, imgAr, len;
  
  for (i=0; arguments[i]; i++) {
    rObj = arguments[i];
    r = new qv.Rotator(rObj.id, rObj.speed, rObj.path, rObj.pTransform, rObj.pauseOnHover);
    try {
      imgAr = rObj.images; 
      len = imgAr.length;
      for (j=0; j<len; j++) { 
        r.addImages( imgAr[j] ); 
      }
      if ( rObj.num ) {
        r.counter = rObj.num; // for seq after random selection
      }
      if ( rObj.actions && rObj.actions.length === len ) {
        r.addProp("actions", rObj.actions);
      }
      if ( rObj.alt && rObj.alt.length === len ) {
        r.addProp("alt", rObj.alt);
      }
      if ( rObj.title && rObj.title.length === len ) {
        r.addProp("title", rObj.title);
      }
    } 
    catch (e) { 
      //alert(e.message); 
    }
  }
  qv.Rotator.start();
};

// for adding actions, alt, title
qv.Rotator.prototype.addProp = function(prop, arr) {
  if ( !this[prop] ) {
    this[prop] = [];
  }
  var len = arr.length; 
  for (var i=0; i < len; i++) {
    this[prop][ this[prop].length ] = arr[i]; 
  }
};



function $(pElementID) {
  return document.getElementById(pElementID);
}

function fInit() {
  sfHoverBehavior();
  fPreloadImages('../App_Themes/' + vPageTheme + '/gfx/bg_btn_hvr.png');
}

function fSetFocus(pObj) {
  pObj.focus();
}

function fCloseAndReloadOpener() {
  if (window.opener) {
    window.opener.location.reload();
  }
  window.close();
}

/** suckerfish **/
sfHover = function() {
  var sfEls = $('oNav').getElementsByTagName('LI');
  for (var i = 0; i < sfEls.length; i++) {
    sfEls[i].onmouseover = function() {
      this.className += ' sfhover';
    };
    sfEls[i].onmouseout = function() {
      this.className=this.className.replace(new RegExp(' sfhover\\b'), '');
    };
  }
};

function sfHoverBehavior() {
  if (($('oNav')) && (window.attachEvent)) {
    window.attachEvent('onload', sfHover);
  }
}
/****/

function fScrollElementIntoView(pElem, pParentId, pH) {
  var _oParent = $(pParentId);
  var _hTotal = (pH !== null) ? pH : 0;
  
  if ((pElem !== null) && (_oParent !== null)) {
    /* _oParent must have position to find it as offsetParent */
    if (_oParent.style.position === '') {
      _oParent.style.position = 'relative';
    }
    
    if (pElem == _oParent) {
      _oParent.scrollTop = _hTotal - Math.floor((_oParent.clientHeight/2));
    }
    else {
      _hTotal = _hTotal + pElem.offsetTop;
      if ( pElem.offsetParent ) {
        fScrollElementIntoView(pElem.offsetParent, pParentId, _hTotal);
      }
    }
  }
}

function fAdjustPageWidth() {
  /* let op, width is in stylesheet default gedefinieerd in relatieve % */
  if (($('o3ColumnTable')) && ($('oPageBox'))) {
    var hLayoutTable = $('o3ColumnTable');
    var hPage = $('oPageBox');
    hPage.style.width = hLayoutTable.offsetWidth + 'px';
  }
}

function fGetWinHeight() {
  var pH=0;
  if (typeof(window.innerHeight) == 'number') {
    /* non IE */
    pH = window.innerHeight;
  }
  else {
    if ((document.documentElement) && (document.documentElement.clientHeight)) {
      /* IE 6+ in 'standards compliant mode' */
      pH = document.documentElement.clientHeight;
    }
    else {
      if ((document.body) && (document.body.clientHeight)) {
        /* IE 4 compatible */
        pH = document.body.clientHeight;
      }
    }
  }
  return pH;
}

function fGetWinWidth() {
  var pW=0;
  if (typeof(window.innerWidth) == 'number') {
    /* non IE */
    pW = window.innerWidth;
  }
  else {
    if ((document.documentElement) && (document.documentElement.clientWidth)) {
      /* IE 6+ in 'standards compliant mode' */
      pW = document.documentElement.clientWidth;
    }
    else {
      if ((document.body) && (document.body.clientWidth)) {
        /* IE 4 compatible */
        pW = document.body.clientWidth;
      }
    }
  }
  return pW;
}

function fSetPageHeight() {
  if ((document.getElementById) && ($('oPageBox'))) {
    var oPage = $('oPageBox');
    var hWinHeight = fGetWinHeight();
    var hContentHeight = $('oMain').offsetHeight;
    if (hWinHeight > 0) {
      if (hWinHeight > hContentHeight) {
        oPage.style.height = hWinHeight + 'px';
      }
    }
  }
}

function fGetScreenSize() {
  if (window.screen) {
    var w = screen.width;
    var h = screen.height;
    return [w,h];
  }
  else {
    return [0,0];
  }
}

var vDefaultPopupFeatures = 'toolbar=0,location=0,directories=0,statusbar=0,status=0,menubar=0,width=560,height=400,resizable=0';
var vDefaultNewWindowFeatures = 'resizable=yes,toolbar=yes,location=yes,directories=no,status=yes,menubar=yes';

function fRawPopup(pUrl, pName, pFeatures) {
  if (fIsUndefined(pFeatures)) {
    pFeatures = vDefaultPopupFeatures;
  }
  if (fIsUndefined(pName)) {
    pName = 'ipfPopup';
  }
  
  var oPopupWindow = window.open(pUrl, pName, pFeatures);
  oPopupWindow.focus();
  return oPopupWindow;
}

function fOpenPopup(pUrl, pName, pWidth, pHeight) {
  var hLeft = 200;
  var hTop = 200;
  var aSrcSize = fGetScreenSize();
  if (aSrcSize[0] !== 0){
    hLeft = Math.round((aSrcSize[0] - pWidth)/2);
    hTop  = Math.round((aSrcSize[1] - pHeight)/2);
  }

  return fRawPopup(pUrl, pName, 'width=' + pWidth + ',height=' +pHeight+ ',top=' + hTop + ',left=' +hLeft+ ',toolbar=no,location=no,directories=no,status=no,menubar=no');
}

function fOpenWindow(pUrl, pFullScreen){
  var vSize = '';
  var hTaskBarCorrection = 30; /* height for windows taskbar */
  if (pFullScreen) {
    var aScrSize = fGetScreenSize();
    if ((aScrSize[0] !== 0) && (aScrSize[1]-hTaskBarCorrection > 0)) {
      vSize = 'width='+aScrSize[0]+',height='+(aScrSize[1]-hTaskBarCorrection)+',top=0,left=0,';
    }
  }
  window.open(pUrl, '_blank', vSize + vDefaultNewWindowFeatures);
}

function fIsUndefined(p) { 
  return typeof p == 'undefined';
}

function fShowObject(pObj) {
  pObj.style.display = 'block';
  if (pObj.className.indexOf('cHidden') >= 0) {
    pObj.className = pObj.className.replace(new RegExp('cHidden\\b'), '');
  }
}

function fHideObject(pObj) {
  pObj.style.display = 'none';
}

function fToggleVisibility(pId, pSender) {
  if ($(pId)) {
    var oTarget = $(pId);
    var hShow;
    
    if ((oTarget.style.display == 'none') || (oTarget.className.indexOf('cHidden') >= 0)) {
      fShowObject(oTarget);
      hShow = true;
    }
    else {
      fHideObject(oTarget);
      hShow = false;
    }
    
    /* regular expressions */
    var hRE_up    = /_up/g;
    var hRE_down  = /_down/g;
    var hRE_right = /(»|&raquo;)/g;
    var hRE_left  = /(«|&laquo;)/g;
    
    var hSrc;
    
    if ((pSender !== null) && (pSender.childNodes[0])) {
      /* case textnode (3) */
      if (pSender.childNodes[0].nodeType == 3) {
        /* in case pSender is hyperlink with >> or << we toggle this symbol */
        if (hShow) {
          pSender.childNodes[0].nodeValue = pSender.childNodes[0].nodeValue.replace(hRE_right,'«');
        }
        else {
          pSender.childNodes[0].nodeValue = pSender.childNodes[0].nodeValue.replace(hRE_left,'»');
        }
      }
      else if ((pSender.childNodes[0].nodeType == 1) && (pSender.childNodes[0].tagName == 'IMG')) {
        /* case xmlnode (1) and <img> */
        hSrc = pSender.childNodes[0].src;
        if (hShow) {
          hSrc = hSrc.replace(hRE_down,'_up');
        }
        else {
          hSrc = hSrc.replace(hRE_up,'_down');
        }
        pSender.childNodes[0].src = hSrc;
      }
    }
    else if ((pSender !== null) && (pSender.tagName.toUpperCase() === 'INPUT') && (pSender.type.toUpperCase === 'IMAGE')) {
      /* in case pSender is <input type="image"> toggle src */
      hSrc = pSender.src;
      if (hShow) {
        hSrc = hSrc.replace(hRE_down,'_up');
      }
      else {
        hSrc = hSrc.replace(hRE_up,'_down');
      }
      pSender.src = hSrc;
    }
  }
}

window.onload = function() {
  fInit();
};
