to avoid any issues with your posted code, it's best to wrap your code with [code] tags (one beginning, one ending)
in case you weren't already aware, there's a mod available that does this in a slick js fashion (but I can understand if you prefer your method, just didn't know if you weren't aware). The module is called Dynamic Images, it's free. In the default package, it doesn't offer a pop-up for detailed images, but this how-to extends Dynamic Images further:
Extend Dynamic Images module to use Lytebox for displaying larger images
Regardless, congrats on your modification, it's always good to see someone else getting their hands dirty