//图片按比例缩放
var temp_img=new Image();
function img_size(img)
{
	img.onerror=function()
	{
		this.src="none.gif";
		this.alt="this images address error";
	};
	//获取设置尺寸		防止图片src改变width/height变化问题
	var pw=0,ph=0;
	var img_w=0,img_h=0;
	if (!img.getAttribute("w")||!img.getAttribute("h"))
	{
		pw=img.width;							//获取设置尺寸 无设置为原始尺寸
		ph=img.height;						//style和css class设置的作用级别高于img 标签width/height设置
		img.setAttribute("w",pw);
		img.setAttribute("h",ph);
	}
	pw=img.getAttribute("w");
	ph=img.getAttribute("h");
	img.focus();
	if (img.onload==false)			//图片可能未载入不能获取尺寸
	{
//		alert(0);
		temp_img.src=img.src;
		img_w=temp_img.width;																	//获取原始尺寸
		img_h=temp_img.height;
		img.src=temp_img.src;																					//防止随机图片失效
	}
	else
	{
//		temp_img=img;						//复制对象 当对象在CSS伪类作用范围内时无效,删除对象时会删除原型
		temp_img=img.cloneNode(false);														//克隆对象
		document.body.appendChild(temp_img);
		temp_img.clearAttributes();																	//清除对象属性 以清除width和height设置
		img_w=temp_img.width;																	//获取原始尺寸
		img_h=temp_img.height;
		document.body.removeChild(temp_img);											//删除对象
//		alert(1);
	}
//	alert("原始尺寸：W="+img_w+" h="+img_h+" ,设置尺寸:W="+pw+" h="+ph);
//	alert("img_w="+img_w+";pw="+pw+";img_h="+img_h+";ph="+ph);
	if(img_w>0 && img_h>0)
	{
		if (img_w<pw&&img_h<ph)
		{
			img.style.height=img_h;
			img.style.width=img_w; 
		}
		else
		{
			if(img_w/img_h>= pw/ph)
			{
				img.style.width=pw;
				img.style.height=(img_h*pw)/img_w;
			}
			else
			{
				img.style.height=ph;
				img.style.width=(img_w*ph)/img_h; 
			}
		}
	}
} 
//调用：<img src="图片" onload="javascript:img_size(this)">
//自动缩放所有图片
function img_load()
{
	var img_item=document.body.getElementsByTagName("img");
	for (i=0;i<img_item.length; i++)
	{
		if (img_item[i]!=temp_img)
		{
			img_item[i].galleryImg="no";	//取消图片快捷工具
			img_size(img_item[i]);
//			img_item[i].onload=function(){ img_size(this);};
		}
	}
}
if (window.attachEvent)	//IE
{
//	window.attachEvent("onload",img_load);
}
else
{
	window.addEventListener("load",img_load,false);
}
//页面图片多不建议使用img_load()而是对图片使用img_size(img)
//<img src="图片地址" onload="javascript:img_size(this)">
