/**
 * コンテンツ内の画像サイズ
 */
var contentImageSize = 560;

/**
 * コンテンツ内画像のボーダー幅
 */
var contentImageSizeBorder = 10;

/**
 * サイドバー内の画像サイズ
 */
var sideBarImageSize = 250;

/**
 * サイドバー内画像のボーダー幅
 */
var sideBarImageSizeBorder = 5;

virtualTopId = "wrapper";
delayExternal = 0;

/**
 * 画像サイズ計算
 */
function calcImageSize(width, height, maxWidth, maxHeight, allowZoom) {
	if (width > 0 && height > 0) {
		if (maxHeight > 0 && height > maxHeight || allowZoom) {
			width = calcWidth(width, height, maxHeight);
			height = maxHeight;
			
			if (maxWidth > 0 && width > maxWidth) {
				height = calcHeight(width, height, maxWidth);
				width = maxWidth;
			}
		}
		
		if (maxWidth > 0 && width > maxWidth || allowZoom) {
			height = calcHeight(width, height, maxWidth);
			width = maxWidth;
			
			if (maxHeight > 0 && height > maxHeight) {
				width = calcWidth(width, height, maxHeight);
				height = maxHeight;
			}
		}
	}
	
	return { width: Math.floor(width), height: Math.floor(height) };
}

/**
 * 画像高さ計算
 */
function calcHeight(width, height, maxWidth) {
	var ratio = 100 * maxWidth / width;
	return height * ratio / 100;
}

/**
 * 画像幅計算
 */
function calcWidth(width, height, maxHeight) {
	var ratio = 100 * maxHeight / height;
	return width * ratio / 100;
}

$(function() {
	// 最大サイズを超える画像を縮小
	$('img', $('#content .entry')).each(function() {
		if ($(this).width() >= contentImageSize - contentImageSizeBorder * 2) {
			var size = calcImageSize($(this).width(), $(this).height(), contentImageSize - contentImageSizeBorder * 2, 0);
			$(this).width(size.width);
			$(this).height(size.height);
		}
	});
	
	// プラグインの最大サイズを超える画像を縮小
	$('img', $('#content .plugin')).each(function() {
		if ($(this).width() >= sideBarImageSize - sideBarImageSizeBorder * 2) {
			var size = calcImageSize($(this).width(), $(this).height(), sideBarImageSize - sideBarImageSizeBorder * 2, 0);
			$(this).width(size.width);
			$(this).height(size.height);
		}
	});
	
	if ($.browser.msie && $.browser.version < 7) {
		$('#navigation').css({ left: 0, position: 'absolute' }).get(0).style.setExpression('top', "document.documentElement.clientHeight + (documentElement.scrollTop || document.body.scrollTop) - 40 + 'px'");
		$('#navigation h2, #navigation ul').css({ display: 'block' });
	} else if (!/iphone/.test(navigator.userAgent.toLowerCase())) {
		$('#navigation').css({ left: 0 }).animate({ bottom: 0 }, 400, 'swing', function() {
			$('#navigation h2, #navigation ul').fadeIn(400);
		});
	}
	
	if (!/iphone/.test(navigator.userAgent.toLowerCase())) {
		$('.entryData').each(function() {
			if (this.id.match(/_([0-9]+)$/)) {
				sc = new wrapScroll(this.id, 'entry_' + RegExp.$1);
				sc.Interval = 100;
				sc.marginBottom = 35;
			}
		});
		
		tb_init('#content a[href$=.jpg]');
		imgLoader = new Image();
		imgLoader.src = "http://jyokyoku.sakura.ne.jp/etc/sorairo/image/loadingAnimation.gif";
	}
});
