var maxZoomFactor = 1.0;

function mouseMove(event) {
var divObj = document.getElementById(this.alt);
var imgObj = divObj.firstChild;
var aspectRatio = imgObj.naturalWidth / imgObj.naturalHeight;
var x = 0, y = 0, maxWidth = 0, maxHeight = 0, zoomFaktor = 0;
// check which side of the cursor has more space left
if(event.pageX < (window.innerWidth / 2)) {
x = event.pageX + 10;
maxWidth = window.innerWidth - (event.pageX + 50);
} else {
x = event.pageX - (5 + divObj.offsetWidth); // show large image left from cursor
maxWidth = event.pageX - 20;
}
// zoom image to max size, so it still fits in the window
maxHeight = window.innerHeight - 20;
zoomFaktor = Math.min(maxZoomFactor, maxHeight / imgObj.naturalHeight, maxWidth / imgObj.naturalWidth);
imgObj.width = imgObj.naturalWidth * zoomFaktor;
imgObj.height = imgObj.naturalHeight * zoomFaktor;
// if the zoomed image still fits on the left side of the cursor, put it there
if(event.pageX - (5 + divObj.offsetWidth) >= 0) {
x = event.pageX - (5 + divObj.offsetWidth); // show large image left from cursor
}
y = event.pageY - (divObj.offsetHeight / 2); // show large image centered beside the cursor
// don't let it slide out the top or bottom of the window
y = Math.min(y, window.pageYOffset + window.innerHeight - divObj.offsetHeight - 5);
y = Math.max(y, window.pageYOffset + 5);

divObj.style.left = x + 'px';
divObj.style.top = y + 'px';
divObj.style.visibility = "visible";
}

function mouseOut(event) {
var divObj = document.getElementById(this.alt);
divObj.style.visibility = "hidden";
}

function addImage(smallImgObj) {
var smallSrc = smallImgObj.src;
// do not re-add images that already have an alt tag starting with 'largeDiv'
if((smallSrc.lastIndexOf("/thumbs/") > 0) && smallImgObj.alt.indexOf("largeDiv") != 0)
{
	//alert(smallSrc.lastIndexOf("/plugins/galerie/"));
	var largeSrc = smallSrc.replace("/thumbs/", "/view/");
	var startpos = largeSrc.lastIndexOf("/plugins/galerie/galerien/");
	largeSrc = largeSrc.replace("/plugins/galerie/galerien/", "/");
	largeSrc = "http://www.maczocker.de/khmweb" + largeSrc.substr(startpos);//alert(largeSrc);
	smallImgObj.alt = "largeDiv:" + largeSrc;
	var largeDivObj = document.createElement('div');
	largeDivObj.innerHTML = "<div id='largeDiv:" + largeSrc + "' class='tip'><img src='" + largeSrc + "' /></div>";
	document.body.appendChild(largeDivObj);
	if (document.addEventListener) {
		smallImgObj.addEventListener('mouseover', mouseMove, true);
		smallImgObj.addEventListener('mousemove', mouseMove, true);
		smallImgObj.addEventListener('mouseout', mouseOut, true);
	}
	else if (document.attachEvent) {
		smallImgObj.attachEvent('mouseover', mouseMove);
		smallImgObj.attachEvent('mousemove', mouseMove);
		smallImgObj.attachEvent('mouseout', mouseOut);
	}
	else
	{
		smallImgObj.mouseover;
		smallImgObj.mousemove;
		smallImgObj.mouseout;
	}
}
}

function addAllImages() {
for (var i = 0; i < document.images.length; i++) {
addImage(document.images[i], i);
}
}

