/*
 * @(#) scroller.js	(c)2005 chime interactive
 *
 * see http://www.chime.com/ for contact info/use permissions
 *
 * a simple scroller for moving aorund inside of <DIV> blocks
 */

var _scrollSteps = 15;			// how many moves until we get there
var _scrollIntervalLength = 10;		// how often to move a step (in ms)
var _scrollIntervalId = new Array();	// currently active scroller intervals

/*
 * _scrollStep() - this is called via the interval to keep moving
 * closer to our destination and then stop when we get there
 */
function _scrollStep (id, horizontalAmt, finalLeft, verticalAmt, finalTop)
{
    var obj = myGetElementById (id);
    var horizontalDone = false;
    var verticalDone = false;

    if ( ! obj ) 
    {
	alert (id + " cannot be located; aborting!");
	clearInterval (_scrollIntervalId[id]);
	_scrollIntervalId[id] = undefined;
	return;
    }

    if ( horizontalAmt != 0 )
    {
	// check for being exactly done or leaping past the end
	if ( horizontalAmt < 0 )
	{
	    if ( obj.scrollLeft + horizontalAmt <= finalLeft )
		horizontalDone = true;
	}
	else
	{
	    if ( obj.scrollLeft + horizontalAmt >= finalLeft )
		horizontalDone = true;
	}

	if ( horizontalDone )
	    obj.scrollLeft = finalLeft;
	else
	{
	    // this checks to see if we've hit the end of our 
	    // available scrolling space
	    var old = obj.scrollLeft;
	    obj.scrollLeft += horizontalAmt;
	    if ( old == obj.scrollLeft )
		horizontalDone = true;
	}
    }
    else horizontalDone = true;

    if ( verticalAmt != 0 )
    {
	// check for being exactly done or leaping past the end
	if ( verticalAmt < 0 )
	{
	    if ( obj.scrollTop + verticalAmt <= finalTop )
		verticalDone = true;
	}
	else
	{
	    if ( obj.scrollTop + verticalAmt >= finalTop )
		verticalDone = true;
	}

	if ( verticalDone )
	    obj.scrollTop = finalTop;
	else
	{
	    // this checks to see if we've hit the end of our 
	    // available scrolling space
	    var old = obj.scrollTop;
	    obj.scrollTop += verticalAmt;
	    if ( obj.scrollTop == old )
		verticalDone = true;
	}
    }
    else verticalDone = true;


    if ( horizontalDone && verticalDone )
    {
	clearInterval (_scrollIntervalId[id]);
	_scrollIntervalId[id] = undefined;
    }
}

/*
 * scrollLayerAmount() - this scrolls a specific <DIV> layer id by
 * a certain amount horizontally and vertically
 */
function scrollLayerAmount (id, verticalAmount, horizontalAmount)
{
    var obj = myGetElementById(id);
    var horizontalIncrement, verticalIncrement;
    var finalLeft, finalTop;
    var ds = "";

    if ( ! obj )
	return false;

    if ( horizontalAmount )
    {
	horizontalIncrement = parseInt (horizontalAmount / scrollSteps);

	curLeft = parseInt (obj.scrollLeft, 10);
	finalLeft = curLeft + horizontalAmount;

	ds += ("scroll hor " + obj.getAttribute("ID") +
	    " from " + obj.scrollLeft + " to " +
	    finalLeft);
    }

    if ( verticalAmount )
    {
	verticalIncrement = parseInt (verticalAmount / scrollSteps);

	curTop = parseInt (obj.scrollTop, 10);
	finalTop = curTop + verticalAmount;

	ds += ("scroll vert " + obj.getAttribute("ID") +
	    " from " + obj.scrollTop + " to " +
	    finalTop);
    }

//    debug (ds);

    if ( finalLeft != undefined || finalTop != undefined )
    {
	var cmd;

	// Stop any current scrolling
	if ( _scrollIntervalId && _scrollIntervalId[id] )
	{
	    clearInterval (_scrollIntervalId[id]);
	    _scrollIntervalId[id] = undefined;
	}
      
	cmd = "_scrollStep ('"+ id + "'," +
	    horizontalIncrement + "," + finalLeft + "," +
	    verticalIncrement + "," + finalTop +
	    ")";

	_scrollIntervalId[id] = setInterval (cmd, _scrollIntervalLength);
    }

    return false;
}
