// JavaScript Document
$(function() {
	var $obj = $("#menu-float");
	var bottomPos = $("#container-viewport .container-mw").height() - $obj.height(); // lowest possible value for the menu
	if (bottomPos < 0) { bottomPos = 0;	}
	
	$("#container-viewport").scroll(function () { 
		$obj.stop(); // stop all calculations on scroll event
		var pastStartOffset			= $("#container-viewport").scrollTop() > 0;	// check if window scrolled down more than top of container
		var objFartherThanTopPos	= $obj.offset().top > 0;	// check if the object is at it's top position (starting point)
		var objBiggerThanWindow 	= $obj.outerHeight() < $("#container-viewport").height();	// if the window size is smaller than the Obj size, then do not animate.
		// if window scrolled down more than startOffset OR obj position is greater than the top position possible AND window size must be bigger than Obj size
		if ( (pastStartOffset || objFartherThanTopPos) && objBiggerThanWindow ) { 
			var newpos = $("#container-viewport").scrollTop();
			if ( newpos > bottomPos ) { newpos = bottomPos; }
			if ( $("#container-viewport").scrollTop() <= 0 ) { newpos = 0; } // if window scrolled < starting offset, then reset Obj position;
			$obj.animate({ top: newpos }, 800, 'easeOutBack' );
		}
	});
});
