/**
* Executed when DOM structure is loaded
*/
$(window).ready(function () {
	gaExtender();
});

/**
* Executed when page and all resources have loaded
*/

$(window).load(function () {
	equalizeHeight($('div.steps_holder div.container_half'));
	equalizeHeight($('ul.subjects li'));
	equalizeHeight($('ul.business_units li'));
});


/**
* Equalizes the height of items on the same row.
* 
* @author Ralph Meeuws
* @version dec 2011
*/
function equalizeHeight(arrItems) {
	var intHeight = 0;
	var intHeightMax = 0;
	var intItemsPerRow = 0;
	var intIndexPerParent = -1;
	var elParent = jQuery(arrItems).first().parent();
	var intChildrenCount = jQuery(elParent).children().length;
	var arrItemsOnRow = [];
	var elHiddenParent;

	jQuery(arrItems).each(function (index, elItem) {
		// Reset optional variables:
		elHiddenParent = '';

		// Reset measurement when items belong to a different parent:
		if (jQuery(elParent)[0] != jQuery(jQuery(elItem).parent())[0]) {
			elParent = jQuery(elItem).parent();
			intItemsPerRow = 0;
			intIndexPerParent = -1;
			intChildrenCount = jQuery(elParent).children().length;
		}

		// Calculate how many items fit on a single row:
		if (intItemsPerRow == 0) {
			intItemsPerRow = Math.floor(jQuery(elParent).width() / jQuery(elItem).outerWidth());
		}

		// Count a sub index for items per unique parent:
		intIndexPerParent++;

		// If an item is hidden make it temporarily measurable outside of view:
		if (jQuery(elItem).css('display') == 'none') {
			jQuery(elItem).addClass('measure');
		}

		// Define the maximum height of an item:
		intHeightMax = (jQuery(elItem).height() > intHeightMax) ? jQuery(elItem).height() : intHeightMax;

		// If an item has a temporarily measure class, remove it again:
		if (jQuery(elItem).add(elHiddenParent).hasClass('measure')) jQuery(elItem).add(elHiddenParent).removeClass('measure');

		// Remember which items are on the same row:
		arrItemsOnRow.push(elItem);

		// Apply and reset heights when at the last item in a full row or the last child within a parent:
		if (intIndexPerParent % intItemsPerRow == intItemsPerRow - 1 || intIndexPerParent == intChildrenCount - 1) {
			// Apply the maximum height for the tallest item in a row on all items within that row:
			jQuery(arrItemsOnRow).css('height', intHeightMax);

			// Remember the maximum height for each item in a row:
			jQuery(arrItemsOnRow).data('height', intHeightMax);

			// Reset row measurements:
			intHeightMax = 0;
			arrItemsOnRow = [];
		}
	});
}

/**
* functions to load/reset at an ajax call
*/

function ajaxCall() {
	equalizeHeight($('ul.business_units li'));
}
