Event.observe(window, 'resize', FG_fixfloats, false);
Event.observe(window, 'load', FG_fixfloats, false);

function FG_fixfloats() {
  lastParent = 0;
  galleryitems = document.getElementsByClassName("galleryitem");
  for (i=0; i<galleryitems.length; i++) {
    galleryitem = galleryitems[i];
    if(galleryitem.parentNode != lastParent) {
      lastTop = galleryitem.offsetTop;
      lastLeft = 0;
      rowHeight = 0;
      rowStart = i;
      lastParent = galleryitem.parentNode;
    }
    galleryitem.style.height = "auto";
    galleryitem.style.clear="none";
    if(galleryitem.offsetLeft <= lastLeft) {
      galleryitem.style.clear="left";
      rowHeight = galleryitem.offsetTop - lastTop - ( parseInt(Element.getStyle(galleryitem, "margin-top"))+parseInt(Element.getStyle(galleryitem, "margin-bottom")) );
      for(j=rowStart;j<i;j++) {
        if (galleryitems[j].offsetTop==lastTop)
          galleryitems[j].style.height = rowHeight+"px";
        else
          galleryitems[j].style.height = (rowHeight-(galleryitems[j].offsetTop-lastTop))+"px"
      }
      lastTop = galleryitem.offsetTop;
      rowStart = i;
    }
    lastLeft = galleryitem.offsetLeft;
  }
}