Flip and flip back after all have been flipping flipped?

I have a bunch of plain squares I would like transition to an image when rolled over. When the user flips **[COLOR=Black]all[/COLOR] **of them over, I would like to have them all flip back to there original positions. As a bonus the next time the user rolled over them I would like a different picture to load in each square.

I know how to make the images flip and flip back, but i dont know how to have them wait until the last one is turned over then flip. Can some one help?