var marginElementId = 'Viewport';
var additionalMargin = 0;

function addResizeEvent(func) {
	var oldonresize = window.onresize;
	if (typeof window.onresize != 'function') {
		window.onresize = func;
	} else {
		window.onresize = function() {
			if (oldonresize) oldonresize();
			func();
		}
	}
}
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) oldonload();
			func();
		}
	}
}

addLoadEvent(updateTopMargin);
addResizeEvent(updateTopMargin);

function updateTopMargin() {
	var elem = window.document.getElementById(marginElementId);
	var trueBody = document.compatMode && document.compatMode!='BackCompat' ? document.documentElement : document.body;
	var windowHeight = window.innerHeight ? window.innerHeight : trueBody.clientHeight;
	var elemHeight = window.document.getElementById(marginElementId).clientHeight;
	if (elem) elem.style.marginTop = (Math.ceil((windowHeight-elemHeight)/2) + additionalMargin).toString() + 'px';
}