$(function(){

	chgPos("#main",790,490);

	$("#mo_main,#sub_open").hover(
		function(){
			$("#common").css("display","none");
			$("#sub_back").css({
				display  : "block",
				width    : Math.max($(window).width(),850),
				height   : Math.max($(window).height(),510),
				position : "absolute",
				top      : "0px",
				left     : "0px"
			});

			var posx = $("#mo_main").position().left + $("#main").position().left + "px";
			var posy = $("#mo_main").position().top + $("#main").position().top + "px";
			$("#sub_open").css({
				display  : "block",
				position : "absolute",
				left     : posx,
				top      : posy
			});

		},
		function(){
			$("#common").css("display","block");
			$("#sub_back").css("display","none");
		}
	);

	$("#sub_open").click(
		function(){

			$("#common").css("display","none");

			/* margin around sub */
			$("#sub_open").css("display","none");

			$("#sub_back2").css({
				display  : "block",
				width    : Math.max($(window).width(),850),
				height   : Math.max($(window).height(),510),
				position : "absolute",
				top      : "0px",
				left     : "0px"
			});

			/* margin around sub */

			$("#main").css("display","none");
			$("#sub").css("display","block");

			chgPos("#sub",790,490);
		}
	);

	$(".main_open").click(
		function(){
			$("#sub_back2").css("display","none");
			$("#sub").css("display","none");
			$("#common").css("display","block");
			$("#main").css("display","block");
		}
	);
});

$(window).resize(function(){
		chgPos("#main",790,490);
		chgPos("#sub",790,490);
		$("#sub_back").css({
			width  : Math.max($(window).width(),850),
			height : Math.max($(window).height(),510),
			top    : "0px",
			left   : "0px"
		});

		$("#sub_back2").css({
			width  : Math.max($(window).width(),850),
			height : Math.max($(window).height(),510),
			top    : "0px",
			left   : "0px"
		});

});

function chgPos(idName,width,height){
	var ha=Math.max(($(window).height()+19-(height+18))/2,19);
	var wa=Math.max(($(window).width()-width)/2,19);
	var hb=""+ha+"px";
	var wb=""+wa+"px";
	$(idName).css({
		position : "absolute",
		top      : hb,
		left     : wb
	});
}

