$(function() {
	$('body:first').append('<div id="LB_overlay"></div><div id="LB_menu"><div id="LB_maximize" class="LB_button" title="フルスクリーン">maximize</div><div id="LB_close" class="LB_button" title="Escでも閉じられます">close</div></div><div id="LB_inner"></div>');
	
	document.onkeyup = function(e){
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}
		if(keycode == 27){
			lbclose();
		}
	};

	
	$('#LB_overlay').click(lbclose);
	$('#LB_close').click(lbclose);
	$(window).resize(lbfixed);
	$(window).scroll(lbfixed);
	$('#LB_maximize').click(lbmaximize);
});
function lbopen(html, cap) {
	lbfixed();
	if(cap) {
		html = '<div id="LB_header">'+cap+'</div>'+html;
	}
	$('html').css('overflow', 'hidden');
	$('body').css('overflow', 'hidden');
	$('select').hide();
	$('#LB_inner').html(html);
	$('#LB_overlay').show().fadeTo(200, 0.8, function(){
		$('#LB_menu').show();
		$('#LB_inner').show();
		lbfixed();
	});
	lbfixed();
	/*
	$('#LB_overlay').show().fadeTo(100, 0.8, function(){
		$('#LB_menu').fadeIn('normal');
		$('#LB_inner').fadeIn('normal').html(html);
		lbfixed();
	});
	*/
}
function lbmaximize() {
	var nv = $('#nicovideo');
	var wd = $(window).width();
	var ht = $(window).height();
	var wtop = $(window).scrollTop();
	//$('#LB_menu').fadeOut('fast');
	$('#LB_overlay').fadeTo(100, 100);
	$('#LB_inner').css('position', 'static');
	nv.css('position', 'absolute').css('top', wtop+35).css('left', 0).width(wd).height(ht-35).attr('src', nv.attr('src'));
}
function lbclose() {
	$('#LB_menu').fadeOut('fast');
	$('#LB_inner').hide().html('');
	$('#LB_overlay').fadeOut();
	$('html').css('overflow', 'auto');
	$('body').css('overflow', 'auto');
	$('select').show();
	$('#LB_inner').css('position', 'absolute');
	$('#nicovideo').css('position', 'static');
}
function lbfixed() {
	var ht = $(window).height();
	var wd = $(window).width();
	var wtop = $(window).scrollTop();
	$('#LB_menu').css('top', wtop+5);
	$('#LB_overlay').height(ht).width(wd).css('top', wtop);
	var obj = $('#LB_inner');
	var tp = ($(window).height() - obj.height())/2+wtop;
	var lf = ($('html:first').width()-obj.width())/2;
	obj.css('top', tp).css('left', lf);
	
}

