var name = "#page-left-move";
var menuYloc  = null;
var margin_top= 20;		// 移動後のwindow内top(上のマージン px) 
var def_top   = 256;	// window内でのデフォルトtop(px)
var footer_height = 140;

$(document).ready(function(){
	menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")));
	menuYloc = -( def_top );
	docHight = $(document).height();
	$(window).scroll(function () { 
		var scroll_top = $(document).scrollTop();
		var box_y      = $(name).offset().top;
		var move_span  = $(name).height();	// 左メニューのheight(px)
		offset = menuYloc + $(document).scrollTop() + margin_top ;

//$('#out').text('docHight:'+ docHight + ', scrollTop: '+ scroll_top + ', menuYloc:'+ menuYloc + ', box_y' + box_y + 'def:' + def_top);
		if( scroll_top - (box_y + move_span) > 0 || box_y - (scroll_top + move_span) > 0 || scroll_top == 0 ){
			// 一番上までスクロール時（付きぬけ防止）
			if( offset < def_top ){
				offset = 0;
			}
			// 一番下までスクロール時（付きぬけ防止）
			var over = (scroll_top + move_span + footer_height ) - docHight;
			if( over > 0 ){
				offset -= over;
			}

			$(name).animate({top:offset+"px"},{queue:false,duration:800,easing:"easeInOutQuad"});
		}
	});
});

jQuery.extend( jQuery.easing,
{
	def: 'easeInOutQuad',
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	}
});

