/*
 * 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);
	
    // Resize the contentframe
	resizeBoxByID('contentframe-4', 42);
	blockHeight = 54;
	addBoxes = 1;
	resizeProductFilter();
    
    moveBranches();
}

/*
    This functions moves the branches layer around 
    so that it fits into the grid.
    We need to consider 2 special cases because we
    have more than one grid.
*/
function moveBranches() { 
    var branches = document.getElementById('branches');
    var obj = document.getElementById('contentframe');
    var submenu = document.getElementById('submenu');
    
    // case 1
        // fallback for special sites that rely on a different grid 
        // that don't have the contentframe container
    if(!obj) {
        var obj = document.getElementById('contentframe-4');
    }
    // case 2
        // move the branches 40px up on the contactmap
    if(document.getElementById('contentframe-6')) { 
        var special = -40;
    }
    else {
        var special = 0;
    }
    
    if(obj) {
        var object = branches.getElementsByTagName('div')[0];
        var newheight = parseInt(obj.style.height.replace(/px,*\)*/g,""));
        if(!submenu) {
            object.style.top = newheight + 124 + special + 'px';
        }
        else if(submenu.style.height == '214px') {
            object.style.top = newheight + 124 + special + 'px';
        }
        else {
            object.style.top = newheight + 232 + 'px';
        }
    }
}

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";
	}
	
}
