Photogallery with differing picture sizes

I want to modify the following example to use pictures of different widths:

http://www.kirupa.com/developer/mx2004/thumbnails.htm

The thumbnail viewer will only correctly space the pictures if the thumbnails are all the same width. The equation just takes the width of a picture and creates and assigns an equally spaced position for each additional picture.

Say I had 5 pictures with the follow widths (in pixels):

100, 60, 120, 30, 80.

How can I modify the code to correctly space this pictures (with say 5 pixels space in between each picture)?

Thanks to anyone who can help!