var reset, distance;

function windowOpen(imageLocation, imageWidth, imageHeight)
{

if (document.all || document.layers) {
   w = screen.availWidth;
   h = screen.availHeight;
}

// For left windows
// var leftPos = 400-(imageWidth/2), topPos = 300-(imageHeight/2);
 
 var leftPos = (w-imageWidth)/2, topPos = (h-imageHeight)/2;

displayWindow=window.open("","screenshotWindow","toolbar=no,width=10,height=10,top="+topPos+",left="+leftPos+",directories=no,status=no,scrollbars=no,resizable=yes,menubar=no");
	displayWindow.document.write('<html><head><title>Screenshot</title></head><body>');
	displayWindow.document.write('<div style="position:absolute; top:0px; left:0px; z-index:3;">');
	displayWindow.document.write('<a href="javascript:window.close();"><img src="'+imageLocation+'" width="'+imageWidth+'" height="'+imageHeight+'" border="0" alt="Click to close window"></a>');
	displayWindow.document.write('</div>');
	displayWindow.document.bgColor = '#000000';
	displayWindow.document.write('</body><html>');
	displayWindow.document.close();
	displayWindow.window.focus();
	resizeStart(imageWidth,imageHeight);
}

function resizeStart(targetWidth,targetHeight)
{ 
	if (reset) { clearTimeout(reset); }
	var currentWidth = (window.innerWidth) ? displayWindow.window.innerWidth : parseInt(displayWindow.document.body.clientWidth);
	var currentHeight = (window.innerHeight) ? displayWindow.window.innerHeight : parseInt(displayWindow.document.body.clientHeight);
	resizeCycle(targetWidth,targetHeight,currentWidth,currentHeight);
	
}
	
function resizeCycle(targetWidth,targetHeight,currentWidth,currentHeight)
{ 
	var ieWindowElementsWidth=12;
	var ieWindowElementsHeight=30;
	if ((currentWidth != targetWidth) || (currentHeight != targetHeight))
	{ 
		if (currentWidth < targetWidth)
		{ distance = targetWidth - currentWidth;
			distance = (distance/10);
			distance = Math.round(distance);
			distance = (distance<1)?1:distance;
			currentWidth += distance;
		} 
		if (currentHeight < targetHeight)
		{ distance = targetHeight - currentHeight;
			distance = (distance/10);
			distance = Math.round(distance);
			distance = (distance<1)?1:distance;
			currentHeight += distance;
		} 

		if (document.layers){
			displayWindow.window.resizeTo(currentWidth,currentHeight)
		}
		else {
			displayWindow.window.resizeTo(currentWidth+ieWindowElementsWidth,currentHeight+ieWindowElementsHeight)
		}

		reset = setTimeout('resizeCycle('+targetWidth+','+targetHeight+','+currentWidth+','+currentHeight+')','10');
	}
	return;
}

/* Used to detect whether the "opner" window still exists - if not then opens new window with same old specs */
function openergoto(where, pageWidth, pageHeight){

	if(!opener.closed && opener != null){
		opener.location.href=where;
		window.opener.focus();
	}
	else {
		if (document.all)
			var xMax = screen.width, yMax = screen.height;
		else
			if (document.layers)
				var xMax = screen.availWidth, yMax = screen.availHeight;
			else
				var xMax = 640, yMax=480;

		var xOffset = (xMax - pageWidth)/2, yOffset = (yMax - pageHeight)/2;
		displayWindow=window.open(where,"dynamicWindow","toolbar=no,width="+pageWidth+",height="+pageHeight+",left="+xOffset+",top="+yOffset+",screenX="+xOffset+",screenY="+yOffset+",resizable=yes");
	}
}
