/**
 * 記事内画像のサイズが範囲外(上限/下限)の場合はリサイズする。
 * 事前に jquery.js を読み込んでおくこと。
 * 
 * @author net@aic-co.jp
 * @version $Revision$
 * 
 * Copyright 2006-2007, AIC CORPORATION All Rights Reserved.
 **/
$(document).ready(function()
{
	/*
	 * 記事本文内の画像
	 */
	$("img", $(".cnt1")).each( aic_resizer);
	/*
	 * 記事本文（続きを読む）内の画像
	 */
	$("img", $(".cnt2")).each( aic_resizer);
});

function aic_resizer()
{
	// ユーザ投稿画像判定
	var outside = true;
	var dir = new Array();
	dir[0] = "/images/users/";
	dir[1] = "/images/users2/";

	for(i=0; i < dir.length; i++)
	{
		if( -1 != this.src.indexOf(dir[i]))
		{
			outside = false;
			break;
		}
	}
	if( outside){ return;}

	if( this.complete)
	{
		// 読込み完了 -> リサイズ実行
		// thisを渡したいので .each() を使ってます
		$(this).each( aic_resize);
	}
	else{
		// 読込み未完 -> 読込み後リサイズ実行
		$(this).bind( "load", aic_resize);
	}
}
function aic_resize()
{
	// 画像サイズ判定
	var max = 300;
	var min = 10;
	var rate;
	var w = this.style.width  || this.width;
	var h = this.style.height || this.height;
	if( isNaN(w)){ w = w.match("^[0-9]+");}
	if( isNaN(h)){ h = h.match("^[0-9]+");}

	if( (0 == w) || ("" == w))
	{
		// 0のときがあるようです...(読込み未完?)
		// サイズ判定できないので、とりあえずreturn
//		alert( this.src + "\nwidth=" + w + "\ncomplete=" + this.complete);
		return;
	}

	// リサイズ
	if( max < w)
	{
		rate = h / w;
		w = max;
		h = max * rate;
	}
	else if( min > w)
	{
		rate = h / w;
		this.width  = min;
		this.height = min * rate;
	}
	this.width  = w;
	this.height = h;
	this.style.width  = w + "px";
	this.style.height = h + "px";
}

