var noImgUrl = "http://www.nexus-web.net/image/calendar/no_image.gif";

function changeCalCat( url ) {
	
	var sIndex = Number( $("#cal_cat").find("option[selected]").val() );
	
	parent.location.href = url + sIndex;
}


function adjustImgSize( imgObj, tgtWidth, tgtHeight, maxWidth, maxHeight ) {
	var styleStr, imgWidth, imgHeight, marginL, marginT;
	
	imgWidth = Number( $(imgObj).width() );
	imgHeight = Number( $(imgObj).height() );
	
	//リンク切れ及び1pxの変な画像対策
	if( imgWidth < 2 || imgHeight < 2) {
		styleStr = "display:inline; width:" + tgtWidth + "px; height:" + tgtHeight + "px;";
		var newImgObj = "<img class='" + $(imgObj).attr("class") + "' src='" + noImgUrl + "' style='" + styleStr + "'>";
		$(imgObj).replaceWith(newImgObj);
		
	//サイズ変更処理
	} else {
		if( imgWidth >= imgHeight ) {
			if( imgWidth > tgtWidth ) {
				rate = (tgtWidth / imgWidth);
				if( Math.ceil(imgHeight * rate) > tgtHeight ) {
					tmpHeight = Math.ceil(imgHeight * rate);
					tmpRate = (tgtHeight / tmpHeight);
					fixedHeight = Math.ceil(tmpHeight * tmpRate);
					rate = (fixedHeight / imgHeight);
				}
			} else {
				rate = 1;
			}
		} else {
			if( imgHeight > tgtHeight ) {
				rate = (tgtHeight / imgHeight);
				if( Math.ceil(imgWidth * rate) > tgtWidth ) {
					tmpWidth = Math.ceil(imgWidth * rate);
					tmpRate = (tgtWidth / tmpWidth);
					fixedWidth = Math.ceil(tmpWidth * tmpRate);
					rate = (fixedWidth / imgWidth);
				}
			} else {
				rate = 1;
			}
		}
		
		marginL = (maxWidth - Math.ceil(imgWidth * rate)) / 2;
		marginT = (maxHeight -  Math.ceil(imgHeight * rate)) / 2;
		
		styleStr = "display:inline; width:" + Math.ceil(imgWidth * rate) + "px; height:" + Math.ceil(imgHeight * rate) + "px;" +
					"margin-left:" + marginL + "px; margin-top:" + marginT + "px; margin-bottom:" + marginT + "px;";
	}

	$(imgObj).attr("style", styleStr);
}


/* ページ描画完了後にジャケ写サイズ調整 */
window.onload = function() {
	var jacketArray = $("body").find("img[class='jacket']");
	var sJacketArray = $("body").find("img[class='jacket_s']");
	
	for(i=0; i<jacketArray.length; i++) {
		adjustImgSize( jacketArray[i], 80, 80, 97, 80) ;
	}
	
	for(i=0; i<sJacketArray.length; i++) {
		adjustImgSize( sJacketArray[i], 50, 50, 50, 50);
	}
	
};
