function Tooltip()
{
	var prevTooltip;
	
	function getWindowWidth() {
		if (window.innerWidth) {
			return window.innerWidth;
		}
		return document.body.clientWidth;
	}
	function mouseX(e) {
		if (e.pageX) {
			return e.pageX;
		}
		return e.clientX + (document.documentElement.scrollLeft ? 
			document.documentElement.scrollLeft : document.body.scrollLeft);
	}
	function mouseY(e) {
		if (e.pageY) {
			return e.pageY;
		}
		return e.clientY + (document.documentElement.scrollTop ?
			document.documentElement.scrollTop : document.body.scrollTop);
	}

	Tooltip.prototype.update = function (e, o) {
		var windowWidth = getWindowWidth();
		o = document.getElementById(o);
		
		if (prevTooltip && prevTooltip != o) {
			prevTooltip.style.visibility = 'hidden';
		}
		if (o.style.visibility == 'visible') {
			o.style.visibility = 'hidden';
		} else {
			var ew;
			
			if (o.offsetWidth) {
				ew = o.offsetWidth;
			} else if (o.clip.Width) {
				ew = o.clip.width;
			}
			
			var y = mouseY(e) + 16;
			var x = mouseX(e) - (ew / 4);
			
			if (x < 2) {
				x = 2;
			} else if (x + ew > windowWidth) {
				x = windowWidth - ew - 4;
			}
			
			o.style.left = x + 'px';
			o.style.top = y + 'px';
			o.style.visibility = 'visible';
			
			prevTooltip = o;
		}
	}
}

function DocumentPanel() {
	var previousPanel;
	
	function getWindowWidth() {
		if (window.innerWidth) {
			return window.innerWidth;
		}
		return document.body.clientWidth;
	}
	function getWindowHeight() {
		if (window.innerHeight) {
			return window.innerHeight;
		}
		return document.body.clientHeight;
	}
	function mouseX(e) {
		if (e.pageX) {
			return e.pageX;
		}
		return e.clientX + (document.documentElement.scrollLeft ? 
			document.documentElement.scrollLeft : document.body.scrollLeft);
	}
	function mouseY(e) {
		if (e.pageY) {
			return e.pageY;
		}
		return e.clientY + (document.documentElement.scrollTop ?
			document.documentElement.scrollTop : document.body.scrollTop);
	}
	
	DocumentPanel.prototype.display = function (e, o) {
		var windowWidth = getWindowWidth();
		var windowHeight = getWindowHeight();
		
		o = document.getElementById(o);
		
		if (previousPanel && previousPanel != o) {
			previousPanel.style.visibility = 'hidden';
		}
		if (o.style.visibility == 'visible') { // 
			o.style.visibility = 'hidden';
		} else {
			var ew, eh;
			
			if (o.offsetWidth) {
				ew = o.offsetWidth;
			} else if (o.clip.Width) {
				ew = o.clip.width;
			}
			
			if (o.offsetHeight) {
				eh = o.offsetHeight;
			} else if (o.clip.Height) {
				eh = o.clip.height;
			}
			var y = mouseY(e) + 16;
			var x = mouseX(e) - (ew / 4);
			
			if (x < 2) {
				x = 2;
			} else if (x + ew > windowWidth) {
				x = windowWidth - ew - 4;
			}
			
			if (y + eh > windowHeight) {
				y = windowHeight - eh - 4;
			}
			
			o.style.left = x + 'px';
			o.style.top = y + 'px';
			o.style.visibility = 'visible';
			
			previousPanel = o;
		}
	}
	
	DocumentPanel.prototype.close = function() {
		if (previousPanel)
			previousPanel.style.visibility = 'hidden';
	}
}

function updateFootnote(id) {
	var element = document.getElementById(id);
	var footnote = document.getElementById('footnote');
	
	footnote.innerHTML = element.innerHTML;
}

var tooltip = new Tooltip();
var documentPanel = new DocumentPanel();
var commentPanel = new DocumentPanel();