//Scales an image based on max size provided
//Arguments: "img" div wrapper element by id, max height, max width
function AdjustImgSz(ImgDivEl, maxHeight, maxWidth, result){
//	alert("in AdjustImgSz ImgDivEl:"+ImgDivEl.attributes[0].name);
	var SrcImgEl = ImgDivEl.getElement('img');
//	alert("got SrcImgEl");
	var thisSrc = SrcImgEl.getProperty('src');
//	alert("got thisSrc");

	//Adjust dimensions for dumbass IE which doesn't support max-height, max-width
	//but, since we have to do it for IE, we have to do it for others as well
	//because the dimensions get morphed along with opacity below (and they must be,
	//otherwise, the transition is ugly)
	newImg = new Image();
	newImg.src = thisSrc;		//DO NOT CREATE A NEW IMAGE, THIS REQUIRES RE-LOAD
//	newImg = this.SrcImgEl;
	
	var ratio;
	var width = newImg.width, height = newImg.height;

	if(newImg.height >= newImg.width)
		ratio = maxHeight / newImg.height;
	else{
		ratio = maxWidth / newImg.width;
//		if(ratio > 0.76) ratio = 0.76;
	}
		
//	alert(newImg.height + " " + newImg.width + " " + ratio);
	
	if((newImg.height > maxHeight) && (newImg.width > maxWidth)){
		if(newImg.height >= newImg.width){
			height = maxHeight;
//			ratio = newImg.width / newImg.height;
//			width = maxHeight * ratio;
			width = newImg.width * ratio;
		}
		else if(newImg.width > newImg.height){
			width = maxWidth;
//			ratio = newImg.height / newImg.width;
			height = newImg.height * ratio;
		}
	}
	else if(newImg.height >= maxHeight){
		height = maxHeight;
//		ratio = newImg.width / newImg.height;
//		width = maxHeight * ratio;
//		width = height * ratio;
		width = newImg.width * ratio;
	}
	else if(newImg.width > maxWidth){
		width = maxWidth;
//		ratio = newImg.height / newImg.width;
//		height = maxWidth * ratio;
		height = newImg.height * ratio;
	}
	else{ //Both less than max?
		height = newImg.height;
		width = newImg.width;
	}
	
	if(result){
		result.height = height;
		result.width = width;
	}
	else{
		SrcImgEl.set('height', height);
		SrcImgEl.set('width', width);
	}	
}


function ScaleImg(ImgDivID, maxHeight, maxWidth){
//	alert("in ScaleImg: "+ImgDivID+" h: "+maxHeight+" w:"+maxWidth);
	var divEl = document.getElementById(ImgDivID);
//	alert("got div element");
	AdjustImgSz(divEl, maxHeight, maxWidth);
}

/*Adjust Image Size Class
 * waits for image copy to load, then performs re-size
 * of the target image 
 */
function AdjustImgSzClass(ImgDivEl, maxHeight, maxWidth, result){
//	alert("Got to AdjustImgSzClass " + ImgDivEl.getElement('img') + " " + maxHeight);
	
	this.LoadImg = LoadImg;
	this.ResizeImg = ResizeImg;
	this.UpdateProgBar = UpdateProgBar;
	this.maxHeight = maxHeight;
	this.maxWidth = maxWidth;
	this.result = result;	
	this.SrcImgEl = ImgDivEl.getElement('img');
	this.thisSrc = this.SrcImgEl.getProperty('src');

	function LoadImg(copyImage)
	{
//		alert("Got to start LoadImg" + newImg);
		//TEST!!! just curious
//		var image_count = document.getElementsByTagName("img").length;

		if(copyImage == true){				//Create a fresh copy of the image element?
			this.newImg = new Image();
			this.newImg.src = this.thisSrc;	//DO NOT CREATE A NEW IMAGE, THIS REQUIRES RE-LOAD
		}
		else{
			this.newImg = this.SrcImgEl;
//			this.newImg.width = this.newImg.naturalWidth; 
//			this.newImg.height = this.newImg.naturalHeight;
		}
		
		//Append this image to the current page so progress bar will see and count it
		this.currentImg = document.createElement('div');	//New element
		this.currentImg.style.position = "absolute";
		this.currentImg.style.opacity = "0";
		this.currentImg.style.visibility = "hidden";
		this.currentImg.innerHTML = "<img src='"+ this.newImg.src +"' width='10' height='10'/>";
//not supported	-- requires mootools	this.currentImg.setStyle('opacity',0);
//not supported	-- requires mootools	this.currentImg.setStyle('visibility', 'hidden');
		var tag_array = document.getElementsByTagName("body");
		tag_array[0].appendChild(this.currentImg);
//		this.currentImg.currentOpacity = new Fx.Tween(this.currentImg, 'opacity', {duration: 40});	//was 400:JMW110404
		//TEST!!! just curious
//		var image_count = document.getElementsByTagName("img").length;

		//Add progress bar class so we can re-size the logo when done loading
		this.imgProgBar = new progBar();
		this.imgProgBar.doneyet = false;
		
		this.imgProgBar.setup_bar('hidden'); 	// call the function setup_bar() first
		//Periodically call: this.imgProgBar.progress_bar()
		this.progTimer = $clear(this.progTimer);
		this.progTimer = this.UpdateProgBar.periodical(800, this);				
	}	


	function UpdateProgBar()
	{
		this.imgProgBar.progress_bar();
		if(this.imgProgBar.doneyet){	//Done loading images?
//			alert("Got to start UpdateProgBar");
			this.progTimer = $clear(this.progTimer);
//not supported -- requires mootools this.currentImg.dispose();
			var tag_array = document.getElementsByTagName("body");
			tag_array[0].removeChild(this.currentImg);
			//TEST!!! just curious
//			var image_count = document.getElementsByTagName("img").length;
			this.ResizeImg();
//			this.ResizeImg.delay(1000);	//Delay a little more
		}
	}	

	
	function ResizeImg()
	{
		var ratio;
		var width = this.newImg.width, height = this.newImg.height;
		
		if(this.newImg.height > this.maxHeight){
			ratio = this.maxHeight / this.newImg.height;
			height = this.maxHeight;
			width = this.newImg.width * ratio;
			
			//Width still too wide?
			if(width > this.maxWidth){
				ratio = this.maxWidth / width;
				width = this.maxWidth;
				height = height * ratio;
			}
		}
		else if(this.newImg.width > this.maxWidth){
			width = this.maxWidth;
			ratio = this.maxWidth / this.newImg.width;
			height = this.newImg.height * ratio;
		
			//Height still too tall?
			if(height > this.maxHeight){
				ratio = this.maxHeight / height;
				height = this.maxHeight;
				width = width * ratio;				
			}
		}
		
/*
		if(this.newImg.height >= this.newImg.width)
			ratio = this.maxHeight / this.newImg.height;
		else{
			ratio = this.maxWidth / this.newImg.width;
//			if(ratio > 0.76) ratio = 0.76;
		}
		
//		alert(this.newImg.height + " " + this.newImg.width + " " + ratio);
		
		if((this.newImg.height > this.maxHeight) && (this.newImg.width > this.maxWidth)){
			if(this.newImg.height >= this.newImg.width){
				height = this.maxHeight;
//				ratio = this.newImg.width / this.newImg.height;
//				width = this.maxHeight * ratio;
				width = this.newImg.width * ratio;
			}
			else if(this.newImg.width > this.newImg.height){
				width = this.maxWidth;
//				ratio = this.newImg.height / this.newImg.width;
				height = this.newImg.height * ratio;
			}
		}
		else if(this.newImg.height >= this.maxHeight){
			height = this.maxHeight;
//			ratio = this.newImg.width / this.newImg.height;
//			width = this.maxHeight * ratio;
//			width = height * ratio;
			width = this.newImg.width * ratio;
		}
		else if(this.newImg.width > this.maxWidth){
			width = this.maxWidth;
//			ratio = this.newImg.height / this.newImg.width;
//			height = this.maxWidth * ratio;
			height = this.newImg.height * ratio;
		}
		else{ //Both less than max?
			height = this.newImg.height;
			width = this.newImg.width;
		}
*/		
		this.result.height = height;
		this.result.width = width;
		if(this.result.SetSz == true){
//			alert("Got to start ResizeImg: " + height + " " + width);
			this.SrcImgEl.set('height', height);
			this.SrcImgEl.set('width', width);				
		}

		return this.result.parent.ImgReSz();
	}	
}

