﻿$(document).ready(function() {
    var menu = $("#sub-navlist");
    menu.addClass("sf-menu");
    menu.addClass("sf-vertical");
    
    
    $.get("/menu.htm", function(data) {
       setupMenu(data);
    });
    
});

function setupMenu(items)
{
    var jItems = $(items);
    var topLevel = jItems.children();
    var subNavItems = $("#sub-navlist").children();
    var domain = window.location.protocol + "//" + window.location.hostname + "/";
    
    $.each(subNavItems, function() {
       var current = $(this);
       var link =  current.find("a:first");
       
       if (link == null || link.length == 0) return true;
       
       var href = link.attr("href").replace(domain,"");
       var newItems;
       var currentHref = window.location.pathname;
       currentHref= currentHref.substring(1,currentHref.length);
       $.each(topLevel, function() {
           var topHref =  $(this).find("a:first").attr("href").replace(domain,"");
           if (topHref==href)
           {
              newItems = $(this).find("ul:first");
              return true;
           }
       });
       if (newItems != null && newItems.length > 0)
       {
           current.append(newItems);
       }
       
       if (href==currentHref)
           current.find("a:first").addClass("selected");
       else {
         // check the children
         if (current.find("a[href$=" + currentHref + "]").length > 0)
           current.find("a:first").addClass("selected");  
       } 
    });
    
    //alert(subNavItems.length);

    var menu = $("#sub-navlist");
    menu.superfish({
	    autoArrows: false,
	    dropShadows: false,
	    delay: 200
	});
}

