(function(jQuery){

var noImgUrl = "http://www.nexus-web.net/image/calendar/no_image.gif";

jQuery(document).ready(function(){
	var IndexTodayBirthdayArray = jQuery("body").find("img[class='imgsize_index_today_birthday']");
	var IndexTodayLiveArray = jQuery("body").find("img[class='imgsize_index_today_live']");
	var IndexTodayReleaseArray = jQuery("body").find("img[class='imgsize_index_today_release']");
	var TodayBirthdayArray = jQuery("body").find("img[class='imgsize_today_birthday']");
	var TodayReleaseArray = jQuery("body").find("img[class='imgsize_today_release']");
	
	for(i=0; i<IndexTodayBirthdayArray.length; i++) {
		adjustImgSize( IndexTodayBirthdayArray[i], 30, 30, 30, 30);
	}
	
	for(i=0; i<IndexTodayLiveArray.length; i++) {
		adjustImgSize( IndexTodayLiveArray[i], 30, 30, 30, 30);
	}
	
	for(i=0; i<IndexTodayReleaseArray.length; i++) {
		adjustImgSize( IndexTodayReleaseArray[i], 50, 50, 50, 50);
	}
	
	for(i=0; i<TodayBirthdayArray.length; i++) {
		adjustImgSize( TodayBirthdayArray[i], 100, 60, 100, 60);
	}
	
	for(i=0; i<TodayReleaseArray.length; i++) {
		adjustImgSize( TodayReleaseArray[i], 60, 60, 60, 60);
	}
	
 });



function changeCalCat( url ) {
	
	var sIndex = Number( jQuery("#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( jQuery(imgObj).width() );
	imgHeight = Number( jQuery(imgObj).height() );
	
	//リンク切れ及び1pxの変な画像対策
	if( imgWidth < 2 || imgHeight < 2) {
		styleStr = "display:inline; width:" + tgtWidth + "px; height:" + tgtHeight + "px;";
		var newImgObj = "<img class='" + jQuery(imgObj).attr("class") + "' src='" + noImgUrl + "' style='" + styleStr + "'>";
		jQuery(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;";
	}

	jQuery(imgObj).attr("style", styleStr);
}


})(jQuery);

