﻿$( function() {
	$(".PicAuto").each( function() {
		var BoxWidth = $(this).attr("width"),BoxHeight = $(this).attr("height");
		var img = new Image() , _this = $(this);
		img.src = $(this).attr("src");
		if ( img.complete ) {
			var RealWidth = img.width,RealHeight = img.height;
			if ( RealWidth / RealHeight > BoxWidth / BoxHeight ) {
				RealHeight = BoxWidth / RealWidth * RealHeight;
				RealWidth = BoxWidth;
				var Padding = ( BoxHeight - RealHeight ) / 2;
				$(this).attr("width",RealWidth).attr("height",RealHeight).css("padding",Padding + "px 0");
			}
			else {
				RealWidth = BoxHeight / RealHeight * RealWidth;
				RealHeight = BoxHeight;
				var Padding = ( BoxWidth - RealWidth ) / 2;
				$(this).attr("width",RealWidth).attr("height",RealHeight).css("padding","0 " + Padding + "px");
			}
		}
		img.onload = function() {
			var RealWidth = img.width,RealHeight = img.height;
			if ( RealWidth / RealHeight > BoxWidth / BoxHeight ) {
				RealHeight = BoxWidth / RealWidth * RealHeight;
				RealWidth = BoxWidth;
				var Padding = ( BoxHeight - RealHeight ) / 2;
				_this.attr("width",RealWidth).attr("height",RealHeight).css("padding",Padding + "px 0");
			}
			else {
				RealWidth = BoxHeight / RealHeight * RealWidth;
				RealHeight = BoxHeight;
				var Padding = ( BoxWidth - RealWidth ) / 2;
				_this.attr("width",RealWidth).attr("height",RealHeight).css("padding","0 " + Padding + "px");
			}
			return;
		};
	} );
} );
