var imageScale, imageWidth, imageHeight;
function setImageAttr(image){
	if(typeof(image) == "string"){
		im = getObj(image);
	}else if(typeof(image) == "object"){
		im =image;
	}else{
		return;
	}
	imageWidth = im.width;
	imageHeight = im.height;
	imageScale = 100;
	scaleImage(im,"fit");
}
function scaleImage(image,size){
	if(typeof(image) == "string"){
		im = getObj(image);
	}else if(typeof(image) == "object"){
		im =image;
	}else{
		return;
	}
	if(imageWidth == null){
		imageWidth = im.width;
		imageHeight = im.height;
		imageScale = 100;
	}

	switch(size){
		case "fit":
			// fit image to window proportions
			var winSizeArr = getWindowSize();
			if(winSizeArr[0] && winSizeArr[1]){
				if(imageWidth/imageHeight > winSizeArr[0]/winSizeArr[1]){//image is proportionally wider - constrain to width
					im.width = winSizeArr[0];
					im.height = Math.round(imageHeight*(winSizeArr[0]/imageWidth));
				}else{
					im.height = winSizeArr[1];
					im.width = Math.round(imageWidth*(winSizeArr[1]/imageHeight));
				}
				imageScale = Math.round(100*im.width/imageWidth);
			}
			break;
		case 100:
			// scale to size
			im.width = imageWidth;
			im.height = imageHeight;
			imageScale = 100;
			break;
		default:
			// find the current scale
			var i=0;
			while(imageScale > 25){
				imageScale *= 0.5;
				i++;
			}
			imageScale = 25;
			for(j=0;j<i+size&&j<4;j++){
				imageScale *= 2;
			}
			im.width = Math.round(imageWidth*(imageScale/100));
			im.height = Math.round(imageHeight*(imageScale/100));
	}
}
function getWindowSize(){
	var winWidth = 0, winHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		winWidth = window.innerWidth;
		winHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		winWidth = document.documentElement.clientWidth;
		winHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		winWidth = document.body.clientWidth;
		winHeight = document.body.clientHeight;
	}
	var winSizeArr = new Array(winWidth, winHeight)
	return winSizeArr;
}
function getObj(n, d) {
	var p,i,obj;
	if(!d) d=document;
	if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
	}
	if(!(obj=d[n])&&d.all) obj=d.all[n];
	for (i=0;!obj&&i<d.forms.length;i++) obj=d.forms[i][n];
	for(i=0;!obj&&d.layers&&i<d.layers.length;i++) obj=getObj(n,d.layers[i].document);
	return obj;
}
