jQuery(document).ready( function($) {
    if ($('.snav').length) {
        var menu_padding = 40;
        $right_margin = $(".snav").parent("div").offset().left + $(".snav").parent("div").innerWidth();
        $(".snav").each( function (a) {
            $(this).children("li").each( function (i) {
                    $(this).children("ul").each( function (j) {
                            $max_width = 0;
                            $(this).children("li").each( function (k) {
                                    $(this).children("ul").each( function (l) {
                                            $child_max_width = 0;
                                            $(this).children("li").each( function (m) {
                                                    $this_width = $("a", this).width();
                                                    if ($this_width >= $child_max_width) {
                                                        $child_max_width = $this_width;
                                                    }
                                                });
                                            $(this).width($child_max_width + menu_padding);
                                            if ($(this).offset().left + $(this).innerWidth() + $("ul", this).innerWidth() > $right_margin) {
                                                $(this).css({
                                                    left: - $(this).width()
                                                });
                                            }
                                        });
                                    $this_width = $("a", this).width();
                                    if ($(this).attr("class") == "dir") {
                                        $this_width += 10;
                                    }
                                    if ($this_width >= $max_width) {
                                        $max_width = $this_width;
                                    }
                            });
                            $(this).width($max_width + menu_padding);
                            $max_width = 0;
                            if ($(this).offset().left + $(this).width() > $right_margin) {
                                $(this).css({
                                    left: -($(this).width() - $(this).parent("li").width())
                                });
                            }
                    });
            });
        });
    }
});
