in chrome and safari the image has to be cached before u can use this script to get it's height/width, so u should try to execute this function after the image is printed on the screen.
you are trying to use the variables you got of the ajax call outside your call, this is not possible. And indeed, it could be possible that the function is still busy when ur creating the variable...
Instead of toggling the class, maybe you can try to set the background immediatly when clicking it, cuz i don't think IE7 checks the classes when changed without refresh..
you can run your own javascript server using ASP: JScript.NET that way u can call ur javascript with Ajax. (still an 'advanced' user would be able to check ur javascript, u can't do anything about...