$.fn.getImgSize = function() {
	var image = $(this).get(0);
	var w = image.width,
		h = image.height;

	if (typeof image.naturalWidth !== 'undefined') {
		w = image.naturalWidth;
		h = image.naturalHeight;

		if(!w || !h){
			// 20210309 itayama naturalWidth取れていないときはImageオブジェクト生成してサイズ取得
			var imageObj = new Image();
			imageObj.src = $(image).attr("src");
			w = imageObj.width;
			h = imageObj.height;
		}
		
	} else if (typeof image.runtimeStyle !== 'undefined') {
		var run    = image.runtimeStyle;
		var mem    = { w: run.width, h: run.height };  // keep runtimeStyle
		run.width  = "auto";
		run.height = "auto";
		w          = image.width;
		h          = image.height;
		run.width  = mem.w;
		run.height = mem.h;
	} else {
		var mem = { w: image.width, h: image.height };  // keep original style
		image.removeAttribute("width");
		image.removeAttribute("height");
		w            = image.width;
		h            = image.height;
		image.width  = mem.w;
		image.height = mem.h;
	}

	if (!w && !h) {
		w = $(this).width();
		h = $(this).height();
	}

	return {
		width	: w,
		height	: h
	};
}

$.fn.setImgPos = function(maxwidth, maxheight) {
	var $_this = $(this);
	var w      = $_this.width();
	var h      = $_this.height();
	var top    = ((maxheight - h) / 2);

/*
	if (maxheight < h) {
		top = top - (top * 2);
	}
*/
	$_this.css("margin-top", top+"px");
}

$.fn.resizeImg = function(maxwidth, maxheight) {
	if (!$(this).length) return;
	//alert($(this).get(0).tagName);

	var size    = $(this).getImgSize();
	var wp = hp = 100;
	var percent = 0;

	if (maxwidth < size.width) {
		wp = maxwidth/size.width * 100;
	}
	if (maxheight < size.height) {
		hp = maxheight / size.height * 100;
	}

	if (wp <= hp) {
		percent = wp;
	} else if (hp < wp) {
		percent = hp;
	}

	if (percent) {
		w = Math.round(size.width * percent / 100);
		h = Math.round(size.height * percent / 100);

		$(this).width(w).height(h);

		if ($(this).parent().hasClass("-amp-element")) {
			$(this).parent().width(w).height(h);
		}
	}
}

$.fn.getResizeImg = function(maxwidth,maxheight) {
	if (!$(this).length) return;

	var size = $(this).getImgSize();
	var wp = hp = 100;
	var percent = 0;

	if (maxwidth < size.width) {
		wp = maxwidth/size.width * 100;
	}
	if (maxheight < size.height) {
		hp = maxheight / size.height * 100;
	}

	if (wp <= hp) {
		percent = wp;
	} else if (hp < wp) {
		percent = hp;
	}

	return {
		width	: Math.round(size.width * percent / 100),
		height	: Math.round(size.height * percent / 100)
	};
}

$.fn.fitImgW = function(maxwidth, maxheight) {
	if (!$(this).length) return;
//alert($(this).get(0).tagName);
	var size = $(this).getImgSize();
//	if (size.width <= maxwidth) return;
	// 表示サイズより小さい場合に行事が崩れる
	if (size.width < maxwidth) {
		maxwidth  = size.width;
	}
	if (size.height <= maxheight) {
		maxheight = size.height;
	}

	var wp = hp = 100;
	var percent = maxwidth / size.width * 100;

	w = Math.round(size.width * percent / 100);
	h = Math.round(size.height * percent / 100);

	$(this).width(w).height(h);

	if ($(this).parent().hasClass("-amp-element")) {
		$(this).parent().width(w).height(h);
	}
}

$.fn.fitImgH = function(maxwidth, maxheight) {
	if (!$(this).length) return;
	//alert($(this).get(0).tagName);
	var size = $(this).getImgSize();
//	if (size.width <= maxwidth) return;
	// 表示サイズより小さい場合に行事が崩れる
	if (size.width < maxwidth) {
		maxwidth  = size.width;
	}
	if (size.height <= maxheight) {
		maxheight = size.height;
	}

	var wp = hp = 100;
	var percent = maxheight / size.height * 100;
	w = Math.round(size.width * percent / 100);
	h = Math.round(size.height * percent / 100);

	$(this).width(w).height(h);

	if ($(this).parent().hasClass("-amp-element")) {
		$(this).parent().width(w).height(h);
	}
}

