/**
 * Initializes functions called on page load.
 *
 * @author Ralph Meeuws <ralph.meeuws{AT}efocus.nl>
 * @since 1.0, dec 2010
 */
window.addEvents({
    'domready': function() {
        init();   
    }
});

function init() {
    if (getElementsByClassName(document, 'ul', 'home_products').length != 0) measureListItemHeights(getElementsByClassName(document, 'ul', 'home_products')[0].getElementsByTagName('li'));    
}

/**
 * Helper function getting elements by classname using native javascript.
 *
 * @author Jonathan Snook, http://www.snook.ca/jonathan
 * @author Add-ons by Robert Nyman, http://www.robertnyman.com
 * @since 1.0, dec 2010
 */
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

/**
 * Measures and sets the equalized height of all list items on each row.
 *
 * @author Ralph Meeuws <ralph.meeuws{AT}efocus.nl>
 * @since 1.0, dec 2010
 */
function measureListItemHeights(arrListItems) {
	var intListItemMaxHeight = 0;
	var intListItemsPerRow = 2;
	
	setHeight = function(intIndex, intLoopCount, arrElements, intHeight) {
		for (var j = 0; j < intLoopCount; j++) {
			arrElements[intIndex - j].style.height = intHeight + 'px';
		}
		intListItemMaxHeight = 0;
	}
	
	for (var intIndex=0; intIndex < arrListItems.length; intIndex++) {
		if (arrListItems[intIndex].clientHeight > intListItemMaxHeight) intListItemMaxHeight = arrListItems[intIndex].clientHeight;
		
		if ((intIndex + 1) % intListItemsPerRow == 0) {
			setHeight(intIndex, intListItemsPerRow, arrListItems, intListItemMaxHeight);
		} else if ((intIndex + 1) == arrListItems.length) {
			var intLastRowItemsCount = arrListItems.length - Math.floor(arrListItems.length / intListItemsPerRow) * intListItemsPerRow;
			setHeight(intIndex, intLastRowItemsCount, arrListItems, intListItemMaxHeight);
		}
	}
}
