Making images maintain a certain distance

I’m making a thumbnail gallery where I want the image you click on to expand, while the pictures to either side move out of the way. I managed to get it working using this to move the pictures:

“Value” is just a number assigned to each picture, so this does one thing to the pictures to the right of the target, and one thing to the pictures on the left.

// for (q=0; q<total; q++) {
// if (q<this.Value) {
// mcs[q]._x = mcs[q+1]._x-20-mcs[q]._width;
// } else if (q>this.Value) {
// mcs[q]._x = mcs[q-1]._x+20+mcs[q-1]._width;
// }
// }

The problem with this is it doesn’t seem to work if you have more than one picture changing size at once, which is a feature I’d like to have. Also, the pictures tend to squish together at first before settling out to their proper spacings.

Does anyone have a better/more flexible way of doing this?