
// JavaScript Document
MaxTryTime = 20;	//最大尝试次数
var LoadTimes = 5;
var strLoadFail = "<DIV>图片不存在</DIV>";
var strLoading = "<DIV>读取中...</DIV>";
var strLoadTimeOut = "<DIV>图片过大，无法读取...</DIV>";
var imgObj;
var isLoading = false;

//整理图片大小，返回一个数组(整理后的图片尺寸)，下标0和1分别是宽和高
function FixImagesSize(MaxWidth,MaxHeight,iObj)
{
	
	var MaxScale = MaxWidth/MaxHeight;
	var TrueScale,ZoomScale;
	var tmpWidth,tmpHeight;
	if (typeof(iObj) == "object"){
		if(iObj.width<MaxWidth&&iObj.height<MaxHeight)return;
		TrueScale = iObj.width/iObj.height;
		if(TrueScale>MaxScale){
			ZoomScale = MaxWidth/iObj.width;
		}
		else{
			ZoomScale = MaxHeight/iObj.height;
		}
		tmpWidth = Math.round(iObj.width*ZoomScale);
		tmpHeight = Math.round(iObj.height*ZoomScale);
		iObj.width = tmpWidth;
		iObj.height = tmpHeight;
	}
	else{
		iObj.width = 0;
		iObj.height = 0;
	}
	
	return;
}

//放置图片
function PutImage(ImageURL,Target,MaxWidth,MaxHeight){
	if(isLoading){
		setTimeout("PutImage('" + ImageURL + "','" + Target + "'," + MaxWidth + "," + MaxHeight + ")", 250);
	}
	else{
		/*if (typeof(imgObj) != "object")*/imgObj = new Image();
		imgObj.src = ImageURL;
		CheckLoaded(ImageURL,Target,MaxWidth,MaxHeight);
	}
}


//判断是否已经加载
function CheckLoaded(ImageURL,Target,MaxWidth,MaxHeight)
{
	//alert(imgObj.width); 原始宽
	//alert(imgObj.height); 原始高
	isLoading = true;
		var objTarget = document.getElementById(Target);
	// 对象是否已创建
	//if(typeof(objTarget)!="object")Target = eval(Target);
  if (typeof(imgObj) == "object"){
    // 是否已取得了图像的高度和宽度
    if ((imgObj.width != 0) && (imgObj.height != 0)){
      // 根据取得的图像高度和宽度设置弹出窗口的高度与宽度，并打开该窗口
			if((imgObj.width == 28) && (imgObj.height == 30)){
				objTarget.innerHTML = strLoadFail;
			}
			else{
				FixImagesSize(MaxWidth,MaxHeight,imgObj);
				
				
				objTarget.innerHTML = "<img border='0' title='点击看大图' src=" + ImageURL + " width=" + imgObj.width + " height=" + imgObj.height + " />";
//				SetDefSize();
				
			}
			LoadTimes = 0;
			isLoading = false;
		}
    else{
      // 因为通过 Image 对象动态装载图片，不可能立即得到图片的宽度和高度，所以每隔250毫秒重复调用检查
			if(LoadTimes > MaxTryTime){
				objTarget.innerHTML = strLoadTimeOut;
				LoadTimes = 0;
				isLoading = false;
			}
			else{
				objTarget.innerHTML = strLoading;
				LoadTimes++;
				setTimeout("CheckLoaded('" + ImageURL + "','" + Target + "'," + MaxWidth + "," + MaxHeight + ")", 250);
			}
		}
  }
}

