//#### BEGIN NAVIGATION BLOCK (MORE OR LESS) FUNCTIONS ####//

  $(document).ready(function(){

    function getURLPath(){

      return escape(window.location.pathname);
    }
    
    // The non-js version of the html needs to have cleanbottom to style correctly, but in the js version it will mangle the toggler... 
    $("div.list:has(div.moreorless) + div.toggle.cleanbottom").removeClass("cleanbottom").addClass("bottom");

    // find all the lists that have a moreorless div
    $("div.list").each(function(){

      var moreorless  = $("div.moreorless",this);

      if (moreorless != null && moreorless.length > 0) {

        // The first child of the moreorless block is a script element containing the data object as JSON
        var data = eval("(" + moreorless.children()[0].innerHTML + ")");
        
        //Check for a saved state on this part of the list, saved would indicate that it was opened
        var SavedState = readCookie("SavedState_" + getURLPath() + "_" + $("*").index($(moreorless)));
        if(SavedState == null){  

          var toggler = $("<span><a class='down' href='#'>" + data.moreText + "</a></span>");

          //The toggleable items are visible if you don't have js, so start by hiding them
          $(moreorless).toggle();

        }else{

          var toggler = $("<span><a class='up' href='#'>" + data.lessText + "</a></span>");
        }

        // Wire up the toggling behaviour
        toggler.click(function(){
          
          moreorless.toggle();

          if (moreorless.is(":visible")){
            $("a", toggler).text(data.lessText).removeClass("down").addClass("up");
            setCookie("SavedState_" + getURLPath() + "_" + $("*").index($(moreorless)),true,"/",1);
          }else{
            $("a", toggler).text(data.moreText).removeClass("up").addClass("down");
            setCookie("SavedState_" + getURLPath() + "_" + $("*").index($(moreorless)),"","/",-1);
          }
          return false;           		    
        });

        // and inject the toggler span into the div which follows the div.list 
        $("+ .toggle", this).append(toggler);
      }
    });
  });

//#### END NAVIGATION BLOCK (MORE OR LESS) FUNCTIONS ####//
