    // a box that fades in/out
    var fadeBox     = new Box;
    fadeBox.bgColor     = 'white';
    fadeBox.fontColor   = 'black';
    fadeBox.borderStyle = '1px solid';
    fadeBox.delayTime   = 200;
    fadeBox.allowFade   = true;
    fadeBox.fadeIn      = 750;
    fadeBox.fadeOut     = 200;

    function AcAddTooltipClass(class_name)
    {
        // Hide the glossary
        var items = AcGetElementsByClass(document,class_name, '*');

		// Before we do anything else, are there any glossary classes?
		if (items.length > 0)
		{
			// Add the glossary header to the page
			glossary_header=document.createElement('div');
			header_text=document.createTextNode('Glossary of terms');
	
			//if ( fadeBox.isIE() )
			{
				glossary_header.className = class_name + "-header";
			}
		//	else
			{
			//	glossary_header.setAttribute("class", class_name + "-header");
			}
			
			glossary_header.appendChild(header_text);
		
			// Insert the header before the first item
			items[0].insertBefore(glossary_header, items[0].firstChild);
		
			for (glossary in items)
			{	
				// Change the class to the tooltip version
				items[glossary].className = class_name + "-tooltip";
				
				//AcDebug('Changed class of element: ' + items[glossary]);
			}    
	
			// Items found			
			return_value = true;
		}
		else
		{
			// No items found
			return_value = false;
		}
		
		return return_value;
    }

    
    function AcGetInternalAnchors()
    {
        var internal_anchors = new Array();
        
        // Get all anchors
        var anchors = document.getElementsByTagName('a');
        
        // Pull out just internal ones
        for (anchor in anchors)
        {
            try 
            {
                // Check first char of href against '#'
                if (anchors[anchor].getAttribute("href", 2).charAt(0) == "#")
                {
                    // Add to internal anchors array
                    internal_anchors[internal_anchors.length] = anchors[anchor].getAttribute("href", 2).substring(1);
                    
                    // Add an id to the anchor
                    if ( fadeBox.isIE() )
                    {
                        anchors[anchor].id = anchors[anchor].getAttribute("href", 2).substring(1) + "-anchor";
                    }
                    else
                    {
                        anchors[anchor].setAttribute("id", anchors[anchor].getAttribute("href", 2).substring(1) + "-anchor")
                    }
                    
                    
                    //AcDebug('Got internal anchor: ' + anchors[anchor].getAttribute("href", 2));
                }
                else
                {
                    //AcDebug('Got external anchor, discarding: ' + anchors[anchor].getAttribute("href", 2));
                }
            }
            catch (err)
            {
                //AcDebug('Got anchor but no href, discarding');
            }    
        }
        
        return internal_anchors;
    }
    
    function AcCreateTooltips(scope)
    {        
        // Get all classes in scope
        var elements_in_scope = AcGetElementsByClass(document, scope, "div");

        // Create array to hold named anchors
        var tooltip_anchors = new Array();
                        
        for (element in elements_in_scope)
        {
            // Search for anchors within selected element
            var anchors = new Array();

            anchors[anchors.length] = elements_in_scope[element].getElementsByTagName('a');

            for (anchor in anchors)
            {
                // Is the name attribute there?
                try 
                {
                    if(anchors[anchor][0].getAttribute("name") != 'undefined')
                    {
                        // Set the id of the parent node (if correct class name) to that of the anchor
                        AcAssignParentID(scope, anchors[anchor][0], anchors[anchor][0].getAttribute("name"));
                        
                        // Create tooltip array
                        var tooltip = new Array();
                        
                        tooltip["name"] = anchors[anchor][0].getAttribute("name");
                        tooltip["title"] = anchors[anchor][0].innerText;
                        tooltip["content"] = document.getElementById(anchors[anchor][0].getAttribute("name")).innerText;
                        
                        // Add to named anchor array
                        tooltip_anchors[tooltip_anchors.length] = tooltip;

                        //AcDebug('Got named anchor:' + anchors[anchor][0].getAttribute("name"));    
                    }
                }
                catch (err)
                {
                    //AcDebug('Got anchor :' + anchors[anchor][0].getAttribute("name") + ", not named so discarding");
                }        
            }
            

        }

        return tooltip_anchors;
    }
    
    function AcDisplayTooltip(id)
    {
        document.getElementById(id).style.display = "block";
    }

    function AcAttachTooltipEvents(anchors, tooltips)
    {

        // Loop through the internal anchors
        for (anchor in anchors)
        {    
            // Loop through the tooltips
            for (tooltip in tooltips)
            {
                // Does this anchor have a tooltip
                if (anchors[anchor] == tooltips[tooltip]["name"])
                {
                    // Attach the event
                    // Get the anchor tag 
                    var anchor_element = document.getElementById(anchors[anchor] + "-anchor");
                    
                    // Set the href attribute to a # as we don't want it going anywhere
                    if ( fadeBox.isIE() )
                    {
                        anchor_element.href = "#";  
                    }
                    else
                    {
                        anchor_element.setAttribute("href", "#");           
                    }
                    
                    // add the onclick event
                    // we must return false also as otherwise the popup fails to load if the page is scrolled
                    if ( fadeBox.isIE() )
                    {
                        anchor_element.onclick = function() { fadeBox.showTooltip(event,"load:" + this.id.replace('-anchor', '')); return false;}
                    }
                    else
                    {
                        anchor_element.setAttribute("onclick", "fadeBox.showTooltip(event,'load:" + anchors[anchor] + "'); return false;");
                    }
                    
                    // add the question mark cursor
                    if ( fadeBox.isIE() )
                    {
                        anchor_element.style.cursor = "help";
                    }
                    else
                    {
                        anchor_element.setAttribute("style", "cursor:help");
                    }

                    //AcDebug('Attached event to anchor: ' + anchors[anchor]);
                    
                    // End the loop
                    break;
                }
                else
                {
                    //AcDebug('Checking for anchor: ' + tooltips[tooltip] + ' - no match');
                }
            }
        
        }
    }
    
/***********************************
*
* Helper functions
*
***********************************/

    function AcAssignParentID(scope, child_node, id)
    {
        // Is the parent the body?
        if (child_node.parentNode.nodeName != "body")
        {
            if (child_node.parentNode.className == scope + "-tooltip")
            {
                // Add the name to the id of the anchor
                if ( fadeBox.isIE() )
                {
                    child_node.parentNode.id = id;
                }
                else
                {
                    child_node.parentNode.setAttribute("id", id);
                }
            }
            else
            // Recurse with parent
            {
                AcAssignParentID(scope, child_node.parentNode, id);
            }
        }
    }
    
    function AcGetElementsByClass(node,searchClass,tag) 
    {
        var classElements = new Array();
        var els = node.getElementsByTagName(tag); // use "*" for all elements
        var elsLen = els.length;
        var pattern = new RegExp("\\b"+searchClass+"\\b");
        for (i = 0, j = 0; i < elsLen; i++) 
        {
            if ( pattern.test(els[i].className) ) 
            {
                classElements[j] = els[i];
                j++;
            }
        }
        
        return classElements;
    }



    function AcDebug (message)
    {
        debug_area = document.getElementById("debug");
        
        debug_area.innerHTML = debug_area.innerHTML + "<p>" + message + "</p>";    
    }
    
    function AcIsSet( variable )
    {
        return( typeof( variable ) != 'undefined' );
    }
    
    window.onload = function() {
    
        // Change class to tooltip
        glossary_items = AcAddTooltipClass('_glossary');
           
        // Glossary items found?        
        if (glossary_items)
        {
			// Get glossary classes and check named anchors
			tooltip_anchors = AcCreateTooltips('_glossary');
	
			// Get list of internal anchors
			named_anchors = AcGetInternalAnchors();
	
			// Add tooltip event
			AcAttachTooltipEvents(named_anchors, tooltip_anchors);
		}
    }
    
