function addLoadEvent(func){
// author: Simon Willisons - http://simon.incutio.com/archive/2004/05/26/addLoadEvent
  if(!document.getElementById || !document.getElementsByTagName)return
  var oldonload=window.onload
  if(typeof window.onload!='function')window.onload=func
  else window.onload=function(){oldonload();func()}
}

function $T(id,el){return el?document.getElementById(id).getElementsByTagName(el):document.getElementById(id)}

function idExists(id){return($T(id)?true:false)}

function replaceContent(id,content){if(idExists(id))$T(id).innerHTML=content}

function jsCSS(action,obj,class1,class2){
// author: Christian Heilmann - http://onlinetools.org
  switch (action){
    case 'swap':
        obj.className=!jsCSS('check',obj,class1)?obj.className.replace(class2,class1): obj.className.replace(class1,class2)
        break
    case 'add':
        if(!jsCSS('check',obj,class1)){obj.className+=obj.className?' '+class1:class1}
        break
    case 'remove':
        var rep=obj.className.match(' '+class1)?' '+class1:class1
        obj.className=obj.className.replace(rep,'')
        break
    case 'check':
        return new RegExp('\\b'+class1+'\\b').test(obj.className)
        break
  }
  return false
}

// XMLHttpRequest methods:
// author Jim Ley - http://jibbering.com/2002/4/httprequest.html
var xmlhttp=false
/*@cc_on @*/
/*@if (@_jscript_version>=5)
    try{xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")}
    catch(e){
      try{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")}
      catch(E){xmlhttp=false}
    }
  @else
    xmlhttp=false
  @end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined'){
  try{xmlhttp=new XMLHttpRequest()}
  catch(e){xmlhttp=false}
}
if (!xmlhttp && window.createRequest){
  try{xmlhttp=window.createRequest()}
  catch(e){xmlhttp=false}
}

function insertAfter(targetElement,newElement){
  var parent=targetElement.parentNode
  if(parent.lastChild==targetElement) parent.appendChild(newElement)
  else parent.insertBefore(newElement,targetElement.nextSibling)
}

function findPos(obj){
// author: Peter-Paul Koch - http://www.quirksmode.org/js/findpos.html
  var curleft=0,curtop=0
  if(obj.offsetParent){
    curleft=obj.offsetLeft
    curtop=obj.offsetTop
    while(obj=obj.offsetParent){
      curleft+=obj.offsetLeft
      curtop+=obj.offsetTop
  } }
  return [curleft,curtop]
}

function browserOffset(){
// not essential, just neater
  var agt=navigator.userAgent.toLowerCase()
  if (agt.indexOf("msie")!=-1) return 26
  if (agt.indexOf("firefox")!=-1) return 26
  return 0
}

function getAjaxObject(obj){
  // please wait notice
  obj.nextSibling.appendChild(waitImg)
  obj.nextSibling.appendChild(document.createTextNode(" Please wait..."))

  // set x y positioning
  obj.nextSibling.id="term"
  $T('term').style.top=findPos(obj)[1]+browserOffset()+'px'
  $T('term').style.left=findPos(obj)[0]+'px'

  // this version calls glossary.php?term=glossary_term
  // cleaning the called html is done in php
  var contentFile=obj.href.replace(/#/g,"?term=")
  if(xmlhttp){
    xmlhttp.open("GET",contentFile,true)
    xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4)replaceContent('term',xmlhttp.responseText)}
    xmlhttp.send(null)
  }
/*
  // this version fetches terms/glossary_term.html directly
  var contentFile=obj.href.replace(/glossary.php#/g,"terms/")+'.html'
  if(xmlhttp){
    xmlhttp.open("GET",contentFile,true)
    xmlhttp.onreadystatechange=function(){
      if(xmlhttp.readyState==4)
        replaceContent('term',xmlhttp.responseText.replace(/<dt /,'<span class="dt" ').replace(/<\/dt>/,'</span>').replace(/<dd/g,'<span class="dd"').replace(/<\/dd>/g,'</span>'))
        // note: IE will not insert block code into an inlne element
    }
    xmlhttp.send(null)
  }
*/
  return false
}

function resetTitles(){for(var i=0;i<glossaryLinks.length;i++)glossaryLinks[i].title=openText}

function closeGlossaries(){for(var i=0;i<glossaryLinks.length;i++){glossaryLinks[i].nextSibling.innerHTML="";glossaryLinks[i].nextSibling.id=""}}

function clickedTerm(obj){
  closeGlossaries()
  if(obj.title==openText){
    getAjaxObject(obj)
    resetTitles()
    obj.title=closeText
  }else resetTitles()
  obj.onclick=function(){clickedTerm(this);return false}
  obj.nextSibling.onclick=function(){closeGlossaries();resetTitles();return false}
  return false
}

function setupGlossary(){
  var links=$T('content','a')
  for(var i=0;i<links.length;i++){
    if(jsCSS('check',links[i],'glossary')){
      links[i].innerHTML=links[i].innerHTML+'<img src="graphics/transparent.png" width="1" height="1" title="" alt=" (glossary term) " />'
      glossaryLinks[glossaryLinks.length]=links[i]
      insertAfter(links[i],document.createElement('span'))
      links[i].title=openText
      links[i].onclick=function(){clickedTerm(this);return false}
} } }

var waitImg=document.createElement('img')
waitImg.width="24"
waitImg.height="24"
waitImg.src="graphics/please_wait.gif"
waitImg.alt=""
var glossaryLinks=new Array()
var openText="Expand glossary term"
var closeText="Contract glossary term"

addLoadEvent(setupGlossary)
