/**
 * jQuery plugin to automaticaly add a goTop link 
 */
(function($){
	var _init = false; // have the plugin been allready launched ?
	$.goTop = function (config)
	{
		if(!_init && $.fn.scrollTop) {
			config = $.extend({
				template : '<div style="display:block;" id="gotop"><a href="javascript:void(0);"></a></div>'
				,speed : 100 // ms
				,min : 100 // px
				,checkDelay : 200 // ms
			},config);
			// insertion :
			var _$goTop = $(config.template)
			.hide()
			.click(function(){
				$.scrollTo(0,config.speed);
			})
			.prependTo('body')
			// a private delay pointer :
			,_scrollTimeout
			// checking function :
			,_check = function() {
				$.profile && console.time('goTop');
				if ( $(window).scrollTop() > config.min ) 
				{
					_$goTop.fadeIn(config.speed);
				}
				else
				{ 
					_$goTop.fadeOut(config.speed);
				}
				$.profile && console.timeEnd('goTop');
			};
			// spying scroll position :
			$(window)
			// on scroll
			.scroll(function scrollTemporizer(){
				clearTimeout(_scrollTimeout);
				_scrollTimeout = setTimeout(_check,config.checkDelay);
			})
			// and even on load :
			.ready(_check)
			;
			_init = true;
		}
		return this;
	};
})(jQuery);
