Thumbnail gallery with varying width thumbnails


I have my thumbnails loading in a scrolling grid.
Problem is my thumbnails have varying widths (vertical and horizontal images)
and can’t figure out a way to space it without huge gaps.

Any thoughts???

my spacing code is below:

tlistener.onLoadInit = function(target_mc) {
target_mc._x = (k%5)*(210);
target_mc._y =Math.floor(k/5)*130;

any help is appreciated