// 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 = js6_Action_ZoomPic;
			myLinks[i].onkeypress = js6_Action_ZoomPic;
		}
	}
	if (preservedFunction) {
		preservedFunction();
	}
};
function js6_Action_ZoomPic (
	myEvent)
{
	myEvent = js6_Sys_CrossBrowser.Event(myEvent);
	myEvent.stopPropagation();
	myEvent.preventDefault();

	var aAttribs = { 1: 'title', 2: 'alt', 3: 'src'};
	var sTitle = '';
	var srcLink = null;
	if (myEvent.target.tagName.toLowerCase() == 'img') {
		for (var ii in aAttribs) {
			sTitle = myEvent.target.getAttribute(aAttribs[ii]);
			if ('' != sTitle) break;
		}
		srcLink = myEvent.target.parentNode;
	} else if (myEvent.target.tagName.toLowerCase() == 'a') {
		if ((myEvent.target.firstChild.tagName) && (myEvent.target.firstChild.tagName.toLowerCase() == 'img')) {
			for (var ii in aAttribs) {
				sTitle = myEvent.target.firstChild.getAttribute(aAttribs[ii]);
				if ('' != sTitle) break;
			}
		} else {
			if (3 == myEvent.target.firstChild.nodeType) {
				sTitle = myEvent.target.firstChild.nodeValue;
			}
		}
		srcLink = myEvent.target;
	}
	if ((!sTitle) || ('' == sTitle)) {
		sTitle = srcLink.getAttribute('href');
	}

	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">'+js6_Action_ZoomPicFitSize+'</script>');
	newDoc.writeln('<title>' + sTitle + '</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="js6_Action_ZoomPicFitSize()" width="'+myWidth+'" height="'+myHeight+'" /></a>');
	newDoc.writeln('</body>');
	newDoc.writeln('</html>');
	newDoc.close();
	myWindow.focus();
	return false;
}
function js6_Action_ZoomPicFitSize ()
{
	// downscale the image if requested (set variable js6_Action_ZoomPicFitValue to true in opening document)
	if (window.opener.js6_Action_ZoomPicFitValue) {
		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);
	}
}