For the windows in my project I am using a system that requires one global variable - window_Level, and an instance variable window_Level for every object (buttons, images etc.) that I am displaying in windows (add them in a family and make one instance variable for all).
So, every time you open a window, raise the global window_Level by 1 and set the instance variable window_Level to the new level for all objects of that window.
Once you click a button, check if the button instance variable is equal to the global.
Also, don't forget to reduce the value when closing a window.