function mousepos(e) {
	if (!e) e = window.event;
	var body = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ? window.document.documentElement : window.document.body;
	return {
		top: e.pageY ? e.pageY : e.clientY + body.scrollTop - body.clientTop,
		left: e.pageX ? e.pageX : e.clientX + body.scrollLeft  - body.clientLeft
	};
}

function movepicov(e) {
	var p = mousepos(e);
	var pad = 15;
	var cs = clientsize();
	var sx = 360;
	var sy = 210;
	var mpadx = 10;
	var mpady = 10;
	var goesright = body2client("x", p["left"]) < cs["width"] / 2;
	var goesdown = body2client("<", p["top"]) < cs["height"] / 2;

	var tx = p["left"] + (goesright ? mpadx: - mpadx - sx);
	if (body2client("x", tx) < pad) {
		tx = client2body("x", pad);
	}
	else if (body2client("x", tx) + sx > cs["width"] - pad * 2) {
		tx = client2body("x", cs["width"] - pad * 2 - sx);
	}
	document.getElementById("picov").style.left = tx + "px";

	var ty = p["top"] + (goesdown ? mpady: - mpady - sy);
	if (body2client("y", ty) < pad) {
		ty = client2body("y", pad);
	}
	/*else if (body2client("y", ty) + sy > cs["height"] - pad) {
		ty = client2body("y", cs["height"] - pad - sy);
	}*/
	document.getElementById("picov").style.top = ty + "px";


	if (shouldbevisible) {
		if (tx < p["left"] && p["left"] < tx + sx
		 && ty < p["top"] && p["top"] < ty + sy) {
			document.getElementById("picov").style.display = "none";
		}
		else {
			document.getElementById("picov").style.display = "block";
		}
	}
}

window.document.onmousemove = movepicov;
var shouldbevisible = false;

function mov(id) {
	shouldbevisible = true;
	document.getElementById("picov").style.display = "block";
	var ht = "<img src=\"galerie/350x200/" + id + ".jpeg\" alt=\"" + id + "\" />";
	if(document.getElementById("picov").innerHTML != ht) document.getElementById("picov").innerHTML = ht;
}

function mou(id) {
	shouldbevisible = false;
	document.getElementById("picov").style.display = "none";
}


function client2body(axis, val) { return body2client(axis, val, true); }
function body2client(axis, val, reverse) {
	return document.all
		? (val - (axis == "x" ? document.body.scrollLeft : document.body.scrollTop) * (reverse ? -1 : 1))
		: (val - (axis == "x" ? window.pageXOffset : window.pageYOffset) * (reverse ? -1 : 1));
}

function clientsize() {
	return {
		width: document.all ? document.body.clientWidth : window.innerWidth,
		height: document.all ? document.body.clientHeight : window.innerHeight
	};
}
