/**
 *  standard j-insites javascript library
 */


/*
 *
 */
// walk all elements of DOM tree at and below e, 
// run function fn for each element with parameters param
// not suitable for fn's which change a node  
function walkTree(e, fn, param) {

  fn(e, param);
  
  var eChild = e.firstChild
  while (eChild) {
    walkTree(eChild, fn, param);    
    eChild = eChild.nextSibling;
  }
}

/*
 * Crossbrowser addEvent & RemoveEvent functions
 * Courtesy of http://www.soziologie.uni-halle.de/unger/scripts/workshop_internet/fr_js_322.html
 */
 function addEvent(obj, eventType, fn, isCapture) {
    // W3C DOM
    if (obj.addEventListener) {
       obj.addEventListener(eventType, function(evt) { fn(normalizeEvent(evt)) }, isCapture);
       return true;
    }
    // Internet Explorer
    else if (obj.attachEvent) {
       return obj.attachEvent("on"+eventType, function() { fn(normalizeEvent(window.event)) });
    }
    else return false;
 }

 function removeEvent(obj, eventType, afunction, isCapture) {
    if (obj.removeEventListener) {
       obj.removeEventListener(eventType, afunction, isCapture);
       return true;
    }
    else if (obj.detachEvent) {
       return obj.detachEvent("on"+eventType, afunction);
    }
    else return false;
 }

 function normalizeEvent(evt) {
   
    /* needs to be outside evt.srcElement (IE) test to apply to onLoad event */
    if (!evt.preventDefault) evt.preventDefault = function(){ window.event.returnValue = false; }
    if (!evt.stopPropagation) evt.stopPropagation = function() { window.event.cancelBubble = true; }

    if (evt.srcElement) {
      if (!evt.target) evt.target = evt.srcElement;
      
      if (!evt.relatedTarget) {    
        if (evt.type == 'mouseover')
          evt.relatedTarget = evt.fromElement;
        else if (evt.type == 'mouseout')
          evt.relatedTarget = evt.toElement;
      }

      if(!evt.pageX){ evt.pageX = evt.clientX + (document.body.parentNode.scrollLeft || 0) }
      if(!evt.pageY){ evt.pageY = evt.clientY + (document.body.parentNode.scrollTop || 0) }
      
    } else {
      
      if(evt.pageX && !evt.offsetX) { 
        var loc = findPos(evt.target);
        evt.offsetX = evt.pageX - loc[0];
        evt.offsetY = evt.pageY - loc[1];
      }
    }
    
    return evt;
 }

/*
 * Element coordinates
 * Courtesy of PPK @ http://www.quirksmode.org/js/findpos.html
 */
function findPos(obj)
{
  var curleft = curtop = 0;
  
  try {
    if (obj.offsetParent) {
      curleft = obj.offsetLeft
      curtop = obj.offsetTop
      while (obj = obj.offsetParent) {
        curleft += obj.offsetLeft
        curtop += obj.offsetTop
      }
    }
  } catch(err) {
    /* nothing to do */
  }
  return [curleft,curtop];
}

function getCoordinates(e) {
 
 var loc = findPos(e);
 var width = parseInt(e.offsetWidth)
 var height = parseInt(e.offsetHeight);
 
 return {x:loc[0], y:loc[1], w:width, h:height}
}

function getParentByTag(e, tag) {
  if (!e.parentNode) return false;
  
  if (!e.parentNode.tagName || e.parentNode.tagName.toUpperCase() != tag.toUpperCase()) 
    return getParentByTag(e.parentNode, tag);
    
  return e.parentNode;
}

function getParentByClass(e, className) {
  if (!e.parentNode) return false;
  
  var pattern = new RegExp ("(^| )"+className+"( |$)");
  if (!e.parentNode.className || -1 == e.parentNode.className.search(pattern))
    return getParentByClass(e.parentNode, className);
    
  return e.parentNode;
}

function getElementsByClass(e, className, tagName) {
  var result = new Array;
  if (!e) return result;

  if (!tagName) tagName = '*';
  
  var pattern = new RegExp ("(^| )"+className+"( |$)");
  var nodes = e.getElementsByTagName(tagName);
  
  for (var i=0; i<nodes.length; i++) {
    if (nodes[i].className && nodes[i].className.match(pattern)) {
      result.push(nodes[i]);
    }
  }
  
  return result;
}


function toggleClass(e,c1,c2) {
  if (!e) return;
  
  e.className = (e.className == c2) ? c1 : c2;
}

function hasClass(e, className) {
  if (!e || !e.className) return false;

  var pattern = new RegExp ("(^| )"+className+"( |$)");
  return (e.className.search(pattern) != -1);
}
function addClass(e, className) {

  if (e && !hasClass(e, className)) {
    e.className += (e.className ? " " : "") + className;
  }
}
function removeClass(e, className) {
  if (!hasClass(e,className)) return;
  
  var pattern = new RegExp ("(^| )"+className+"( |$)","g");
  e.className = e.className.replace(pattern," ");
  e.className = e.className.replace(/^ +| +$/,"");
  e.className = e.className.replace(/  +/g," ");
}

function hasParent(e, parent) {
  if (!e.parentNode) return false;
  if (e.parentNode == parent) return true;
  
  return hasParent(e.parentNode, parent);
}

function array_search(needle, haystack) {
  for (var i=0; i<haystack.length; i++) {
    if (haystack[i] == needle) return i;
  }
  
  return false;
}


/*
 *
 */
function set_link_targets() {
   var links = document.links;
   for (var i=0; i<links.length; i++) {
     if (links[i].className && links[i].className.match(/\b_new\b/)) {
       links[i].target = '_blank';
       addEvent(links[i],'click',newWindow,false);
     }
   }
 }
 
function newWindow(evt) {
  var e = evt.target;
  if (e.tagName != "A") e = getParentByTag(e,"A");
  if (!e) return;

  // get current size of browser window (guesstimate for IE)
  var width = window.outerWidth ? parseInt(window.outerWidth) : parseInt(document.body.offsetWidth) + 20;
  var height = window.outerHeight ? parseInt(window.outerHeight) : parseInt(document.body.offsetHeight) + 100;
  
  // shrink the size slightly for the new window
  if (width >= 400) width -= 80;
  if (height >= 400) height -= 80;
  
  // offset slightly from current window
  var screenX = (window.screenX ? window.screenX : window.screenLeft) + 40;
  var screenY = (window.screenY ? window.screenY : window.screenTop - 80) + 40;
  
  var w = window.open(e.href,e.target,"status,menubar,scrollbars,resizable,toolbar,titlebar,left="+screenX+",top="+screenY+",width="+width+",height="+height);

  evt.preventDefault();
  evt.stopPropagation();

  w.focus();
}


addEvent(window, 'load', init, false);

/*
 *
 */
function init(evt) {
    /* set up onclick handlers for links which want to operate in a new window */
    set_link_targets();

}

document.writeln('<style type="text/css"> body .js-only{ position: static; visibility: visible; } #bio-details .bio-item { display: none; } </style> ');
document.writeln('<style type="text/css"> #pagelinks .show ul { display: block; } #pagelinks .hide ul { display: none; } </style> ');
 



