var winWidth, winHeight;
var screenWidth, screenHeight;
var originalImageWidth, originalImageHeight;

function getWindowBounds() {
	// all except Explorer
	if (self.innerHeight) { 
		winWidth = self.innerWidth;
		winHeight = self.innerHeight;
	// Explorer 6 Strict Mode
	} else if (document.documentElement && document.documentElement.clientHeight) {
		winWidth = document.documentElement.clientWidth;
		winHeight = document.documentElement.clientHeight;
	// other Explorers
	} else if (document.body) {
		winWidth = document.body.clientWidth;
		winHeight = document.body.clientHeight;
	}
}

function getScreenBounds() {
	screenWidth = screen.availWidth - 50;
	screenHeight = screen.availHeight - 50;
}

function resizeImage(imageName) {
	getWindowBounds();
	
	var element = document.images[imageName];
	
	if(originalImageWidth == undefined) {
		originalImageWidth = Number(element.width);
		originalImageHeight = Number(element.height);
	}
	
	if(element.height > winHeight) {
		element.height = winHeight;
	} else {
		var newHeight;
		if(element.width > winWidth) {
			newHeight = (winWidth / originalImageWidth) * originalImageHeight;
		} else {
			newHeight = winHeight > originalImageHeight ? originalImageHeight : winHeight;
			
		}
		element.height = newHeight;
	}
	
}

function openWindow(url) {
	getScreenBounds();
	var fittoscreen = window.open(url, "image", "width="+screenWidth+",height="+screenHeight+",left=0,top=0,resizable=yes");
	fittoscreen.focus();
}

function resizeWindowToImage(imageName) {
	var element = document.images[imageName];
	window.resizeTo(element.width, element.height);
}

function resizeEverything(imageName) {
	resizeImage(imageName);
	resizeWindowToImage(imageName);
}

