Check if viewer is embedded only once and cache the result

I realized that I've managed to check if the viewer is embedded in two separate places in the code. Since it seems much more logical to only check once, and then cache the result, I decided to fix this.