var floatingWindow = document.getElementById("floating-window");

floatingWindow.onmousedown = function(event) {
	event.preventDefault();

	var shiftX = event.clientX - floatingWindow.getBoundingClientRect().left;
	var shiftY = event.clientY - floatingWindow.getBoundingClientRect().top;

	document.addEventListener("mousemove", onMouseMove);
	document.addEventListener("mouseup", onMouseUp);

	function onMouseMove(event) {
		var newLeft = event.clientX - shiftX;
		var newTop = event.clientY - shiftY;

		if (newLeft < 0) {
			newLeft = 0;
		}
		if (newTop < 0) {
			newTop = 0;
		}
		if (newLeft + floatingWindow.offsetWidth > document.documentElement.clientWidth) {
			newLeft = document.documentElement.clientWidth - floatingWindow.offsetWidth;
		}
		if (newTop + floatingWindow.offsetHeight > document.documentElement.clientHeight) {
			newTop = document.documentElement.clientHeight - floatingWindow.offsetHeight;
		}

		floatingWindow.style.left = newLeft + "px";
		floatingWindow.style.top = newTop + "px";
	}

	function onMouseUp() {
		document.removeEventListener("mousemove", onMouseMove);
		document.removeEventListener("mouseup", onMouseUp);
	}
};