Going to specific images on a stack - advice needed

Hi,

I’m currently coding an image stack, (in quick words, when the user click on the top image, this one goes and hides at the end of the stack and displays the one under)

I have the project to add a menu to be able to jump to specific images (the position could be stored on the id of the link or in xml)

Link 1 would take me to image 3
Link 2 would take me to image 7

My question, (let’s take an example) if I want to access image #5 (right in the middle of a stack of 10 pics) how should I code it the proper way to make disappear the pictures before (if there are some), and place them after the ones currently under the image # 5

Example :

image3
image4
image5 > the one to bring on top
image…
image9
image1
image2

or

image6
image7
image8
image9
image…
image5

I’m having problem trying to figure out how to code this clean.

Thanks a lOT