var marginHeight = 364;
var minimalContainerHeight = 0;
var mainContainer = null;

function resizeTable() {
	var docHeight = getWindowHeight();
	if(mainContainer == null) {
		mainContainer = document.getElementById('mainContainer');
		minimalContainerHeight = mainContainer.offsetHeight;
		marginHeight = document.getElementById('mainTable').offsetHeight - minimalContainerHeight;
	}
	var newHeight = (docHeight - marginHeight - 24);
	if(newHeight < minimalContainerHeight) newHeight = minimalContainerHeight;
	mainContainer.style.height = newHeight + "px";
}

function getWindowHeight() {
	if(window.innerHeight) {
		return window.innerHeight
	}
	else {
		return document.body.clientHeight;
	}
}

function initializeMenu() {
	document.submenus = new Object();
	document.submenuNames = new Array(arguments.length-1);
	for(var i = 0; i < arguments.length-1; i++) {
		var sm = document.getElementById('sub_' + arguments[i]);
		document.submenus[arguments[i]] = sm;
		document.submenuNames[i] = arguments[i];
		sm.style.overflow = 'hidden';
		sm.initialHeight = sm.offsetHeight;
	}
	// hide all except one
	var keepVisible = arguments[arguments.length-1];
	if(keepVisible == 0) keepVisible = 2;
	else keepVisible--;
	for(var j = 0; j < document.submenuNames.length; j++) {
		if(keepVisible != j) document.submenus[document.submenuNames[j]].style.height = '0px';
		else document.visibleSubmenu = document.submenus[document.submenuNames[j]];
	}
}

function toggleMenu(name) {
	var s = document.submenus[name];

	// make sure no timeout is running
	if(document.sshowTimeout) {
		window.clearTimeout(document.sshowTimeout);
		document.sshowTimeout = null;
	}

	if(!document.activeCloseSubmenu && !document.activeOpenSubmenu) {
		if(s.offsetHeight > 0) {
			document.activeCloseSubmenu = s;
			document.submenuCloseIntv = window.setInterval(decreasemenuheight, 25);
			// if a menu is actively closed, no other menu should be visible
			document.visibleSubmenu = null;
		}
		else {
			document.activeOpenSubmenu = s;
			document.submenuOpenIntv = window.setInterval(increasemenuheight, 25);
			// if a menu is opened, make sure any other open menu is closed (can only be one)
			if(document.visibleSubmenu) {
				document.activeCloseSubmenu = document.visibleSubmenu;
				document.submenuCloseIntv = window.setInterval(decreasemenuheight, 25);
			}
			document.visibleSubmenu = s;
		}
	}
}
function decreasemenuheight() {
	if(document.activeCloseSubmenu) {
		var s = document.activeCloseSubmenu;
		if(s.offsetHeight > 0) {
			var d = Math.min(s.offsetHeight, 50);
			s.style.height = (s.offsetHeight - d) + 'px';
		}
		else {
			clearInterval(document.submenuCloseIntv);
			document.activeCloseSubmenu = null;
		}
	}
	else {
		clearInterval(document.submenuCloseIntv);
	}
}
function increasemenuheight() {
	if(document.activeOpenSubmenu) {
		var s = document.activeOpenSubmenu;
		if(s.offsetHeight < s.initialHeight) {
			var d = Math.min(s.initialHeight-s.offsetHeight, 50);
			s.style.height = (s.offsetHeight + d) + 'px';
		}
		else {
			clearInterval(document.submenuOpenIntv);
			document.activeOpenSubmenu = null;
		}
	}
	else {
		clearInterval(document.submenuOpenIntv);
	}
}
function startSShow(name) {
	var s = document.submenus[name];
	if(document.visibleSubmenu == s) return;

	// else: start an intv to show the submenu
	document.sshowTimeout = window.setTimeout("toggleMenu('" + name + "')", 500);
}
function stopSShow(name) {
	// var s = document.submenus[name];
	if(document.sshowTimeout) {
		window.clearTimeout(document.sshowTimeout);
		document.sshowTimeout = null;
	}
}
