/**
 * JPAP
 * Float Pagetop
 * 
 * Copyright (c) KFW 2010
 * Require: jQuery 1.3
 */
(function($) {
	
	//for ie6
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(error) {};
	
	
	var _initop, _endtop, _PGTOP, _ASIDE;
	var _margin = 50;
	
	/**
	 * 追従型ページトップ設定（jQuery拡張）
	 * @return {jQuery}
	 */
	$.fn.follow = function(callback) {
		_PGTOP = $(this);
		_ASIDE = $("#Aside");
		
		//初期値設定
		$(window).bind("resize", function(event) {
			_initop = $("#Footer").offset().top;
			_endtop = _ASIDE.position().top + _ASIDE.height() - _margin - _initop;
		});
		
		$(window).bind("scroll", _follow).bind("resize", _follow).trigger("resize");
		
		return this;
	};
	
	/**
	 * 追従型ページトップ処理
	 * @param	event {Event} イベントオブジェクト
	 */
	function _follow(event) {
		var dx = (_initop - ($(window).scrollTop() + $(window).height() - _margin)) * -1;
		
		if (dx < 0 && dx > _endtop) _PGTOP.stop(true, false).animate({ "top": dx }, 100, "swing");
		else if (dx > 0 && _PGTOP.position().top != 0) _PGTOP.stop(true, false).animate({ "top": 0 }, 100, "swing");
	};
	
})(jQuery);
