﻿//==============================================================================
  // Assign JQuery Behavior
  //   These functions assign behavior to the desired elements.
  //==============================================================================
  //toggle first level navigation
  $(document).ready(function() {
    $("a").filter(".FirstLevel").toggle(expandFirstJquery,collapseFirstJquery);
  });
  //toggle second level navigation
  $(document).ready(function() {
    $("a").filter(".SecondLevel").toggle(expandSecondJquery,collapseSecondJquery);
   });
  /*
  This jquery is used whenever a link is clicked on with 'class="LoadLink"'
  Necessary, because the toggle functionality "disables" the link functionalities
  */
  $(document).ready(function() {
    $(".LoadLink").click( function(){
      linkTarget = this.target=="" ? "_self" : this.target;
      window.open(this.href, linkTarget);
    });
  });
  //==============================================================================
  // Define JQuery Behavior
  //   These functions define the behaviors that toggle uses.  These functions
  //   work on the clicked element iteself, and simply call the worker function
  //    which manipulates the page.
  //==============================================================================
  function expandFirstJquery()
  {
    if (this.className.indexOf("LoadLink") < 0)
    {
      expandFirstLevel($("ul:first",this.parentNode.parentNode).get(0), false);
    }
  }
  
  function collapseFirstJquery()
  {
    if (this.className.indexOf("LoadLink") < 0)
    {
      collapseFirstLevel($("ul:first",this.parentNode.parentNode).get(0), false);
    }
  }
  
  function expandSecondJquery()
  {
    if (this.className.indexOf("LoadLink") < 0)
    {
      expandSecondLevel($("ul:first",this.parentNode.parentNode).get(0), false);
    }
  }
  
  function collapseSecondJquery()
  {
    if (this.className.indexOf("LoadLink") < 0)
    {
      collapseSecondLevel($("ul:first",this.parentNode.parentNode).get(0), false);
    }
  } 
  //==============================================================================
  // Define Toggle Functions
  //   These functions do the actual work of the toggles.
  //==============================================================================
  function expandFirstLevel(ul, slide)
  {
    //looks up the list in the surrounding tags of the link and uses a jquery functionality to hide/show them and resets the style
    $("div",ul.parentNode).filter("FirstLevel_Arrow").css("color","#ff0000");
    if ($("li",ul).size() > 0)
    {
      if (slide)
      {
        $(ul).slideDown("fast");
      }
      else
      {
        $(ul).show();
      }
    }
    /* 
      Causes the toggle state for the link to reflect that the list has been expanded.
      This is handled automatically when the link is clicked, but must be done manually if the link
        is expanded in javascript code.
    */
    $("div", ul.parentNode).filter(".FirstLevel_Link").find("a").each(function(i){
      this.last = expandFirstJquery;
    });
  }
  
  function collapseFirstLevel(ul, slide)
  {
    $("div",ul.parentNode).filter("FirstLevel_Arrow").css("color","#4f4f4f");
    if ($("li",ul).size() > 0)
    {
      if (slide)
      {
        $(ul).slideUp("fast");
      }
      else
      {
        $(ul).hide();
      }
    }
    /* 
      Causes the toggle state for the link to reflect that the list has been collapsed.
      This is handled automatically when the link is clicked, but must be done manually if the link
        is expanded in javascript code.
    */
    $("div", ul.parentNode).filter(".FirstLevel_Link").find("a").each(function(i){
      this.last = collapseFirstLevel;
    });
  }
  
  function expandSecondLevel(ul, slide)
  {
    //looks up the list in the surrounding tags of the link and uses a jquery functionality to hide/show them and resets the style
    if ($("li",ul).size() > 0)
    {
      if (slide)
      {
        $(ul).slideDown("fast");
      }
      else
      {
        $(ul).show();
      }
      $("div", ul.parentNode).filter(".SecondLevel_Button").find("img").get(0).src="/test_images/bullet_minus.jpg";
    }
    /* 
      Causes the toggle state for the link to reflect that the list has been collapsed.
      This is handled automatically when the link is clicked, but must be done manually if the link
        is expanded in javascript code.  Also, since the plus/minus icon is a seperate link from the 
        text, this must be done to assure that both links have the same state.
    */
    $("div", ul.parentNode).filter(".SecondLevel_Button").find("a").each(function(i){
      this.last = expandSecondJquery;
    });
    $("div", ul.parentNode).filter(".SecondLevel_Link").find("a").each(function(i){
      this.last = expandSecondJquery;
    });
  }
  
  function collapseSecondLevel(ul, slide)
  {
    if ($("li",ul).size() > 0)
    {
      if (slide)
      {
        $(ul).slideUp("fast");
      }
      else
      {
        $(ul).hide();
      }
      $("div", ul.parentNode).filter(".SecondLevel_Button").find("img").get(0).src="/test_images/bullet_plus.jpg";
    }
    /* 
      Causes the toggle state for the link to reflect that the list has been collapsed.
      This is handled automatically when the link is clicked, but must be done manually if the link
        is expanded in javascript code.  Also, since the plus/minus icon is a seperate link from the 
        text, this must be done to assure that both links have the same state.
    */
    $("div", ul.parentNode).filter(".SecondLevel_Button").find("a").each(function(i){
      this.last = collapseSecondLevel;
    });
    $("div", ul.parentNode).filter(".SecondLevel_Link").find("a").each(function(i){
      this.last = collapseSecondLevel;
    });
  }
  //==============================================================================
  // Define Re-expansion Functions
  //   These functions re-expand the navigation tree to the current page.
  //==============================================================================
  // Determines which page the tree should be expanded to.  If the current page is in
  // the tree, it expands to that page.  If not, it searches backwards through the
  // breadcrumb trail for pages that are in the tree and expands to the first
  // page found.
  function expandNav(id, parents)
  {
    var li = getElementById(id);
    if (li == null)
    {
      var i;
      for (i=parents.length-1; i>=0; i--)
      {
        li = getElementById(parents[i]);
        if (li != null) break;
      }
    }
    if (li != null)
    {
      expandNavTo(li);
      li.style.fontWeight = "bold";
    }
  }
  // Re-expands the Javascript tree to the selected list element (li), and expands 
  // its children if they exist
  function expandNavTo(li)
  {
    //Expands children lists if they exist
    if (li.childNodes != null)
    {
      var i;
      for (i=0; i<li.childNodes.length; i++)
      {
        if (li.childNodes[i].nodeName.toLowerCase()=="ul")
        {
          expandUL(li.childNodes[i]);
        }
      }
    }
    //Expand all parent lists
    expandParents(li);
  }
  //Recursivly expands all parent lists (ul) of the selected list element (li)
  function expandParents(li)
  {
    var ul = li.parentNode;
    if ((ul != null) && (ul.nodeName.toLowerCase()=="ul"))
    {
      li = ul.parentNode;
      if (li.nodeName.toLowerCase()=="li")
      {
        expandParents(li);
      }
      expandUL(ul);
    }
  }
  //Expands a list
  function expandUL(ul)
  {
    if (ul.className != null)
    {
      if (ul.className.indexOf("SecondLevel") >= 0)
      {
        // Expand the UL
        expandFirstLevel(ul, false);
      }
      else if (ul.className.indexOf("ThirdLevel") >= 0)
      {
        // Expand the UL
        expandSecondLevel(ul, false);
      }
    }
  }
  // A replacement for document.getElementsById that also works in preview mode
  function getElementById(id)
  {
    //Test for preview mode
    if (id.toLowerCase().indexOf("/cms/iord.asp?") == 0)
    {
      return getElementByIdInPreviewMode("li", id);
    }
    else
    {
      return document.getElementById(id);
    }
  }
  //Get's an element by ID when in preview mode.  The pageID in preview mode changes
  // accross the page, so this function parses out the GUID attribute and checks it 
  // against other ID's.
  function getElementByIdInPreviewMode(tag, id)
  {
    guid = extractGUID(id);
    var elems = document.getElementsByTagName(tag);
    var i;
    for (i=0; i<elems.length; i++)
    {
      var id = elems[i].id;
      if ((id != undefined) && (id != null) && (id != ""))
      {
        var found = extractGUID(elems[i].id);
        if (found == guid)
        {
          return elems[i];
        }
      }
    }
  }
  //Extracts the GUID attribute from a preview mode pageID
  function extractGUID(str)
  {
    var guid;
    var parts;
    var temp;
    parts = str.split("?");
    temp = parts[1];
    parts = temp.split("&");
    var i;
    for (i=0; i<parts.length; i++)
    {
      temp = parts[i];
      if (temp.indexOf("pageguid") == 0)
      {
        var subparts;
        subparts= temp.split("=");
        guid = subparts[1];
      }
    }
    return guid
  }