Somebody knows the AS o the code to detect "how many elements are in a folder"?

I made a great photo gallery with a two buttons next and prev, and a preloaded photo. It works fine but i want to optimisize this script...

I fixe the number of photos present in my folder manualy for exemple 15.
When pushing next button my var increase +1 , and when my var=15 then i put it in var=1 to start again from the begining.

I would like my script detect automaticly how many photos are in my folder, in my case: 15...
I hope it was clear...
Help please