Hi, i m serious freelancer with 100% completion rate and i believe i can help you.
First there is no way from javascript to automatically know which is the biggest gallery file. So you would need to have a variable inside a script like var biggestGalleryNumber = 30. And then when you add for example 3 more files like in your description (e.g. "[login to view URL]", "[login to view URL]", "[login to view URL]", etc.).) , you need to update that number to 33. Other then that everything else will work exactly like you described. This is really easy to do, and i would provide you a demo, before accepting the project. As i said i m serious freelancer and i've never took a single dollar unless my solution was perfect.
Thanks, Milan