/* このJavaScriptファイルはUTF-8でエンコードされています。 */
$(function(){
	var imageHolderContainer = $('#newTopics').css('visibility', 'visible');
	var imageHolder = imageHolderContainer.find('div.photo-thumb').css('position', 'relative');
	var imageHolderLength = imageHolder.length;
	var maxWidth = parseInt(imageHolder.css('width'));
	var maxHeight = parseInt(imageHolder.css('height'));
	if($.browser.opera) {
		maxWidth -= parseInt(imageHolder.css('padding-left')) + parseInt(imageHolder.css('padding-right'));
		maxHeight -= parseInt(imageHolder.css('padding-top')) + parseInt(imageHolder.css('padding-left'));
	}
	
	imageHolder.find('img').each(function(i){
		var tmpImage = $(new Image());
		tmpImage.bind('load', {'self': this, 'count': i}, fixSizeAndPosition);
		tmpImage.attr('src', this.src);
	});

	function fixSizeAndPosition(event) {
		var self = event.data.self;
		var ratioToMaxWidth = this.width / maxWidth;
		var ratioToMaxHeight = this.height / maxHeight;
		if (ratioToMaxWidth > 1 || ratioToMaxHeight > 1) {
			if (ratioToMaxWidth > ratioToMaxHeight) {
				self.width = maxWidth;
			} else {
				self.height = maxHeight;
			}
		}
		self.style.position = 'absolute';
		self.style.left = Math.floor(maxWidth / 2 - self.width / 2 + parseInt(imageHolder.css('padding-top'))) + 'px';
		self.style.top = Math.floor(maxHeight / 2 - self.height / 2 + parseInt(imageHolder.css('padding-left'))) + 'px';
		self = null;
		this.src = '';
		if(event.data.count == imageHolderLength - 1) {
			imageHolderContainer.css('visibility', 'visible');
		}
	}
});