Check out the
Prototype javascipt framework. It's widely used, available as a single js file, and adds a number of extensions to the basic Javascript library, including functions to handle forms, element positioning, classes and functional programming.
In your case, the Position.within function would seem to be the one to use. Perhaps something like:
if (!Position.within($('my_image_id'), event.x, event.y))
// mouse cursor no longer within image with id of 'my_image_id'
Note that the above code isn't tested; it's just to give you an idea of what I mean.
Also note that $() is a wrapper around document.getElementById() provided by Prototype.