﻿/*
**************图片预加载插件******************
///作者：没剑(2008-06-23)
///http://regedit.cnblogs.com

///说明：在图片加载前显示一个加载标志，当图片下载完毕后显示图片出来
可对图片进行是否自动缩放功能
此插件使用时可让页面先加载，而图片后加载的方式，
解决了平时使用时要在图片显示出来后才能进行缩放时撑大布局的问题
///参数设置：
scaling     是否等比例自动缩放
width       图片最大高
height      图片最大宽
loadpic     加载中的图片路径
*/
jQuery.fn.LoadImage=function(width,height,txtheight,loadpic){
    if(loadpic==null)loadpic="/Upfiles/system/loading.gif";
	return this.each(function(){
		var t=$(this);
		//取父框div.img的内外框值
				var	parentPadTop=Math.round(t.parent().css("padding-top").replace(/\D/g,''));
				var	parentPadBom=Math.round(t.parent().css("padding-bottom").replace(/\D/g,''));
				var	parentMarBom=Math.round(t.parent().css("margin-bottom").replace(/\D/g,''));
				var	parentMarTop=Math.round(t.parent().css("margin-top").replace(/\D/g,''));
				var	parentPadLft=Math.round(t.parent().css("padding-left").replace(/\D/g,''));
				var	parentPadRgt=Math.round(t.parent().css("padding-right").replace(/\D/g,''));
				var	parentMarLft=Math.round(t.parent().css("margin-left").replace(/\D/g,''));
				var	parentMarRgt=Math.round(t.parent().css("margin-right").replace(/\D/g,''));
				var	parentBdRgt=Math.round(t.parent().css("border-right-width").replace(/\D/g,''));
				var	parentBdLft=Math.round(t.parent().css("border-left-width").replace(/\D/g,''));
				var	parentBdTop=Math.round(t.parent().css("border-top-width").replace(/\D/g,''));
				var	parentBdBom=Math.round(t.parent().css("border-bottom-width").replace(/\D/g,''));
				var	boxWidth=parentBdLft+parentPadLft+parentMarLft+parentBdRgt+parentPadRgt+parentMarRgt;
				var	boxHeight=parentBdTop+parentPadTop+parentMarTop+parentBdBom+parentPadBom+parentMarBom;
		//设定父级外框a元素高度
		var aheight=height+txtheight;		
		t.parent().parent().css({height:aheight,width:width});
		var src=$(this).attr("src");
		var img=new Image();
		//alert("Loading...")
		img.src=src;
		//自动缩放图片
		var autoScaling=function(){
			
			
				if(img.width>0 && img.height>0){ 
			        if(img.width/img.height>=width/height){ 
			            if(img.width>width){ 
			                t.width(width-boxWidth); 
			                t.height((img.height*width)/img.width-boxHeight); 
			            }else{ 
			                t.width(img.width-boxWidth); 
			                t.height(img.height-boxHeight); 
			            } 
			        } 
			        else{ 
			            if(img.height>height){ 
			                t.height(height-boxWidth); 
			                t.width((img.width*height)/img.height-boxHeight); 
			            }else{ 
			                t.width(img.width-boxWidth); 
			                t.height(img.height-boxHeight); 
			            } 
			        } 
			    } 
				
				if (t.height()<height){
					//取父padding和margin值

					var temppad=Math.abs(Math.round((height-t.height()-boxWidth)/2))-2;
					t.css({"margin-top":temppad});
					t.css({"margin-bottom":temppad});
				}
				if (t.width()<width){

					var temppad2=Math.abs(Math.round((width-t.width()-boxHeight)/2));
					t.css({"margin-left":temppad2});
					t.css({"margin-right":temppad2});
				}

			}	
	
		//处理ff下会自动读取缓存图片
		if(img.complete){
		    //alert("getToCache!");
			autoScaling();
		    return;
		}
		$(this).attr("src","");
		var loading=$("<img alt=\"加载中...\" title=\"图片加载中...\" src=\""+loadpic+"\" />");
		
		t.hide();
		t.after(loading);
		$(img).load(function(){
			autoScaling();
			loading.remove();
			t.attr("src",this.src);
			t.css()
			t.show();
			//alert("finally!")
		});
		
	});
}