jQuery(function($) {

// tag main nav too close to the right edge
    var width = $("#nav").outerWidth();
    $("#nav>li").filter(function(i) {
        var pos = $(this).position();
        var childWith = $(this).children("ul").outerWidth();
        
        //alert("width: " + width + "pos left: " + pos.left + "Cw:" + childWith);
        
        return width - pos.left < childWith;

    }).addClass("right-side");

    // split tertiary nav lists into two columns, where the size of the links allows
    $("#nav li.sn").each(function() {
        // get width of sublist
        var $sublist = $(this).children("ul.tn");
        var childWidth = $sublist.outerWidth();

        // find max width of links in sublist
        var maxLinkWidth = 0;
        $sublist.find("li.tn > a").each(function() {
            var linkWidth = $(this).outerWidth();
            maxLinkWidth = (linkWidth > maxLinkWidth) ? linkWidth : maxLinkWidth;
        });

        // if less than half list's width, split sublist into two halves
        if (maxLinkWidth <= (childWidth - 15) / 2) {
            var $items = $sublist.children("li");
            var $clone = $sublist.addClass("half").clone();
            var split = Math.ceil($items.length / 2);

            $items.slice(split).remove();
            $clone.children("li").slice(0, split).remove();
            $sublist.after($clone);
        }
    });
});	
