Zend certified PHP/Magento developer

How to set custom menu items active in top menu depending on URL visited (menu caching issues)?

When I add custom menu items to the top menu (where product categories are placed) they are not set as active or are always active, depending on logic I implement for checking active state, no matter the URL. The actual issue is caching, I guess.

So, how to set menu items active, depending on URL visited? So it would work the same way as with product categories?