Changing Header -- Best Way To Implement?

The background image of my header (which is included in the main layout file) changes for a variety of circumstances. It will be different depending on the type of account a user has, what cookies he has, and whether or not he is logged in. So there are a number of possible combinations, and more could be added at any time (eg, if we needed to add a new account type).

Thus the logic is not trivial, and it would be pretty cumbersome, and seemingly wrong, to put it in the view file. On the other hand, this logic is not attached to any specific controller action (it is called on nearly every page), so there is no natural controller to put it into. So Where would be the best place to put this logic which determines which header background image is going to be included in the header?

Probably in a widget which can be called from the layout file.

Right, good idea. And what would be the most appropriate directory to put the widget in?