//リサイズ 2010/11/15

var gWidth;
var gHeight;

function resize(){
	gWidth = $(window).width();
	gHeight = $(window).height();

	var sizeMainBox = getMaxSize_body(gWidth - 100, gHeight - 100);

	if(sizeMainBox["height"] < 560 || sizeMainBox["width"] < 1000){
		sizeMainBox["width"]	= 1000;
		sizeMainBox["height"]	= 560;
	}

	$("#main_box").css({
		"width"		: sizeMainBox["width"],
		"height"	: sizeMainBox["height"]
	});

	var sizeRightBox = getMaxSize_flash(sizeMainBox["width"] - $("#left_box").width(), sizeMainBox["height"] - $("#footer").height() - 40);

	$("#right_box").css({
		"width"		: sizeRightBox["width"],
		"height"	: sizeRightBox["height"]
	});

	
	$("#left_box").css({
		"height"	: sizeRightBox["height"]
	});
	
	$("#main_box_inner").css({
		"height"	: sizeRightBox["height"]
	});

	imgWidth = $("#right_box img").width();
	imgHeight = $("#right_box img").height();
	
	$(".contentsBox").css({"height" : sizeRightBox["height"] - 130});
	$(".contentsBoxCl").css({"height" : sizeRightBox["height"] - 160});
	
	
	if(imgHeight < imgWidth){
		$("#right_box img").css({"width": sizeRightBox["width"],"height": "auto"});
		imgHeight = $("#right_box img").height();
		var imgM = (sizeRightBox["height"] - imgHeight)/2;
		$("#right_box img").css({"margin-top": imgM});
	}else if(imgHeight > imgWidth){
		$("#right_box img").css({"width": "auto","height": sizeRightBox["height"]});
	}else{
		$("#right_box img").css({"width": "100%","height": "100%"});
	}
	
	setTimeout(function(){
		getWindowSize();
	},10);
}

function getMaxSize_body(reqWidth, reqHeight){
	//16:9
	var _h = reqWidth / 16 * 9;
	var _w = reqHeight / 9 * 16;

	if(_h < reqHeight){
		return {
			"width"	: _h / 9 * 16,
			"height": _h
		};
	}
	else if(_w < reqWidth){
		return {
			"width"	: _w,
			"height": _w / 16 * 9
		};
	}
	else{
		alert(_w+","+_h);
	}
}

function getMaxSize_flash(reqWidth, reqHeight){
	//560:460
	var _h = reqWidth / 460 * 460;//w*h
	var _w = reqHeight / 460 * 460;//h*w

	if(_h < reqHeight){
		return {
			"width"	: _h / 460 * 460,//h*w
			"height": _h
		};
	}
	else if(_w < reqWidth){
		return {
			"width"	: _w,
			"height": _w / 460 * 460//w*h
		};
	}
	else{
		alert(_w+","+_h);
	}
}

function getWindowSize(){
	var _width = $(window).width();
	var _height = $(window).height();

	if(!gWidth || !gHeight){
		resize();
	}
	else{
		if(gWidth != _width || gHeight != _height){
			resize();
		}
		else{
			setTimeout(function(){
				getWindowSize();
			},50);
		}
	}
}

var timer_load;
var loadtimercount = 0;
$(function(){
	timer_load = setInterval(function(){
		if($("#right_box img").height() || loadtimercount > 10){
			clearInterval(timer_load);
			resize();
			$("body").css({"visibility": "visible"});
		}
		else{
			loadtimercount++;
		}
	}, 300);	
});

