//~ if(window.addEventListener){
	//~ window.addEventListener('load', sitemap, false); 
//~ }else{
	//~ window.attachEvent('onload', sitemap);
//~ }

//			:::		START MAKE SITEMAP INTERACTIVE	:::
// This function finds all h2s and uls on the page with class="sitemap"
// The function parses them and adds an image that when clicked expands or collapses a list with all of the children

function sitemap(){
	// grab all h2 elements
	var h = document.getElementsByTagName('h2');
	// grab all unordered lists
	var u = document.getElementsByTagName('ul');
	
	for(i=0;i<u.length;i++){
		// hide all unordered lists with 'sitemap' class
		if(u[i].className == 'sitemap'){
			u[i].style.display = 'none';
		}
  }
		
		// get all links
		var lis = document.getElementsByTagName('li');
		for(z=0;z<lis.length;z++){
			// checks if the list item has a class of 'list_parent'
			if(lis[z].className == 'list_parent'){
				var li = lis[z];
        var firstchild = li.firstChild
				// creates maximise.gif element
				var img = document.createElement('img');				
				img.src = '/g/global/right.gif';
				
				li.insertBefore(img, firstchild);
				// set style
				li.className = 'list_parent';
				
				//hide child unordered list
				//~ ul = firstchild.nextSibling;
				//~ while (ul.nodeType != 1){
					//~ ul = ul.nextSibling;
				//~ }
				
				//~ ul.style.display = 'none'; // brimley edit because it hides list_parent text
				
				// make clicking new image hide/show child unordered list
				img.onclick = function(){
					li = this.parentNode;					
					ul = li.getElementsByTagName('ul')[0];
					var ulStatus = (ul.style.display == 'none') ? 'block' : 'none';
					ul.style.display = ulStatus;
					
					// toggle between maximise.gif and minimise.gif					
					imgStatus = (ulStatus == 'block') ? 'down' : 'right';
					this.src = '/g/global/' + imgStatus + '.gif';
				}
			}
		}
		
	// }
	
	for(x=0;x<h.length;x++){
    // make h2 element show/hide unordered list when clicked
    if(h[x].className == 'sitemap')
    {
      var has_ul = false;
      var ul = h[x].nextSibling;
      if(ul)
      {
        var searching = true;
        while(searching)
        {
          if(!ul)
          {
            searching = false
          }
          else if(ul.nodeType == 1)
          {
            searching = false
            if(ul.tagName == 'UL')
            {
              has_ul = true;
            }
          }
          if(searching)
          {
            ul = ul.nextSibling;
          }
        }
      }
      if(has_ul)
      {
        var img = document.createElement('img');				
        img.src = '/g/global/right.gif';
        
        h[x].insertBefore(img, h[x].firstChild);
        img.onclick = function()
        {
          h2 = this.parentNode;
          ul = h2.nextSibling;
          while (ul.nodeType != 1)
          {
            ul = ul.nextSibling;
          }
          var ulStatus = (ul.style.display == 'none') ? 'block' : 'none';
          ul.style.display = ulStatus;
          var imgStatus = (ulStatus == 'block') ? 'down' : 'right';
          this.src = '/g/global/' + imgStatus + '.gif';
        }
      }
    }
	}
  show_matching_subpages();
}

//			:::		END MAKE SITEMAP INTERACTIVE	:::

function show_matching_subpages()
{
  var divs = document.getElementsByTagName('DIV');
  var matching_divs = new Array();
  for(var i = 0; i < divs.length; i++) {
    if(divs[i].className == 'matching_subpage') {
      var count = matching_divs.length;
      matching_divs[count] = divs[i];
    }
  }
  
  for(var i = 0; i < matching_divs.length; i++) {
    var li = matching_divs[i].parentNode;
    //~ li.style.background = '#AAA';
    li.className = 'matching_subpage';
    var ul = matching_divs[i].parentNode.parentNode;
    while(ul.tagName.toLowerCase() == 'ul') {
      ul.style.display = 'block';
      ul = ul.parentNode.parentNode;
    }
  }

}