// ========================================================================================@ MTechno
//  Func_ImgAdjuster.js ---- JavaScript Common Library ver.1.0
// -------------------------------------------------------------------------------------------------
//  prototype.js は必要ありません  (!)
// =================================================================================================
//   STRIPE TABLE FUNCTION
//--------------------------------------------------------------------------------------------------
// 特定サイズ内に収まるように画像を比率を変えずにリサイズもしくはクリップします。
// ex : リサイズしたい場合
//       <img src="hoge.jpg" width="320" height="240" onLoad="ImgResizeAdjust(this)">
// ex : クリップしたい場合
//       <img src="hoge.jpg" width="320" height="240" onLoad="ImgClipAdjust(this)">
/*------------------------------------------------------------*/
function ImgResizeAdjust( tImg ) {
	var nImg = new Image(), p = tImg.parentNode;
	nImg.className = tImg.className;
	nImg.style.cssText = tImg.style.cssText;
	var observer = new Image();
	observer.onload = function () {
		var ns = [], w = tImg.width / this.width, h = tImg.height / this.height;
		if ( this.width > tImg.width || this.height > tImg.height ) {
			if ( this.width >= this.height ) {
				ns = [ Math.round( this.width * w ), Math.round( this.height * w ) ];
				if ( ns[1] >= tImg.height ) {
					ns = [ Math.round( this.width * h ), Math.round( this.height * h ) ];
				}
			} else {
				ns = [ Math.round( this.width * h ), Math.round( this.height * h ) ];
				if ( ns[0] >= tImg.width ) {
					ns = [ Math.round( this.width * w ), Math.round( this.height * w ) ];
				}
			}
		} else {
			ns = [ this.width, this.height ];
		}
		nImg.src = this.src;
		nImg.width = ns[0];
		nImg.height = ns[1];
		p.replaceChild( nImg, tImg );
	};
	observer.src = tImg.src;
}
function ImgClipAdjust( tImg ) {
	var nImg = new Image(), p = tImg.parentNode, d = document.createElement('div');
	d.className = tImg.className;
	d.style.cssText = tImg.style.cssText;
	d.style.width = tImg.width + 'px';
	d.style.height = tImg.height + 'px';
	d.style.overflow = 'hidden';
	d.appendChild( nImg );
	var observer = new Image();
	observer.onload = function () {
		var ns = [], w = tImg.width / this.width, h = tImg.height / this.height;
		if ( this.width >= this.height ) {
			ns = [ Math.round( this.width * w ), Math.round( this.height * w ) ];
			if ( ns[1] < tImg.height ) {
				ns = [ Math.round( this.width * h ), Math.round( this.height * h ) ];
			}
		} else {
			ns = [ Math.round( this.width * h ), Math.round( this.height * h ) ];
			if ( ns[0] < tImg.width ) {
				ns = [ Math.round( this.width * w ), Math.round( this.height * w ) ];
			}
		}
		ns[2] = ( ns[0] - tImg.width ) / 2 * -1;
		ns[3] = ( ns[1] - tImg.height ) / 2 * -1;
		nImg.src = this.src;
		nImg.width = ns[0];
		nImg.height = ns[1];
		nImg.style.marginLeft = ns[2] + 'px';
		nImg.style.marginTop = ns[3] + 'px';
		if ( p.tagName.toLowerCase() == 'a' && !!p.href ) {
			var q = document.createElement('a');
			q.href = p.href;
			d.appendChild(q);
			q.appendChild( nImg );
		}
		p.replaceChild( d, tImg );
	};
	observer.src = tImg.src;
}
