With CSS, you'd set a % width on your photo boxes, based on number per row. So if 5 in a row, width is something less than 20% (room for the margin).
Then initially everyone gets a margin. Let's say left margin.
With CSS you would count the items, and every nth item would NOT have the margin. So if there were 3 in a row, the first and every third after it, being the one on the left, would not have the left margin (like, in a ul, li:nth-child(3n+1).
So for example
$('#gallery li:nth-child(3n+1)').css(marginLeft, '0');
You could go further: calculate the width of the container with jQuery, divide it by the number of items per row, remove from that total however much total margin you want between elements, set that as the new element width, set the margins to your calculated margin width, and if you use percents you could have your elements growing and shrinking a bit to ensure they are always absolutely filling all the space, if your container is flexible to the viewport.