Best way to build dynamic sub menus?

Thanks but that isn’t what I’m after. Your code build a menu with submenus.
I need one flat menu. It shows my 7 ‘root’ pages.
If I open a page with children, the whole menu is replaced with the page itself, and it’s children.

Home
 - p1
 - p2
 - p3
    --p3-sub1
    --p3-sub2
        ---sub2-subsub1
        ---sub2-subsub2
    --p3-sub3
- p4
- p5

On the home page the menu is: HOME | p1 | p2 | p3 | p4 | p5
On p3 - the menu is: p3 | p3-sub1 | p3-sub2 | p3-sub3

working so far. But then on p3-sub2 the menu still reads: p3 | p3-sub1 | p3-sub2 | p3-sub3
Where it should read: p3-sub2 | -sub2-subsub1 | -sub2-subsub2