// we overwrite the onload handler and should preserve any that has already been there
if (typeof window.onload == "function") {
	var preservedFunction = window.onload;
}
window.onload = function () {
	var myLinks = document.getElementsByTagName("a");
	for (var i = 0; i < myLinks.length; i ++) {
		if (myLinks[i].className == "cs6ZoomLink") {
			myLinks[i].onclick = zoompic;
			myLinks[i].onkeypress = zoompic;
		}
	}
	if (preservedFunction) {
		preservedFunction();
	}
};
function zoompic (myEvent) {
	myEvent = js6CrossBrowser.Event(myEvent);
	myEvent.stopPropagation();
	myEvent.preventDefault();
	if (myEvent.target.tagName.toLowerCase() == "img") {
		var srcImage = myEvent.target;
		var srcLink = myEvent.target.parentNode;
	} else if (myEvent.target.tagName.toLowerCase() == "a") {
		var srcLink = myEvent.target;
		var srcImage = myEvent.target.firstChild;
	}
	var windowOptions = "resizable=1,width=200,height=200,top=0,left=0";
	var myPicSize = srcLink.getAttribute("bigSize");
	var myWidth = myPicSize.slice(0,myPicSize.indexOf("x"));
	var myHeight = myPicSize.slice(myPicSize.indexOf("x") + 1);
	if (myWidth > screen.availWidth || myHeight > screen.availHeight) {
		windowOptions += ",scrollbars=yes";
	}
	// firefox likes to have a src for the window (otherwise messes up the scrolling),
	// safari doesn't like that at all and the other browsers don't really care...
	// said it before and will say it again: go figure...
	var mySrc = navigator.userAgent.match(/Firefox/) ? "about:blank" : "";
	var myWindow = window.open(mySrc,srcLink.getAttribute("target"),windowOptions);
	newDoc = myWindow.document.open();
	// Safari doesn't return the document object - gotta get it ourselves
	if (!newDoc) newDoc = myWindow.document;
	newDoc.writeln("<html>");
	newDoc.writeln("<head>");
	newDoc.writeln("<script language=\"javascript\">"+fitSize+"</script>");
	newDoc.writeln("<title>" + srcImage.getAttribute("title") + "</title>");
	newDoc.writeln("</head>");
	newDoc.writeln("<body marginwidth=\"0\" marginheight=\"0\" leftmargin=\"0\" topmargin=\"0\">");
	newDoc.writeln("<a href=\"javascript: window.close()\"><img src=\""+srcLink.getAttribute("href")+"\" border=\"0\" id=\"id6ZoomPic\" onload=\"fitSize()\" width=\""+myWidth+"\" height=\""+myHeight+"\" /></a>");
	newDoc.writeln("</body>");
	newDoc.writeln("</html>");
	newDoc.close();
	myWindow.focus();
	return false;
}
function fitSize() {
	// downscale the image if requested (set variable zoomPicFitSize to true in opening document)
	if (window.opener.zoomPicFitSize) {
		var myAvailHeight = screen.availHeight;
		if (window.outerHeight) {
			// we want to know the height available for the windows content(!)
			myAvailHeight -= (window.outerHeight - window.innerHeight);
		} else {
			// IE doesn't know innerHeight and outerHeight (quelle surprise) - so lets make an educated guess...
			myAvailHeight -= 30;
		}
		if (document.images[0].width > screen.availWidth || document.images[0].height > myAvailHeight) {
			var myWidth = document.images[0].width;
			var myHeight = document.images[0].height;
			var factorX = document.images[0].width / screen.availWidth;
			var factorY = document.images[0].height / myAvailHeight;
			var factor = factorX > factorY ? factorX : factorY;
			document.images[0].height = parseInt(myHeight / factor);
			document.images[0].width = parseInt(myWidth / factor);
			document.title += " (scaled from "+myWidth+" x "+myHeight+")";
		}
	}
	var targetWidth = (document.images[0].width < screen.availWidth) ? document.images[0].width : screen.availWidth;
	var targetHeight = (document.images[0].height < screen.availHeight) ? document.images[0].height : screen.availHeight;
	targetHeight += 5;
	window.resizeTo(targetWidth, targetHeight);
	if (document.body.clientWidth < targetWidth && targetWidth < screen.availWidth) {
		window.resizeBy(targetWidth - document.body.clientWidth,0);
	}
	if (document.body.clientHeight < targetHeight && targetHeight < screen.availHeight) {
		window.resizeBy(0,targetHeight - document.body.clientHeight);
		window.resizeBy(targetWidth - document.body.clientWidth,0);
	}
}