<!--
//
// Copyright (c) Tomislav Sereg
// Contact:
//   mailto:tsereg@net.hr
//   http://www.inet.hr/~tsereg/
//
var g_eltFsb       = null;
var g_eltBound     = null;
var g_nFsbMargin   = 20;
var g_nWndTop      = null;
var g_nFsbSpeed    = 200;
var g_nFsbTopBound = null;
var g_cyError      = 0;
function fsbInit( strFsbId, strBoundId, nMargin, nSpeed )
{
    // Does the browser support all the objects and properties we use?
    
    bSupportsModel = arguments && document.all && document.body && window && window.setTimeout;
    if( bSupportsModel )
    {
        var nT = 0;
        for( prop in document.body )
        {
            if( 'scrollTop' == prop || 'offsetHeight' == prop ) nT++;
        }
        bSupportsModel = ( 2 == nT );
    }
    if( bSupportsModel )
    {
        g_eltFsb   = document.all[ strFsbId ];
        g_eltBound = document.all[ strBoundId ];
        bSupportsModel = g_eltFsb && g_eltFsb.style;
        if( bSupportsModel )
        {
            var nT = 0;
            for( prop in g_eltFsb )
            {
                if( 'offsetTop' == prop || 'offsetHeight' == prop || 'offsetParent' == prop ) nT++;
            }
            for( prop in g_eltFsb.style )
            {
                if( 'posTop' == prop ) nT++;
            }
            bSupportsModel = ( 4 == nT );
        }
    }

    if( !bSupportsModel ) return;

    if( 3 <= arguments.length ) g_nFsbMargin = nMargin;
    if( 4 <= arguments.length ) g_nFsbSpeed = nSpeed;

    // Pull slider out of the page - it'll move relatively to its current 
    // position.    

    var y1 = offsetTop( g_eltFsb );
    g_eltFsb.style.position = 'relative';
    var y2 = offsetTop( g_eltFsb );
    g_cyError = y2 - y1;

    // Cannot move above it's current position nor below top of the specified
    // static element.
    g_nFsbTopBound = offsetTop( g_eltFsb ) - g_cyError;

    // Assume window is on the top of the doc. If not, this will simulate
    // it jumped down. 
    g_nWndTop = 0;

    // Continously follow movements of the window.
    window.setTimeout( 'fsbFollow();', g_nFsbSpeed );
}
function fsbFollow()
{
    // Nothing to do if the window hasn't moved.
    if( g_nWndTop == document.body.scrollTop ) 
    {
        // Call me next time, too.
        window.setTimeout( 'fsbFollow();', g_nFsbSpeed );
        return;
    }

    // If the slide bar is shorter than window we extend bottom margin so it's
    // just the same height as window.
    var nMarginExtend = 0;
    if( g_eltFsb.offsetHeight + 2 * g_nFsbMargin < document.body.offsetHeight )
    {
        nMarginExtend = document.body.offsetHeight - g_eltFsb.offsetHeight - 2 * g_nFsbMargin;
    }

    g_nWndTop   = document.body.scrollTop;
    var nWndBot = g_nWndTop + document.body.offsetHeight;
    var nFsbTop = offsetTop( g_eltFsb )  - g_cyError;
    var nFsbBot = nFsbTop + g_eltFsb.offsetHeight;

    nBotBound = offsetTop( g_eltBound );
    if( nBotBound < nFsbBot ) nBotBound = nFsbBot;

    var bWndBotIsBelowFsbBot = nFsbBot + g_nFsbMargin + nMarginExtend < nWndBot;
    var bWndTopIsAboveFsbTop = g_nWndTop < nFsbTop - g_nFsbMargin;
    if( bWndBotIsBelowFsbBot )
    {
        var nDelta = nWndBot - ( nFsbBot + g_nFsbMargin + nMarginExtend );
        if( nBotBound < nFsbBot + nDelta )
        {
            g_eltFsb.style.posTop += ( nBotBound - nFsbBot );
        }
        else
        {
            g_eltFsb.style.posTop += nDelta;
        }
    }
    else if( bWndTopIsAboveFsbTop )
    {
        var nDelta = ( nFsbTop - g_nFsbMargin ) - g_nWndTop;
        if( nFsbTop - nDelta < g_nFsbTopBound )
        {
            g_eltFsb.style.posTop = 0;
        }
        else
        {
            g_eltFsb.style.posTop -= nDelta;
        }
    }

    // Call me next time, too.
    window.setTimeout( 'fsbFollow();', g_nFsbSpeed );
}
function offsetTop( elt )
{
	y = elt.offsetTop;
	for( var eltIter = elt.offsetParent; eltIter; eltIter = eltIter.offsetParent )
	{
	    y += eltIter.offsetTop;
	}
  	return y;
}
-->

