/*
|--------------------------------------------------------------------------
| UItoTop jQuery Plugin 1.1
| http://www.mattvarone.com/web-design/uitotop-jquery-plugin/
|--------------------------------------------------------------------------
*/

(function($){
	$.fn.UItoTop = function(options) {

 		var defaults = {
			text: 'Haut de page',
			min: 400,
			max:95,
			inDelay:600,
			outDelay:400,
  			containerID: 'toTop',
			containerHoverID: 'toTopHover',
			scrollSpeed: 1200,
			easingType: 'linear',
			parentContainer :'body'
 		};

 		var settings = $.extend(defaults, options);
		var containerIDhash = '#' + settings.containerID;
		var containerHoverIDHash = '#'+settings.containerHoverID;
		
		$(settings.parentContainer).append('<a href="#" id="'+settings.containerID+'">'+settings.text+'</a>');
		$(containerIDhash).hide().click(function(){
			$('html, body').animate({scrollTop:0}, settings.scrollSpeed, settings.easingType);
			$('#'+settings.containerHoverID, this).stop().animate({'opacity': 0 }, settings.inDelay, settings.easingType);
			return false;
		})
		.prepend('<span id="'+settings.containerHoverID+'"></span>')
		.hover(function() {
				$(containerHoverIDHash, this).stop().animate({
					'opacity': 1
				}, 600, 'linear');
			}, function() { 
				$(containerHoverIDHash, this).stop().animate({
					'opacity': 0
				}, 700, 'linear');
			});
					
		$(window).scroll(function() {
			var sd = $(window).scrollTop();
			var destiH = $(window).scrollTop() + $(window).height() - $(containerIDhash).innerHeight();
			destiH = (destiH > ($('body').height() - settings.max) ) ? ($('body').height() - settings.max) : destiH
			
			if(typeof document.body.style.maxHeight === "undefined") {//ie
				$(containerIDhash).css({
					'position': 'absolute',
					'top': destiH
				});
			}else{
				if(sd > ($('body').height()- ($(window).height() + settings.max)) ){
					$(containerIDhash).css({'bottom': settings.max - $(containerIDhash).innerHeight()});
				}else{
					$(containerIDhash).css({'bottom':0});
				}
			}
			if ( sd > settings.min ) 
				$(containerIDhash).fadeIn(settings.inDelay);
			else 
				$(containerIDhash).fadeOut(settings.Outdelay);
		});

};
})(jQuery);
