// via http://blog.sakurachiro.com/2010/05/jquery-practice-floating-menu-3/

$(function (){
	offsetY = 40;				//メニュー上のマージン px
	minMargin = 10;				//absoluteでコンテンツ部分が短い時のmargin
	duration = 1000;			//処理時間 (ミリ秒) 0だとposition:fixedのようになる、チラつくけど
	_foName = '#sidebar';		//移動させるボックス部分
	_navRelative = 0;			//0:absolute 1:relative
	_ftName = '#footer';		//フッター部分
	easingSet = 'easeOutExpo';
	
	windowHeight = parseInt($(window).height());
	navHeight = $(_foName).outerHeight(true);					//ナビゲーションのmargin+border+padding+heightを合わせた高さ
	navHeights = navHeight + offsetY;							//navの上余白
	bodyHeight = $('body').outerHeight(true);					//コンテンツの高さ
	navSetY = $(_foName).offset().top;							//ナビゲーションの初期座標(=margin・paddingを含めたheadの高さ)
	navMarginSetY = navSetY - offsetY;							//ヘッダの高さを超えた時の位置用
	chkLayout();
	desiredHeight = navHeights + footerArea;					//スクロールさせる為に最低限必要な高さ(footer+navの高さ)
	navMaxY = bodyHeight - (navHeight + footerArea + offsetY);	//navが重ならずに降りられる限界y座標
	floatObjMove();
});


function chkLayout(){
	if(_navRelative){
		$(_foName).css('position', 'relative');					//勝手にposition付与
	}
	
	if($(_ftName).offset() == null){
		footerArea = footerSetY = 0;
	}else{
		footerSetY = parseInt($(_ftName).offset().top);			//フッターの初期座標(=margin・paddingを含めたheadの高さ)
		footerArea = bodyHeight - footerSetY;					//footerの高さ
		if(_navRelative == 0 && footerSetY < navHeight + navSetY){
			ftWidth = $(_ftName).outerWidth(true);
			$(_ftName).css('position', 'absolute').css('width', ftWidth).css('top', navHeight + navSetY + minMargin);
		}
	}
}


//スクロール毎の処理

function floatObjMove(){
	$(window).scroll(function(){
		var navScrollTop = parseInt($(this).scrollTop());	//スクロール量
		navBottomY = navScrollTop + navHeights;				//ナビゲーションのbottomの座標
		if(navHeights < windowHeight){						//ウインドウサイズとナビゲーションのサイズ判定
			if(navScrollTop <= navSetY){
				$(_foName).animate({
					top: 0
				}, {
					duration: duration,
					queue: false,
					easing: easingSet
				});
			}else if(navMaxY < navScrollTop){
				$(_foName).animate({
					top: navMaxY - navSetY
				}, {
					duration: duration,
					queue: false,
					easing: easingSet
				});
			}else if(navScrollTop < navMaxY){
				$(_foName).animate({
					top: navScrollTop - navMarginSetY
				}, {
					duration: duration,
					queue: false,
					easing: easingSet
				});
			}
		}
	});
}


//リサイズしたらwindowのサイズを変更

$(window).resize(function(){ windowHeight = parseInt($(window).height()); });


//jquery.easing.1.3.jsから

jQuery.extend(jQuery.easing, {
	easeInSine: function (x, t, b, c, d){		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;},
	easeOutSine: function (x, t, b, c, d){		return c * Math.sin(t/d * (Math.PI/2)) + b;},
	easeInOutSine: function (x, t, b, c, d){	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;},
	easeInExpo: function (x, t, b, c, d){		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;},
	easeOutExpo: function (x, t, b, c, d){		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;},
	easeInCirc: function (x, t, b, c, d){		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;},
	easeOutCirc: function (x, t, b, c, d){		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;}
});
