/* Cross-browser function to add events to objects */

function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
   }
  else
    if (obj.attachEvent){
      var r = obj.attachEvent("on"+evType, fn);
      return r;
     }
    else
      alert("Handler could not be attached");
}

Adjust2Grid = function() {
  var curH,lastH,maxW, bassofia;
  var MyLis = document.getElementById("new_products").getElementsByTagName("LI");
  if (MyLis.length == 0) return;
  /* First, uniformize widths */
/*
  maxW  = MyLis[0].clientWidth;
  for (var i=1; i<MyLis.length; i++) {
    if (MyLis[i].clientWidth > maxW)
      maxW = MyLis[i].clientWidth;
   }
  if (maxW > MyLis[0].clientWidth)
    for (var i=0; i<MyLis.length; i++)
      MyLis[i].style.width = maxW  + 'px';
*/
  /* Now uniformize heights in every row - we check from 2nd element on */
    /* Find out what the difference between style heigh and clientHeight is */
  var height1 = MyLis[0].clientHeight;
  MyLis[0].style.height = height1 + 'px';
  bassofia = MyLis[0].clientHeight - height1;
/*
  alert('Parametro de ajuste: '+bassofia);
*/
  for (var i=1; i<MyLis.length; i++) {
    curH  = MyLis[i].clientHeight;
    lastH = MyLis[i-1].clientHeight;
/*
    alert((i+1)+': '+curH+'; '+i+': '+lastH);
*/
    if ((MyLis[i].offsetTop == MyLis[i-1].offsetTop) &&
        (curH != lastH)) {
      if (curH < lastH) {
/*
        alert('Ajuste('+(i+1)+'): '+curH+' -> '+lastH);
*/
        MyLis[i].style.height = (lastH-bassofia) + 'px';
       }
      else {
        var j = i-1;
        while ((j>=0) && (MyLis[j].offsetTop == MyLis[i].offsetTop)) {
/*
          alert('Ajuste('+(j+1)+'): '+MyLis[j].clientHeight+' -> '+curH);
*/
          MyLis[j].style.height = (curH-bassofia) + 'px';
          j--;
         }
       }
     }
   }
}

var DOM;
DOM = (document.getElementById) ? 1 : 0;
if (DOM==1) {
  addEvent(window, 'load', Adjust2Grid, false);
 }
