/*	CapeSoft Help Contents Script
	Last Modified: 11 August 2005
	By: Sean Cameron
*/
      var collapseRecord = new Array();
      var bulletRight = new Image();
      bulletRight.src = 'images/help.gif';
      var bulletDown = new Image();
      bulletDown.src = 'images/help_open.gif';
      var oneSibling = false;
      var selectedNode = null;

      function getNavRoot()
      {
        divs = document.getElementsByTagName('div');
        for (var i = 0; i < divs.length; i++)
          if (divs[i].className == 'nav') return divs[i];
        return;

      }

      function expandChildren(root)
      {

        for (var i = 0; i < root.childNodes.length; i++)
        {
          node = root.childNodes[i];
          if (node.tagName == 'DIV')
		  {
            node.style.display = 'block';
  			if (i == 0)
			{	/* This code should select the first child node, however it is not currently working */
			
			    // deselect the current selection
				selectedNode.className = ''
				selectedNode.backgroundColor = 'white';
				// select this node
				selectedNode = node;
	   		    node.className = 'selected'
				node.style.backgroundColor = '#EEEEEE';
			}
		  }	  
        }
        root.setAttribute('expanded', true);
      }

      function collapseChildren(root)
      {
        for (var i = 0; i < root.childNodes.length; i++)
        {
          node = root.childNodes[i];
          if (node.tagName == 'DIV')
            node.style.display = 'none';
        }
        root.setAttribute('expanded', false);
      }

      function navOnClick(event)
      {
        s = srcElement(event);

   	    /* Code to keep the selection persistant */
		// change the style of the previous node to be deselected
		selectedNode.className = '';
		selectedNode.style.backgroundColor = '#FFFFFF';
		
        if (this.childNodes.length > 1 && (s.parentNode == this || s.parentNode.parentNode == this))
          toggleChildren(this);
		else
		{		
			// show the current node as selected
			selectedNode = this;								// store the new selection
			this.className = 'selected'
			this.style.backgroundColor = '#EEEEEE';
			/* ------------------------------------- */
		}
		  

        // Moz and IE return null for href, NS returns '' with the attribute is not supplied
        if (this.getAttribute('href') == null || this.getAttribute('href') == '')
          // the clicked node does not have an HREF assigned
          return cancelAll(event);
        else
        {
          cancelAll(event);
		  /* Commented out the below code as it break loading in a frame using IE. 
             Works correctly now in IE, Firefox and Netscape                         
		  
          if (window.event)
            window.event.returnValue = true;
	      */

          return true;
        }
      }

      function navOnMouseOver(event)
      {
        this.style.backgroundColor = '#EEEEEE';
        return cancelAll(event);
      }

      function navOnMouseOut(event)
      {
        if (this.className != 'selected')
          this.style.backgroundColor = 'white';
        else
          this.style.backgroundColor = '#EEEEEE';
        return cancelAll(event);
      }

      function setupChildren(root)
      {
        var depth = root.getAttribute('depth');
        for (var i = 0; i < root.childNodes.length; i++)
        {
          node = root.childNodes[i];
          if (node.tagName == 'DIV')
          {
            href = node.getAttribute('href');
            
            // set up the bullets.
            var a = document.createElement('A');
            a.href = href;

            if (node.childNodes.length > 1)
              img_name = 'images/help.gif';
            else
              img_name = 'images/help_bullet.gif';

            a.innerHTML = '<img src="' + img_name + '" width="16" height="16" border="0" valign="middle" alt="" />&nbsp;' + node.firstChild.nodeValue;
            a.style.width = '100%';
                  
                  
            var target = node.getAttribute("target");           // code to open links to external pages in a new window
            if (target)
                var extUrl = target.indexOf("http://");
            
            if (href)
            {
                if (extUrl == -1)                       // reference to an external site               
                    a.onclick = new Function('if (this && this != null && this != \'null\') window.open(href, "_blank"); return false;');                                   
                    
                else
                    a.onclick = new Function('if (this && this != null && this != \'null\') parent.ContentsFrame.location.href = this; return false;');
            }
                
            node.replaceChild(a, node.firstChild);
            
            // set up the events
            node.setAttribute('depth', depth + 1);
            node.onclick = navOnClick;
            node.onmouseover = navOnMouseOver;
            node.onmouseout = navOnMouseOut;
            node.style.cursor = 'pointer';
            //node.style.cursor = 'hand';
            if (node.className != 'selected')
              	node.style.backgroundColor = 'white';
            node.setAttribute('id', root.getAttribute('id') + '_' + i);
            if (depth > 0) 
				node.style.display = 'none';
            
            if (node.className == 'selected') 
				selectedNode = node;
				
            setupChildren(node);
          }
        }
      }

      function cancelAll(event)
      {
        if (window.event) 
			event = window.event;
			
        if (event)
        {
          event.returnValue = false; 
          event.cancelBubble = true; 
        }
        return false;
      }

      function isExpanded(node)
      {
        var expanded = node.getAttribute('expanded');
        if (expanded && expanded != 'false') return true;
        if (!expanded || expanded == 'false') return false;
        return true;
      }

      function toggleChildren(root)
      {
        var expanded = isExpanded(root);
        if (expanded) 
        {
		  // show the current node as selected
		  selectedNode = root;								// store the new selection
   		  root.className = 'selected'
		  root.style.backgroundColor = '#EEEEEE';

          root.firstChild.firstChild.src = bulletRight.src;
          collapseChildren(root);         
        }
        if (!expanded)
        {
          if (oneSibling)
          {
            // first collapse all expanded siblings if the option is selected
            parentNode = root.parentNode;
            for (var i = 0; i < parentNode.childNodes.length; i++)
            {
              node = parentNode.childNodes[i];
              if (node.tagName == 'DIV')
                if ((node != root) && (node.childNodes.length > 1) && isExpanded(node))
                  toggleChildren(node);            
            }
          }

          root.firstChild.firstChild.src = bulletDown.src;
          expandChildren(root);
        }
      }

      function attachEvents()
      {
        nav = getNavRoot();
        if (!nav) return;
        
        nav.setAttribute('depth', 0);
        nav.setAttribute('id', 'nav');
        setupChildren(nav);
        if (selectedNode)
          exposeNode(selectedNode);
                    
      }

      function srcElement(event)
      {
        if (window.event) 
			return window.event.srcElement;
			
        return event.target;
      }


      function exposeNode(node)
      {
        node = node.parentNode;
        while (node.className != 'nav')
        {
          expandChildren(node);
          node.firstChild.firstChild.src = bulletDown.src;
          node = node.parentNode;
        }
      }

