$(function() {
	/* 画像の拡張子を設定 */
	extentionArr = new Array("png", "gif", "jpg", "jpeg");
	
	/* マウスオーバーした時のファイル名の最後に付ける文字
	 * 例) 通常時:sample.png -> オーバー時:sample_over.png なら
	 *     swapfix = "_over";
	 */
	swapfix = "_on";
	/* 現在地の時にファイルを変える場合、ファイル名の最後に付ける文字
	 * 現在地でもファイルを変更しない場合はswapfixと同じ値を指定
	 */
	nowfix = "_on";
	/* 現在地専用の画像を表示させる場合、imgエレメントに設定するクラス名
	 * 現在地でもファイルを変更しない場合は無指定でいい
	 */
	nowclass = "now";
	
	
	/* 対象を指定（ID/クラス/エレメントなど、CSSと同じ要領。仕様はjQueryに準ずる） */
	target = ".image_hover a img";
	
	$(target).hover(
		function() {
			file = chk_extention($(this).attr("src"));
			
			/* 拡張子をチェック、画像ファイルであれば実行 */
			if(file.name!=null && file.ext!=null) {
				/* 拡張子を除いたファイル名にnowfixが付いている場合はnowfixを削除 */
				pattern = new RegExp(nowfix + "$");
				if((now_index = file.name.search(pattern)) != -1) {
					file.name = file.name.substring(0, now_index);
				}
				/* マウスオーバー時のファイルを設定 */
				$(this).attr("src", file.name+swapfix+"."+file.ext);
			}
		},
		function() {
			file = chk_extention($(this).attr("src"));
			
			/* 拡張子をチェック、画像ファイルであれば実行 */
			if(file.name!=null && file.ext!=null) {
				/* 拡張子を除いたファイル名からswapfixを削除 */
				pattern = new RegExp(swapfix + "$");
				if((swap_index = file.name.search(pattern)) != -1) {
					file.name = file.name.substring(0, swap_index);
				}
				
				/* クラスにnowclassが設定されているかどうかチェック */
				classArr = $(this).attr("class").split(" ");
				var nowflag = false;
				for(i in classArr) {
					if(classArr[i] == nowclass) {
						nowflag = true;
						break;
					}
				}
				
				/* マウスオーバー時のファイルを設定 */
				/* nowclassが設定されていればnowfixを付け、いなければ何も付けないファイル名を指定 */
				if(nowflag == true) {
					$(this).attr("src", file.name+nowfix+"."+file.ext);
				} else {
					$(this).attr("src", file.name+"."+file.ext);
				}
			}
		}
	);
});


/* chk_extention ***************
 * 引数
 * 	画像へのファイルパス
 * 	例	common/img/imagename.png
 * 戻り値
 * 	拡張子を除いたファイルパス, 拡張子（ドットなし）
 * 	例	common/img/imagename, png
 */
function chk_extention(path) {
	var extention = null;
	var filename = null;
	
	for(i in extentionArr) {
		pattern = new RegExp("\." + extentionArr[i] + "$");
		extention_index = path.search(pattern);
		if(extention_index != -1 ) {
			extention = extentionArr[i];
			filename = path.substring(0, extention_index);
			break;
		}
	}
	
	return{name:filename, ext:extention};
}



