Thank you very much for the solution. It works great. Could you please help me to figure out how to use it in case where image comes from CSS? I tried to use an id name but it didn't work..
I was also interested how to make this script load random image instead of rotation every time the page is visited?