/*

    ResizeImages v0.00a3, Jonas Eckerman (jonas@truls.org) 2004-10-31

	A small hack to automagically resize images in a document to fit browsers.
	It normally doesn't upscales images, as that would only look bad (but it can be persuaded
	to do so anyway).
	
	functions:

		initImages(marginwidth,marginheight,force)
		Used to initiate the images for the resizer.

			marginwidth: width margin in pixels.
			marginheight: height margin in pixels.
			force: if true, all images in document will be resizeable.

		resizeImages()
		Does the actual work.
	
	Something like this should be in the head of the page:

		<script src="resizeimages.js"></script>

	The body tag should look something like this:

		<body onLoad="initImages(50,70,true)" onResize="resizeImages()">
		
	The tags for images that should be resized must include the boolean attribute "resize" (unless
	force is set to true in initImages), like this:

		<img src="image.gif" width="2000" height="2000" alt="Bild" resize="yes"> 

	Optionally, the image tags may also contain the attributes "orgsize" and "orgwidth" if the maximum
	sizes are to be different from "width" and "height" (for example if upscaling is desired), like this:

		<img src="image.gif" width="800" height="800" alt="Bild" orgwidth="2000" orgheight="2000" resize="yes"> 

	Ideas for future expansion (done by others, or by me when and if I actually learn JavaScript and DOM):
	
		Make it scale coordinates in clientside image maps as well, making it possible to use rescalable
		client side image maps while waiting for the browsers to support HTML (in wich image map coordinates
		can be specified as percentages of the on-screen image).
		
*/

var  imageMarginWidth   =  0;
var  imageMarginHeight  =  0;

// The function that does the work.
function resizeImages() {
	if ((document.all)||(document.getElementsByTagName)) {
		var availwidth=(document.body.clientWidth)?document.body.clientWidth-imageMarginWidth:innerWidth-imageMarginWidth;
		var availheight=(document.body.clientHeight)?document.body.clientHeight-imageMarginHeight:innerHeight-imageMarginHeight;
		var tc=(document.all)?document.all.tags("IMG"):document.getElementsByTagName("IMG");
		for (var ti=0;ti<tc.length;ti++)
			if (tc[ti].getAttribute("resize")) {
				var orgwidth=(typeof tc[ti].getAttribute("orgwidth")=="string")?parseInt(tc[ti].getAttribute("orgwidth")):tc[ti].getAttribute("orgwidth");
				var orgheight=(typeof tc[ti].getAttribute("orgheight")=="string")?parseInt(tc[ti].getAttribute("orgheight")):tc[ti].getAttribute("orgheight");
				if ((orgwidth>availwidth) || (orgheight>availheight)) {
					var scalewidth=availwidth/orgwidth;
					var scaleheight=availheight/orgheight;
					var scale=(scalewidth<scaleheight)?scalewidth:scaleheight;
					tc[ti].width=scale*orgwidth;
					tc[ti].height=scale*orgheight;
				} else {
					tc[ti].width=orgwidth;
					tc[ti].height=orgheight;
				}
			}
	}
}

// A function that saves the margins and initializes "orgheight" and "orgwidth" before resizeing if they are not present.
function initImages(marginwidth,marginheight,force) {
	if ((document.all)||(document.getElementsByTagName)) {
		var tc=(document.all)?document.all.tags("IMG"):document.getElementsByTagName("IMG");
		for (var ti=0;ti<tc.length;ti++)
			if (force || tc[ti].resize) {
				if (!tc[ti].resize)
					tc[ti].setAttribute("resize",true);
				if (tc[ti].getAttribute("orgwidth")==null)
					tc[ti].setAttribute("orgwidth",tc[ti].width);
				if (tc[ti].getAttribute("orgheight")==null)
					tc[ti].setAttribute("orgheight",tc[ti].height);
			}
		imageMarginWidth=(marginwidth)?((typeof marginwidth=="string")?parseInt(marginwidth):marginwidth):0;
		imageMarginHeight=(marginheight)?((typeof marginheight=="string")?parseInt(marginheight):marginheight):0;
		resizeImages();
	}
}

