// $Id: full.js,v 1.14 2003/01/16 12:49:01 enket Exp $
//
// This JS module contains code common to all full content views.

// FUNCTION: doJumpTo()
// This procedure jumps to the JUMP anchor if it is present in the document.
function doJumpTo() {
    var myDoc = document;
    var myWin = window;

    // If we have a JUMP anchor, this is where we'll jump to.  Then recalculate
    // the idxCurrent variable. If no JUMP anchor is found, we simply let
    // idxCurrent be 0 but do set the highlight.
    if (doJumpToId('JUMP')) {
        doJumped('JUMP');
    } else {
        doElemHighlight();
    }
}


// FUNCTION: doElemHighlight()
// Highlight the current element.
function doElemHighlight() {
    var myDoc = document;
    var myElem;

    // NS4
    if (!myDoc.getElementById) {
        return;
    }

    // Remove old highlight.
    myElem = myDoc.getElementById('buttons-' + myDoc.idxCurrentHighlight);
    if (myElem) {
        myElem.className = 'buttonflow';
    }

    // Add new highlight.
    myDoc.idxCurrentHighlight = myDoc.idxCurrent;

    myElem = myDoc.getElementById('buttons-' + myDoc.idxCurrentHighlight);
    if (myElem) {
        myElem.className = 'buttonflowcurrent';
    }
}


// FUNCTION: doMove(inMove)
// Move forwards or backwards through our index anchors.
function doElemMove(inMove) {
    if (checkLoaded()) {
        var myDoc = document;
        var myWin = window;
        var myIdx = myDoc.idxCurrent;

        // NS4
        if (!myDoc.getElementsByTagName) {
            return;
        }

        var myAnchors = myDoc.getElementsByTagName('A');

        var myStep;
        if (inMove < 0) {
            myStep = -1;
        } else {
            myStep = 1;
        }

        // Move in the right direction in steps of one, and clip at
        // either the top or the bottom.
        while (inMove != 0) {
            var myNewIdx = myIdx + myStep;
            inMove = inMove - myStep;

            if (myAnchors['index-' + myNewIdx]) {
                myIdx = myNewIdx;
            } else {
                break;
            }
        }

        myDoc.idxCurrent = myIdx;

        // Jump to IDs instead of location.hash. It is much more reliable on
        // the various browsers.
        doJumpToId('index-' + myIdx);

        // Make sure our new elem is highlighted.
        doElemHighlight();
    }

    return;
}


function doElemBack() {
    return doElemMove(-1);
}


function doElemDoubleBack() {
    return doElemMove(-10);
}


function doElemForward() {
    return doElemMove(1);
}


function doElemDoubleForward() {
    return doElemMove(10);
}


// FUNCTION: doJumped(inAnchor)
// This function is being called whenever a jump to an anchor
// has taken place. 
function doJumped(inAnchor) {
    var myDoc = document;

    // NS4
    if (!myDoc.getElementsByName) {
        return;
    }

    var myFound = false;
    var myIdx = 0;

    // Try to find the closest 'index-' anchor on the left of the anchor
    // that was given.
    var myElements = myDoc.getElementsByName(inAnchor);

    if (myElements && myElements[0]) {
        var myCurrent = myElements[0];

        while (!myFound) {
            var myPrevious = myCurrent.previousSibling;

            if (myPrevious){
                myCurrent = myPrevious;
            } else {
                myPrevious = myCurrent.parentNode;
                if (myPrevious) {
                    myCurrent = myPrevious;
                } else {
                    break;
                }
            }

            if (myCurrent.nodeType == 1 && myCurrent.tagName == 'A') {
                var myMatch = myCurrent.name.match(/^index-(\d+)/);
                if (myMatch && myMatch.length > 0) {
                    myIdx = parseInt(myMatch[1]);
                    myFound = true;
                }
            }
        }
    }

    // If such a node was found highlight.
    if (myFound) {
        myDoc.idxCurrent = myIdx;
        doElemHighlight();
    } else {
        checkLoaded();
    }

    return;
}


// FUNCTION: doExtref(inVerwijzingRef, inLabelRef)
function doExtref(inVerwijzingRef, inLabelRef) {
    var myWin = window;

    var myUrl = document.go_extref_url;
    myUrl = myUrl.replace(/VERWIJZING/, inVerwijzingRef);
    myWin.parent.location.replace(myUrl);

    return;
}



