/** $Id:$ */

/* Scripts used on SAS-Mod.net. Put to a separate file to save bandwidth. */

/**
* Changes cell layout and mouse cursor on cell enter
*/
function menuIn( cell ) {
    cell.className='lrBlock';
    // as a little gimmick, display the link in the status bar
    var link = findFirstLink( cell );
    window.defaultStatus = link.getAttribute( "href" );
}

/**
* Changes cell layout and mouse cursor on cell exit
*/
function menuOut( cell ) {
    cell.className='noBlock';
    window.defaultStatus = "";
}

/**
* Does a document change if the menu cell is clicked.
*/
function menuClick( cell ) {
    // find the first link below the cell
    var link = findFirstLink( cell );
    var linkTarget = link.getAttribute("target");
    // open a new window, if target is _blank
    if ( linkTarget == "_blank" ) {
        window.open( link.getAttribute("href") );
    }
    else {
        window.location.href=link.getAttribute("href");
    }
}
/**
* Finds the first link node  in the subtree below the given node.
* Does DFS.
*/
function findFirstLink( node ) {
    // do a depth-first-search below the node
    var size = node.childNodes.length;
    for ( var i = 0; i < size; i++ ) {
        var aNode = node.childNodes[i];
        // XXX: Does nodeName return upper or lower case ???
        if (  aNode.nodeName == "A" || aNode.nodeName == "a" ) {
            return aNode; // found a link, return it
        }
        else {
            var aResult = findFirstLink( aNode );
            if ( aResult != null ) {
                return aResult;
            }
        }
        // no a-node in sub-tree, try next child
    }
    // no node found at all
    return null;
}

/**
* Attaches the mouseover stuff on the links in the menu.
*/
function attachMouseOvers() {
    
}


function attachListeners() {
    // attaches the listeners to the elements to be scripted
    var menuNode;
    menuNode = document.getElementById( "menunode" );
    // in the menu
    var tdNodes = menuNode.getElementsByTagName( "td");
    for( var i = 0; i < tdNodes.length; i++ ) {
        // for some odd reasone getAttribute does not work in IE
        var attrNode = tdNodes[i].getAttributeNode("class");

        if ( attrNode && attrNode.nodeValue == "noBlock" ){
            tdNodes[i].onmouseover = function(){
                menuIn( this );
            };
            tdNodes[i].onmouseout = function() {
                menuOut( this );
            };
            tdNodes[i].onclick = function() {
                menuClick( this );
            };
            // and the link nodes
            var linkNode = tdNodes[i].getElementsByTagName( "a")[0];
            linkNode.onclick = function() {
                return false;
            };
        }
    }
}

window.onload = function() {
    attachListeners();
};