/*   *********   Javascript for South African Lineage Page   *********   */

/*   ****   Global Variables   *****   */

     var path_num = "";

     var kit_number = "";

/*   ***   End Gobal Variables   ***   */

    addLoadEvent(initializePage); 

/*  ****************************************************  */

  function initializePage()
    {
       document.getElementById("nojava").style.display = 'none';

       document.getElementById("sidebar").style.display = 'block'; 

       document.getElementById("tree").style.borderLeft = '2px solid black';

       first_run = true;

       setLineage();

       if ( path_num != "")
          { 
            collapse();

            kitNumber(kit_number)
          }
     }
 


/*  ****************************************************  */

  function setLineage()
    {
                                        /*  This function determines which path
                                            and sets the elements of that path
                                            to the 'lineage' class.
                                        */
      var myelem = document.getElementById("tree");
      myelem.style.color = "#A0A0A0";

      findAnchor();                     // determine which path is selected

      if (path_num == "")
         {                              // If no path is given show all.
           showAll();

           document.getElementById("sidebar").style.display = "none";
           document.getElementById("original").style.display = "none";
           document.getElementById("expand").style.display = "none";
           document.getElementById("showAll").style.display = "none";
           document.getElementById("fade").style.display = "none";
           document.getElementById("tree").style.color = "black";   
           document.getElementById("tree").style.borderLeft = "0";
           document.getElementById("tree").style.marginLeft = '50px';

         }
        else
         {
           setPath(path_num);           // sets path elements to 'lineage' class

           document.getElementById("original").style.display = "none";
           document.getElementById("expand").style.display = "block";
           document.getElementById("showAll").style.display = "none";
           document.getElementById("fade").style.display = "none";
         }
     }


//  alert("there is no path number.");

/*  ****************************************************  */

  function findAnchor()
    {
                                        /*  This function takes the 'ID Number' of
                                            the lineage in question and from it
                                            determines the path_num to be used
                                            in the 'setLineage' function.
                                        */
      page_loc = window.location;
      group = page_loc.href.split("#");
      this_case = group[1];
      kit_number = this_case;
      
      switch(this_case)
        {
          case "73298":
             path_num = "path1";
             break;
          case "87825":
             path_num = "path2";
             break;
          case "89593":
             path_num = "path3";
             break;
          case "87448":
             path_num = "path4";
             break;
          case "87173":
             path_num = "path5";
             break;
          default:
             path_num = "";
             break;
       }
     return path_num;
     return kit_number;
    }




/*  ****************************************************  */

  function setPath(path_num)

            /*
               This function is used by the 'setLineage' function
               to set the elements of the selected path to the 
               'lineage' class, thereby setting it apart from
               the rest of the tree.
            */

    {
                                          //  Looking for a tag with the selected 'path' class.

      var the_list = document.getElementsByTagName("*");
      for ( var i = 0; i < the_list.length-1; i++)
          {
            var this_tag = the_list[i];

                                          /*  check to see if this_tag has the correct
                                              path_num and if it does it adds the 'lineage'
                                              class to class string.
                                          */

            if ( hasClass(this_tag,path_num) && !hasClass(this_tag,'gen')) addClass(this_tag,"lineage");
          }
    }




/*  ****************************************************  */

   function showAll()

            /*
               This function takes all elements that do not have the 'lineage'
               class and sets sets their 'color' to black, the same as the
               selected path elements.
            */

     {
/*   +++++++++++++++++++++++++++++++++++++++++++++++++++++

     Note: I removed this section, because it appeard not to be necessary.
           (It removed the 'lineage' class from those divisions that had it.)
           All that was required was to chnage 'tree' color to 'black'.

                 document.getElementById("tree").style.color = "black";   
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/

      var the_list = document.getElementsByTagName("*");
      for ( var i = 0; i < the_list.length-1; i++)
          {
            var this_tag = the_list[i];

            if ( hasClass(this_tag,"lineage"))
               {
                 index = this_tag.className.indexOf("lineage");
                 first_part = this_tag.className.slice(0,index-1);
                 this_tag.className = first_part;
                 document.getElementById("tree").style.color = "black";   
               }
          }


                                        // hide the 'original' button

        document.getElementById("original").style.display = "block"
        document.getElementById("expand").style.display = "none"
        document.getElementById("showAll").style.display = "none"
        document.getElementById("fade").style.display = "block";
     }
       



/*  ****************************************************  */

   function collapse()

            /*
               This function takes all elements that do not have the 'lineage'
               class and sets their display to 'none', so only the selected 
               path elements will be displayed. It also hides the 'show all'
               and 'Original Line' buttons.
            */

     {
      deleteLines();                  // deletes non-path generation lines.

                                      /* Find all elements that have a 'path' class
                                         but do not have the 'lineage' class and set
                                         their display to 'none'.
                                      */

      var the_list = document.getElementsByTagName("*");
      for ( var i = 0; i < the_list.length-1; i++)
          {
            var this_tag = the_list[i];

            if ( hasClass(this_tag,"noleft") && !hasClass(this_tag,"lineage")) this_tag.style.borderLeft = "0";

            if ( hasClass(this_tag,"gen")) continue;

            if ( hasClass(this_tag,"path") && !hasClass(this_tag,"lineage"))
               {
                  this_tag.style.display = "none";
                 
               }

          }
        document.getElementById("showAll").style.display = "none"
        document.getElementById("original").style.display = "none"
        document.getElementById("expand").style.display = "block"
        document.getElementById("fade").style.display = "none";
     }




/*  ****************************************************  */

   function uncollapse()
     {
      if (!first_run) restoreLines()  
      setLineage();

      var the_list = document.getElementsByTagName("*");
      for ( var i = 0; i < the_list.length-1; i++)
          {
            var this_tag = the_list[i];

            if ( this_tag.getAttribute('ID') == "nojava") continue;

            if ( hasClass(this_tag,"last")) this_tag.style.borderLeft = "0";

            if ( hasClass(this_tag,"noleft")) this_tag.style.borderLeft = "1px solid blue";

            if ( this_tag.style.display == "none")
               {
                 this_tag.style.display = "block";
               }
          }
        document.getElementById("showAll").style.display = "block"
        document.getElementById("original").style.display = "block"
        document.getElementById("expand").style.display = "none"
        document.getElementById("fade").style.display = "none";
     }




/*  ****************************************************  */

   function deleteLines()
     {

//            This function hides the lines between siblings.

      switch(path_num)
        {
          case "path1":
             hide_num = "hide1";   // Hides all but path1 elements.
             break;
          case "path2":
             hide_num = "hide2";   // Hides all but path2 elements.
             break;
          case "path3":
             hide_num = "hide3";   // Hides all but path3 elements.
             break;
          case "path4":
             hide_num = "hide4";   // Hides all but path4 elements.
             break;
          case "path5":
             hide_num = "hide5";   // Hides all but path5 elements.
             break;
          default:
//             hide_num = "";
        }

        var div_list = document.getElementsByTagName("div");
        for ( var i = 0 ; i < div_list.length; i++)
            {

              if ( hasClass(div_list[i],hide_num))
                    {
                      div_list[i].style.borderLeft = "0";

                                    //  Add left border to H5 headers

                      var children = div_list[i].childNodes;
                      for ( var j = 0; j < children.length; j++)
                          {
                             if ( children[j].nodeName == "DIV" || children[j].nodeName == "H5")
                                { 
                                  addClass(children[j],"needs_lines");
                                }
                          }

                    }
            }
       first_run = false;
    }




/*  ****************************************************  */

   function restoreLines()
     {
       first_run = true;
       switch(path_num)
        {
          case "path1":
             hide_num = "hide1";
             break;
          case "path2":
             hide_num = "hide2";
             break;
          case "path3":
             hide_num = "hide3";
             break;
          case "path4":
             hide_num = "hide4";
             break;
          case "path5":
             hide_num = "hide5";
             break;
          default:
             hide_num = "";
        }

        var div_list = document.getElementsByTagName("div");
        for ( var i = 0 ; i < div_list.length; i++)
            {
                                          // retsore lines to divs where they were deleted.

              if ( hasClass(div_list[i],hide_num))
                    { div_list[i].style.borderLeft = "1px solid blue";

                                          // remove lines to divs & H5s where they were added.

                      var children = div_list[i].childNodes;
                      for ( var j = 0; j < children.length; j++)
                          {
                             if ( children[j].nodeName == "DIV" || children[j].nodeName == "H5")
                                {
                                  if ( hasClass( children[j],'needs_lines'))
                                     {
                                       removeClass( children[j],'needs_lines' );
                                     }
                                }
                          }
                    }
            }
    }

/*  ****************************************************  */

   function kitNumber(this_kit)
     {
 
            /*
               This function is called from 'initialPages' 
               and sets the page to display the proper kit number.
            */

      for( var j= 1; j < 5; j++)
         {
            children = document.getElementById("number"+j).childNodes

            for ( var i = 0; i < children.length; i++)
                { 
                  if ( children[i].nodeType == "3")
                     {
                       //  children[i].nodeValue = "Kit #"+this_kit;
                      children[i].nodeValue = " Kit #"+this_kit;
                     }
                }
          }
     }

/*  ****************************************************  */

   function returnToOrig()
     {
 
            /*
               This function starts at the espanded tree with or with faded 
               entries, and returns to the original requested line.
            */

        uncollapse();
        collapse();
     }


