// auto margin
function DrawImage(ImgD,FitWidth,FitHeight){     
	var image=new Image();
	image.src=ImgD.src;
	if(image.width>0 && image.height>0){
		if(image.width/image.height>= FitWidth/FitHeight){
			if(image.width>FitWidth){
				ImgD.width=FitWidth;
				ImgD.height=(image.height*FitWidth)/image.width;
			}else{
				ImgD.width=image.width;
				ImgD.height=image.height;
			}
		} else{
			if(image.height>FitHeight){
				ImgD.height=FitHeight;
				ImgD.width=(image.width*FitHeight)/image.height;
			}else{
				ImgD.width=image.width;
				ImgD.height=image.height;
			}
		}
		ImgD.style.marginTop = (FitHeight-ImgD.height)/2+"px";
	} 
}

// no margin
function DrawImageNomargin(ImgD,FitWidth,FitHeight){     
	var image=new Image();
	image.src=ImgD.src;
	if(image.width>0 && image.height>0){
		if(image.width/image.height>= FitWidth/FitHeight){
			if(image.width>FitWidth){
				ImgD.width=FitWidth;
				ImgD.height=(image.height*FitWidth)/image.width;
			}else{
				ImgD.width=image.width;
				ImgD.height=image.height;
			}
		} else{
			if(image.height>FitHeight){
				ImgD.height=FitHeight;
				ImgD.width=(image.width*FitHeight)/image.height;
			}else{
				ImgD.width=image.width;
				ImgD.height=image.height;
			}
		}
	} 
}

function ExpandImage(ImgD,FitWidth,FitHeight){     
	var image=new Image();
	image.src=ImgD.src;
	if(image.width>0 && image.height>0){
		ImgD.width=FitWidth;
		ImgD.height=(image.height*FitWidth)/image.width;
		if(ImgD.height > FitHeight)
			ImgD.height = FitHeight;
	} 
}

function ExpandImage1(ImgD,FixedWidth){     //固定图宽，然后后等比例缩放
	var image=new Image();
	image.src=ImgD.src;
	if (image.width < FixedWidth){
		ImgD.width = image.width;
		ImgD.height = image.height;
		return;
	}
	if(image.width>0 && image.height>0){
		ImgD.width=FixedWidth;
		ImgD.height=(image.height*FixedWidth)/image.width;
	}
}

var showimg = {
	bgopacity : 0.8,
	thisimg : '',
	imgsrc : '',
	loadimgsrc : 'images/loading.gif',
	loadimgages : new Array(),
	show : function(imgid) {
		var obj = document.getElementById(imgid);
		showimg.imgsrc = obj.getAttribute('rel');
		if (showimg.imgsrc == '')
			return;
		var scrollTop, winHeight;
		var bgdiv = document.createElement("div");
		bgdiv.id = 'showimagesbg';
		bgdiv.style.position = "absolute";
		bgdiv.style.top = 0;
		bgdiv.style.left = 0;
		bgdiv.style.zIndex = 50;
		if (window.innerHeight) {
			winHeight = window.innerHeight;
		} else if ((document.body) && (document.body.clientHeight)) {
			winHeight = document.body.clientHeight;
		}
		if (document.documentElement && document.documentElement.clientHeight) {
			winHeight = document.documentElement.clientHeight;
		}
		if (winHeight < document.body.clientHeight)
			winHeight = document.body.scrollHeight;
		bgdiv.style.height = winHeight + "px";
		bgdiv.style.filter = "alpha(opacity=" + (showimg.bgopacity * 100) + ")";
		bgdiv.style.opacity = showimg.bgopacity;
		bgdiv.style.MozOpacity = showimg.bgopacity;
		bgdiv.style.backgroundColor = "#000";
		bgdiv.style.width = document.documentElement.clientWidth + "px";
		document.body.appendChild(bgdiv);
		if (document.documentElement && document.documentElement.scrollTop) {
			scrollTop = document.documentElement.scrollTop;
		} else if (document.body) {
			scrollTop = document.body.scrollTop;
		}
		var boxdiv = document.createElement('div');
		boxdiv.id = 'showimagesbox';
		boxdiv.style.position = "absolute";
		boxdiv.style.top = scrollTop + 10 + "px"
		boxdiv.style.width = 150 + "px"
		boxdiv.style.zIndex = 51;
		boxdiv.style.backgroundColor = "#fff";
		boxdiv.style.padding = '5px';
		boxdiv.style.left = (document.body.scrollWidth - 150) / 2 + "px";
		boxdiv.style.textAlign = 'center';
		document.body.appendChild(boxdiv);
		var simg = document.createElement('img');
		simg.id = 'showimagesimg';
		simg.style.cursor = 'pointer';
		simg.style.margin = '0px';
		simg.src = showimg.loadimgsrc;
		simg.onclick = function() {
			showimg.close();
		};
		boxdiv.appendChild(simg);
		/* 
		var closea = document.createElement('a'); 
		closea.innerHTML == '关闭'; 
		closea.href = 'javascript:showimg.close();'; 
		boxdiv.appendChild(closea); 
		 */
		showimg.thisimg = imgid;
		var isload = 0;
		for ( var s = 0; s < showimg.loadimgages.length; s++) {
			if (showimg.loadimgages[s] == showimg.imgsrc) {
				isload = 1;
			}
		}
		if (isload) {
			simg.src = showimg.imgsrc;
			boxdiv.style.left = (document.body.scrollWidth - simg.offsetWidth)
					/ 2 + "px";
			boxdiv.style.width = simg.width + 'px';
		} else {
			showimg.getimg();
			showimg.loadimgages[showimg.loadimgages.length] = showimg.imgsrc;
		}
	},
	close : function() {
		var bgdiv = document.getElementById('showimagesbg');
		var boxdiv = document.getElementById('showimagesbox');
		var simg = document.getElementById('showimagesimg');
		boxdiv.removeChild(simg);
		document.body.removeChild(bgdiv);
		document.body.removeChild(boxdiv);
	},
	imgonload : function(myimg) {
		document.getElementById('showimagesbox').style.left = (document.body.scrollWidth
				- myimg.width - 10)
				/ 2 + "px";
		document.getElementById('showimagesimg').src = showimg.imgsrc;
		document.getElementById('showimagesbox').style.width = myimg.width + 'px';
	},
	getimg : function() {
		var myimg = new Image();
		myimg.onload = function() {
			showimg.imgonload(myimg);
		};
		myimg.src = showimg.imgsrc;
	}
};

