jQuery(function($){

  function setcurrent(url) {
     if (!url) {return}
     var matches = {};

     $("div.col ul a").each(function(){
          var href = this.href;
          if (url.indexOf(href) > -1) {
            matches[href.length] = this;
          }
     });

     var max = 0;
     var match;
     for (x in matches) {
       if (x>max) {
         match = matches[x];
         max = x;
       }
     }
     $(match).addClass("on");
  }

  //setcurrent("http://www.exed.hbs.edu/atwork/");
  var ref = document.referrer;
  if (document.getElementById("notfound")) { ref = document.location.href }
  ref = ref.replace("webdev","www");
  ref = ref.replace("webstage","www");
  setcurrent(ref);

  var bullet = '<img src="/about/images/site/icon.bullet.gif" alt="" height="11" width="11" class="icon" />';
  var expandable = '<img src="/about/images/site/icon.expandable.gif" alt="" height="11" width="11" class="icon" />'
  var expanded = '<img src="/about/images/site/icon.expanded.gif" alt="" height="11" width="11" class="icon" />'
  var arrow1  = '<img src="/about/images/site/icon.arrow.gif" alt="" height="10" width="12" class="arrow" />'
  var arrow2  = '<img src="/about/images/site/icon.arrow.gif" alt="" height="10" width="12" class="icon" />'

  $("div.col>ul>li").each(function(){
      $this = $(this);
      if ($this.find(">ul").size() > 0) {
        $(this).addClass("expandable");
        if ($this.find(">a.on").size() == 0) {
           if ($this.find("a.on").size() > 0) {
              $(this).prepend(expanded);
           } else {
              $(this).prepend(expandable);
              $(this).addClass("closed");
           }
        }
      } else {
        if ($this.find(">a.on").size() == 0) {$(this).prepend(bullet);}
      }
  })

  $("div.col>ul>li>a.on").prepend(arrow2);
  $("div.col>ul ul li a.on").prepend(arrow1);

  $("img.icon").click(function(){
     $(this).parent().toggleClass("closed");
     $(this).trigger("refresh");
     return false;
  });

  $("img.icon").bind("refresh",function(){
     if ($(this).parent().hasClass("closed")) {
        this.src = this.src.replace("expanded","expandable");
     } else {
        this.src = this.src.replace("expandable","expanded");
     }
  });

  $("#expandall").click(function() {
     if ($(this).html().indexOf("Expand") > -1 ) {
         $("li.closed").removeClass("closed");
         $(this).html("Collapse All");
         $(this).css("background-image","url(/about/images/site/icon.expanded.gif)");
     } else {
         $("li.expandable").addClass("closed");
         $(this).html("Expand All");
         $(this).css("background-image","url(/about/images/site/icon.expandable.gif)");
     }
     $("img.icon").trigger("refresh");
     return false;
  });

});
