﻿
var products = new Array();
if (document.getElementById("div_SearchResults")) {
    var products2 = document.getElementById("div_SearchResults").getElementsByTagName("DIV");
    if (products2) {
        for (i = 0; i < products2.length; i++) {
            if (products2[i].className == "div_Product" || products2[i].className == "div_Product div_Last") {
                products.push(products2[i]);
            }
        }
    } 
}
    var thisRowAnchors = new Array();
    var thisRowImages = new Array();
    var thisRowItems = 0;
    var itemsPerRow = 4;
    var maxHeight = 0;
    
    function lineUpProducts() {
        if (!imagesLoaded()) return;
        for (i=0; i<products.length; i++) {
            if (products[i].className == "div_Product" || products[i].className == "div_Product div_Last") {
                var anchor = products[i].getElementsByTagName("A")[0];
                var image = anchor.getElementsByTagName("IMG")[0];
                thisRowAnchors[thisRowItems] = anchor;
                thisRowImages[thisRowItems] = image;
                thisRowItems++;
                if (image.offsetHeight > maxHeight) {
                    maxHeight = image.offsetHeight;
                }
                if (thisRowAnchors.length == itemsPerRow || i==products.length-1) {
                    for (n=0; n<thisRowAnchors.length; n++) {
                        if (thisRowAnchors[n]) {
                            thisRowAnchors[n].style.height = maxHeight + "px";
                            thisRowImages[n].style.marginTop = (maxHeight - thisRowImages[n].offsetHeight) / 2 + "px";
                        }
                    }
                    thisRowAnchors.length = 0;
                    thisRowImages.length = 0;
                    thisRowItems = 0;
                    maxHeight = 0;
                }
            }
        }
    }
    
    imagesTimeout = 0;
    runTimes = 0;
    runTimesMax = 20;
    
    function imagesLoaded() {
        if (runTimes >= runTimesMax) return true;
        runTimes++;
        window.clearTimeout(imagesTimeout);
        if (document.getElementById("div_SearchResults")) {
            images = document.getElementById("div_SearchResults").getElementsByTagName("IMG");
            for (var i = 0; i < images.length; i++) {
                var img = images[i];
                if (img) {
                    if (!img.complete) {
                        imagesTimeout = window.setTimeout(lineUpProducts, 500);
                        return false;
                    }
                }
            }
        }
        return true;
    }
