/*
 * Function to addEvents to certian objects
 * 
 * @param object		Object to add the event to
 * @param string		Typename to add
 * @param function		function to add as event
 * 
 * @return bool			Result of the attach call
 */
function addEvent(obj, evType, fn) {
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, false);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on" + evType, fn);
		return r;
	} else {
		return false;
	}
}

var blockHeight = 108;
var addBoxes = 0;
addEvent(window, 'load', resizeBoxes);

function resizeBoxes() {
	resizeAddress();

	resizeContent();	
	resizeBoxByID('columnframe', 2);
	resizeBoxByID('submenu', 2, 108, 214);
	
	blockHeight = 54;
	addBoxes = 1;
	resizeProductFilter();
}

function resizeProductFilter() {
	resizeBoxByID('productfilter', 42);
}

function resizeContent() {
	var obj = document.getElementById('contentframe');
	if (obj && obj.parentNode.parentNode.id
		&& obj.parentNode.parentNode.id == 'contentframe-6') {
		resizeBoxByID('contentframe', 2);
	} else {
		resizeBoxByID('contentframe', 42);
	}
}

function resizeAddress() {
	var menuObj = document.getElementById('menuframe_menu');
	var addressObj = document.getElementById('menuframe_address');
	if (menuObj && addressObj) {
		var current_height = menuObj.offsetHeight + addressObj.offsetHeight;
		var new_height = (Math.floor(current_height / blockHeight) + 1) * blockHeight - menuObj.offsetHeight - 19 + "px";
		addressObj.style.height = new_height;
	}
}

/*
 * Main function for resizing boxes
 *
 * @param	string		Boxid to resize.
 * @param	int			Sum of paddingTop + paddingBottom.
 * @param	int			Threshold where the object beyond this height should be resized
 * @param	int			minHeight of the object
 * @return	void
 */
function resizeBoxByID(id, diff, threshold, minHeight) {
	var obj = document.getElementById(id);
	if (obj && obj.innerHTML != "" && obj.innerHTML != "<!--TYPO3SEARCH_begin--><!--TYPO3SEARCH_end-->") {
		if (threshold != "undefined" && obj.offsetHeight <= threshold) {
			obj.style.height = minHeight + "px";
		} else {
			var division = obj.offsetHeight / blockHeight;
			var boxes = Math.ceil(division) + addBoxes;
			obj.style.height = boxes * blockHeight - diff + "px";
		}
	} else if (obj) {
		obj.style.display = "none";
	}
}