
//
// override on calling page
//

var SCROLL_DISTANCES = new Array();

var DEFAULT_DELAY = 4;
var DEFAULT_MOVE = 8;
var SPEED_INC = 80; // larger = more pronounced slowdown
var OUTER_RANGE_SPEED_CHANGE_BEGIN = 100;

//
// ingore below here
//

var SCROLL_DIR_UP = 0;
var SCROLL_DIR_DOWN = 1;

var BROWSER_OK = (document.getElementById) ? true : false;
var BROWSER_OPERA = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

var curPos = 0;
var move = 0;
var curMoveDistance = 0;
var speedPointer = 0;
var stopAt = SCROLL_DISTANCES[0];
var curMoving = false;
var delay = DEFAULT_DELAY;
var curDir = SCROLL_DIR_DOWN;
var speeds = new Array();

// gets the script ready to scroll all the way back to the start
function scrollFrameBackToStart () {
    if ((curMoving) || (! BROWSER_OK)) {
        // MAY EXIT THIS BLOCK
        return;
    }

    if (move > 0) {
        // go back
        move = 1;
        // show/hide controls
        showHideNav("scrollLeftNav", "none");
        showHideNav("scrollRightNav", "none");
        curDir = SCROLL_DIR_UP;
        curMoveDistance = SCROLL_DISTANCES[move];
        stopAt = SCROLL_DISTANCES[0];
        speedPointer = 0;
        num = DEFAULT_DELAY;
        scrollFrame();
    }
        // restore controls
        showHideNav("scrollLeftNav", "none");
        showHideNav("scrollRightNav", "block");
}

// gets the script ready to scroll left
function scrollFrameUp () {
    if ((curMoving) || (! BROWSER_OK)) {
        // MAY EXIT THIS BLOCK
        return;
    }

    // reset
    resetScrollVars(SCROLL_DIR_UP);

    if (move == 0) {
        // at the start
        showHideNav("scrollLeftNav", "none");
    } else if (move == (SCROLL_DISTANCES.length - 2)) {
        // make sure it's available
        showHideNav("scrollRightNav", "block");
    }

    scrollFrame();
}

// gets the script ready to scroll right
function scrollFrameDown () {
    if ((curMoving) || (! BROWSER_OK)) {
        // MAY EXIT THIS BLOCK
        return;
    }

    // reset params
    resetScrollVars(SCROLL_DIR_DOWN);

    if (move == (SCROLL_DISTANCES.length - 1)) {
        // at the end
        showHideNav("scrollRightNav", "none");
    }

    if (move < SCROLL_DISTANCES.length) {
        showHideNav("scrollLeftNav", "block");
        scrollFrame();
    }
}

// the guts of this mess
function scrollFrame () {   
    // adjust current position up or down
    curPos += (curDir == SCROLL_DIR_DOWN) ? DEFAULT_MOVE : (DEFAULT_MOVE * -1);
    
    // start the slow down in movement
    if (((curPos + OUTER_RANGE_SPEED_CHANGE_BEGIN) >= stopAt) && (curDir == SCROLL_DIR_DOWN)) {
        num = Math.floor(Math.sqrt(Math.pow(DEFAULT_MOVE, 2) + Math.pow(((stopAt - SPEED_INC) - curPos), 2)));
        // record the calculated speed changes
 	if (move == 1) {
            // it's pretty lazy to do this move test as it will
	    // populate every time the user hits 1 - sigh
            speeds.push(num);
        }
    } else if (((curPos - OUTER_RANGE_SPEED_CHANGE_BEGIN) <= stopAt) && (curDir == SCROLL_DIR_UP)) {
        // slowing down - use the values stored from the first right move
        num = speeds[speedPointer];
        speedPointer += (speedPointer < speeds.length) ? 1 : 0;
    }
    
    if (((curPos >= stopAt) && (curDir == SCROLL_DIR_UP)) || ((curPos <= stopAt) && (curDir == SCROLL_DIR_DOWN))) {
        // move it
        this.sf.scroll(0, curPos);
        window.setTimeout('scrollFrame()', num);
	curMoving = true;
    } else {
        // stop moving and in case it went over in count, reset to the proper place
        curPos = sumToThisPoint(move);
	curMoving = false;
    }
}

// how far from the top are we currently?
function sumToThisPoint (p) {
    var rhett = 0;
    for (i=0; i < p; i++) {
        rhett += SCROLL_DISTANCES[i];
    }
    return rhett;
}

// shows or hides a nav button
function showHideNav (item, status) {
    if (! BROWSER_OPERA) {
        // just don't disable these in opera as it seems
	// fussy about this action - opera users will
	// just see the nav all the time
        document.getElementById(item).style.display = status;
    }
}


// reset all the scroll vars
function resetScrollVars (dir) {
    curDir = dir;
    curMoveDistance = SCROLL_DISTANCES[move];

    if (dir == SCROLL_DIR_UP) {
        stopAt = sumToThisPoint((move - 1));
        // reset the pointer for the speed change array
        speedPointer = 0;
        move--;
    } else {
        stopAt = sumToThisPoint((move + 1));
        move++;
    }

    num = DEFAULT_DELAY;
}

