//addLoadEvent by Simon Willison
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
 }
 
//Much gratitude to Isofarro from the GAWDS_discuss list for providing the original version of this code.
/**********************************************************************
 *
 * initCollapsedLists - initialise definition list
 *
 *********************************************************************/


var openClass  = 'opened';
var closeClass = 'closed';

// Find all definition lists that want to be collapsed
function initCollapsedLists() {
       if (!document.getElementById || !document.getElementsByTagName) {
               return false;
       }

       var lists = document.getElementsByTagName('DL');
       for (var i=0; i < lists.length; i++) {
//               if (/\bcollapseList\b/.exec(lists[i].className)) {
                       initDefinitionList(lists[i]);
//               }

       }

}


// Initialise the selected definition list as a collapsable list
function initDefinitionList(listNode) {
       //alert('initialising: ' + listNode.nodeName);

       listItem = listNode.firstChild;
       while (listItem) {
               if (listItem.nodeType == 1) {
                       //alert('ListItem: ' + listItem.nodeName);
                       switch (listItem.nodeName) {
                               case 'DT':
                                       var linkEl = document.createElement('a');
                                       linkEl.setAttribute('href', '#toggle');
                                       linkEl.onclick = function() {
                                               toggleChildDefs(this.parentNode);
                                       }

                                       // Copy all nodes from the DT into the element
                                       var termChildren = listItem.childNodes;
                                       for (var j=0; j < termChildren.length; j++) {
																			 	if (termChildren[j].nodeName == 'SPAN')
                                               linkEl.appendChild(termChildren[j]);
                                       }

                                       listItem.appendChild(linkEl);

                                       break;
                               case 'DD':
                                       listItem.className = closeClass;
                                       break;
                               default:
                                       alert('Unhandled element: ' + listItem.nodeName);
                                       break;
                       }
               }

               listItem = listItem.nextSibling;
       }

}

// Add the main function to the window.onload
//addLoadEvent(initCollapsedLists);


function toggleChildDefs(termNode) {
       var listItem = termNode.nextSibling;
       while (listItem) {
               if (listItem.nodeType==1) {
                       if (listItem.nodeName=='DD') {
                               toggleClassName(listItem);
                               break;
                       }

               }

               listItem = listItem.nextSibling;
       }

}

function toggleClassName(item) {
       if (item.className==closeClass) {
               item.className=openClass;
       } else {
               item.className=closeClass;
       }
}

function sfHover() {
	var sfEls = document.getElementById("nav").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"), "");
		}
	}
}

