Javascript to php

Ive got a simple image gallery written in javascript with previous an next button, no thumnails. And now I want a similar gallary but whith php insted. There I a few thing I dont know how to do it in php.

I attach the old file(javascript) with coments in the code.

It would be great if someone could help me with this.