My menus are...

main menu 1
-->sub menu 11
-->sub menu 12
-->sub menu 13
-->sub menu 14
main menu 2
-->sub menu 21
-->sub menu 22
-->sub menu 23
-->sub menu 24
-->sub menu 25
-->sub menu 26
main menu 3
-->sub menu 31
-->sub menu 32
-->sub menu 33
-->sub menu 34

I want a code to display main menu 1, main menu 2 and main menu 3 initially. When anyone clicks on main menu 1 it should display the sub menus of main menu 1. When anyone clicks on main menu 2, main menu 2 should expand and main menu 1 should be hidden automatically.

I also want to expand the main menu 1 automatically if one of the sub menus of main menu 1 is active.

Can anyone guide me in the right direction?

Any help is greatly appreciated. Thanks.