Ok, I gave it a try and noticed the admin menu has 3rd level buttons. I decided to make the ids be button_$act_$child_act, and button_$act_$child_act_$grandc_act as the odds of a name collision at that point are lower than just using one of the subvariables.
I then ran the main page through an html5 validator and it was fine. Here's the modified code secion.
// Any 2nd level menus?
if (!empty($button['sub_buttons']))
{
echo '
<ul class="menulevel2">';
foreach ($button['sub_buttons'] as $child_act => $childbutton)
{
echo '
<li id="button_', $act , '_', $child_act, '" class="listlevel2', !empty($childbutton['sub_buttons']) ? ' subsections" aria-haspopup="true"' : '"', '>
<a class="linklevel2" href="', $childbutton['href'], '" ', isset($childbutton['target']) ? 'target="' . $childbutton['target'] . '"' : '', '>', $childbutton['title'], '</a>';
// 3rd level menus :)
if (!empty($childbutton['sub_buttons']))
{
echo '
<ul class="menulevel3">';
foreach ($childbutton['sub_buttons'] as $grandc_act => $grandchildbutton)
echo '
<li id="button_', $act, '_', $child_act, '_', $grandc_act, '" class="listlevel3">
<a class="linklevel3" href="', $grandchildbutton['href'], '" ', isset($grandchildbutton['target']) ? 'target="' . $grandchildbutton['target'] . '"' : '', '>', $grandchildbutton['title'], '</a>
</li>';
echo '
</ul>';
}
Haven't attached a script to any of them yet but I don't see why it would be a problem.