It's really easy. Just use the blur and focus events on window. You can do whatever you want to show/hide your page shadow in the callbacks. Personally, I'd recommend the BlockUI plugin.
$(function()
{
var $body = $('body');
$(window).focus(function () { $body.removeClass('fade'); })
.blur(function () { $body.addClass('fade'); });
});?
Working example: http://jsbin.com/idipo5/2 (tested in Chrome) (be forewarned, it's really ugly)
Edit: I'm pretty this what you're going for. Note how it doesn't unmask if the window is moused-over but doesn't have focus. If you do want it to unmask in this case, you can remove the focused flag and its checks.
Tested in Chrome and FF (IE is a miserable piece of not-worth-my-time):
http://jsbin.com/ufido3
Posting the JS code below, since I'm not sure how long JSBin keeps your stuff around.
$(function ()
{
var $modal = $('#modal'),
masked = false,
focused = true;
function mask()
{
if (!masked)
{
$modal.fadeIn('fast');
masked = true;
}
}
function unmask()
{
if (masked && focused)
{
$modal.fadeOut('fast');
masked = false;
}
}
$('html').hover(unmask, mask).focus(function (e)
{
e.stopPropagation();
if ($(e.target).is('html')) unmask();
}).blur(function (e)
{
e.stopPropagation();
if ($(e.target).is('html')) mask();
});
$(window).focus(function ()
{
focused = true;
unmask();
}).blur(function ()
{
focused = false;
mask();
});
});