window.onload   = function() { document.isReady = true  };
window.onunload = function() { document.isReady = false };

function windowOffset() {
	var x = 0, y = 0;

	if (typeof window.pageXOffset == 'number') { // Netscape compliant
		x = window.pageXOffset;
		y = window.pageYOffset;
	} else if (document.body && (
	           document.body.scrollLeft ||
	           document.body.scrollTop
	)) { // DOM compliant
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	} else if (document.documentElement && (
	           document.documentElement.scrollLeft ||
	           document.documentElement.scrollTop
	)) { // IE6 in 'standards compliant mode'
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}

	return { x: x, y: y };
}

function windowDimensions() {
	var w = 0, h = 0;

	if (typeof window.innerWidth == 'number') { // Non-IE
		w = window.innerWidth;
		h = window.innerHeight;
	} else if (document.documentElement && (
	           document.documentElement.clientWidth ||
	           document.documentElement.clientHeight
	)) { // IE 6+ in 'standards compliant mode'
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	} else if (document.body && (
	           document.body.clientWidth ||
	           document.body.clientHeight
	)) { // IE 4 compatible
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}

	return { x: w, y: h };
}

var cX = 0, cY = 0;
document.onmousemove = function(e) {
	e = e || window.event; // IE hack
	cX = e.clientX; cY = e.clientY;
};

function assignPosition() {
	var size = windowDimensions(); offset = windowOffset();

	var e = $('preview');
	if (cX < size.x/2) e.style.left = (cX + offset.x + 5                ) + 'px'; // left
	else               e.style.left = (cX + offset.x - 5 - e.getWidth() ) + 'px'; // right
	if (cY < size.y/2) e.style.top  = (cY + offset.y + 5                ) + 'px'; // top
	else               e.style.top  = (cY + offset.y - 5 - e.getHeight()) + 'px'; // bottom
}
