
////////////////
// variables
////////////////

var objSecondaryNav = null;
var numOffset = 0;
var numHeight = 0;
var numFloatNavDelay = 0;
var numFloatNavInterval = 0;


////////////////
// functions
////////////////

function getWindowHeight() {
	if ( typeof( window.innerHeight ) == 'number' ) {
		return window.innerHeight;
	} else if ( document.documentElement && document.documentElement.clientHeight ) {
		return document.documentElement.clientHeight;
	} else if ( document.body && document.body.clientHeight ) {
		return document.body.clientHeight;
	} else return false;
}

function getScrollHeight() {
	if ( typeof( window.pageYOffset ) == 'number' ) {
		return window.pageYOffset;
	} else if ( document.documentElement && document.documentElement.scrollTop ) {
		return document.documentElement.scrollTop;
	} else if ( document.body && document.body.scrollTop ) {
		return document.body.scrollTop;
	} else return false;
}

function getOffset( obj ) {
	var numTop = 0;
	if ( obj.offsetParent ) {
		do {
			numTop += obj.offsetTop;	
		} while ( obj = obj.offsetParent );
		return numTop;
	} else return false;
}

function getHeight( obj ) {
	var numHeight = 0;
	if ( obj.offsetHeight ) {
		return obj.offsetHeight;
	} else return false;
}

function floatNav( blnAnimated ) {
	if ( objSecondaryNav ) {
		clearInterval( numFloatNavInterval );
		clearTimeout( numFloatNavDelay );
		numFloatNavDelay = setTimeout( floatNavDelay, 250, blnAnimated );
	}
}

function floatNavDelay( blnAnimated ) {
	if ( blnAnimated ) {
		clearInterval( numFloatNavInterval );
		numFloatNavInterval = setInterval( floatNavInterval, 25, true );
	} else {
		floatNavInterval( false );
	}	
}

function floatNavInterval( blnAnimated ) {
	var numScrollHeight = getScrollHeight();
	var numTargetOffset = numScrollHeight - numOffset;
	var numCurrentOffset = objSecondaryNav.style.top ? parseInt( objSecondaryNav.style.top ) : 0;
	if ( numTargetOffset < 0 ) {
		numTargetOffset = 0;
	} else if ( ( numTargetOffset + numHeight ) > numBodyHeight ) {
		numTargetOffset = numBodyHeight - numHeight;
	} else if ( numHeight > numWindow && 
			    numCurrentOffset < numTargetOffset ) {
		if ( ( numScrollHeight + numWindow ) > ( numCurrentOffset + numOffset + numHeight ) ) { 
			numTargetOffset = ( numScrollHeight + numWindow ) - numOffset - numHeight;
		} else {
			numTargetOffset = numCurrentOffset;
		}
	}
	if ( numCurrentOffset != numTargetOffset ) {
		if ( blnAnimated ) {
			if ( numCurrentOffset < numTargetOffset ) {
				objSecondaryNav.style.top = numCurrentOffset + Math.ceil( ( numTargetOffset - numCurrentOffset ) * 0.25 ) + "px";
			} else {
				objSecondaryNav.style.top = numCurrentOffset + Math.floor( ( numTargetOffset - numCurrentOffset ) * 0.25 ) + "px";
			}
		} else objSecondaryNav.style.top = numTargetOffset + "px";
	} else clearInterval( numFloatNavInterval );
}

function init() {
	objSecondaryNav = document.getElementById( 'secondaryNav' );
	if ( objSecondaryNav ) {
		numOffset = getOffset( objSecondaryNav );
		numHeight = getHeight( objSecondaryNav );
		numBodyHeight = getHeight( document.getElementById( "body" ) );
		numWindow = getWindowHeight();
		if ( numOffset !== false &&
			 numHeight !== false &&
			 numWindow !== false ) {
			window.onscroll = function() { floatNav( true ); };
			floatNavInterval( false );
		}
	}
}

function updateGlobals() {
	numWindow = getWindowHeight();
	floatNavInterval();
}


////////////////
// main
////////////////

window.onload = init;
window.onresize = updateGlobals;
