var MENU_BAR_HEIGHT= 17;
var MENU_WIDTH= 80;

var base;

try
    {
    base= ( document.getElementsByTagName( 'base' ) )[ 0 ].href;
    }
catch( e )
    {
    base= '';
    }

var current_menu_index= 0;


// contains currently open menus
var all_open_menus= new Array();

var menu_open_loop= new Array();
var menu_close_loop= new Array();

var active_menu_count= 0;
var active_menus= new Array();

var menu_loop_handler;


function menuLoop()
    {
    
    var id;

    for( id in active_menus )
        {
        if( active_menus[ id ] )    // true - unfade
            unfade( id );
        else
            fade( id );
        }
  
    if( active_menu_count == 0 )
        clearInterval( menu_loop_handler );

    }


function fade( menu_id )
    {
    curr_op= parseFloat( document.getElementById( menu_id ).style.opacity );
    curr_op-= 0.1;
    if( curr_op >= 0 )
        {
        document.getElementById( menu_id ).style.opacity= curr_op.toString();
        document.getElementById( menu_id ).style.filter = 'alpha(opacity=' + curr_op*100 + ')';
        }
    else
        {
        delete active_menus[ menu_id ];
        document.getElementById( menu_id ).style.display= "none";
        }
    }



function hideMenu( menu )
    {
    if( menu.style.display == "none" )
        return;

    var menu_id= menu.id;

    if( active_menu_count == 0 )
        var menu_loop_handler= setInterval( "menuLoop();", 20 );

    if( !( active_menus[ menu_id ] ) )
        active_menu_count++;

    active_menus[ menu_id ]= false;      // true - opening, false - closing

    menu.style.opacity= 1.0;
    }



function unfade( menu_id )
    {
    var curr_op= parseFloat( document.getElementById( menu_id ).style.opacity );
    curr_op+= 0.1;
    if( curr_op <= 1 )
        {
        document.getElementById( menu_id ).style.opacity= curr_op.toString();

        document.getElementById( menu_id ).style.filter = 'alpha(opacity=' + curr_op*100 + ')';
        }
    else
        {
        delete active_menus[ menu_id ];
        document.getElementById( menu_id ).style.display= "block";
        }
    }



function showMenu( menu )
    {

    if( menu.style.display == "block" )
        return;

    var menu_id= menu.id;

    if( active_menu_count == 0 )
        var menu_loop_handler= setInterval( "menuLoop();", 40 );

    if( !( active_menus[ menu_id ] ) )
        active_menu_count++;

    active_menus[ menu_id ]= true;      // true - opening, false - closing

    menu.style.opacity= 0.0;
    menu.style.display= "block";
    }










var is_hiding= false;
var req_hide_handle;


var cancel_handle;


function hideAll()
    {
    for( itr= 0; itr < all_open_menus.length; itr++ )
        hideMenu( document.getElementById( all_open_menus[ itr ] ) );

    all_open_menus= new Array();
    }



function requestHide()
    {

    if( !is_hiding )
       {
       req_hide_handle= setTimeout( hideAll, 500 );
       is_hiding= true;
       }
    }



function cancelRequestHide()
    {
    if( is_hiding )
        {
        clearTimeout( req_hide_handle )
        is_hiding= false;
        }
    }



function getElemPos( obj )
    {
    var curleft= 0;
    var curtop = 0;

    if( obj.offsetParent )
        {
        do
            {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
            }
        while( obj = obj.offsetParent );
        }
    return [curleft,curtop];
    }



function foldInactiveMenus( active_menu_id )
    {

    var is_active= new Array();
    is_active[ active_menu_id ]= true;
    
    var menu_elem= document.getElementById( active_menu_id ).parentNode;

    while( menu_elem.nodeName != "BODY" ) // change it later to match valid root menu elem 
                                        // or better - match element ids - above root
        {
        if( menu_elem.id )
            {
            is_active[ menu_elem.id ]= true;
            }
        menu_elem= menu_elem.parentNode;
        }

    var new_open_table= new Array();

    
    for( itr= 0; itr < all_open_menus.length; itr++ )
        {
        if( is_active[ all_open_menus[ itr ] ] )
            {
            new_open_table[ new_open_table.length ]= all_open_menus[ itr ];
            }
        else
            {
            hideMenu( document.getElementById( all_open_menus[ itr ] ) );
            }
        }
    all_open_menus= new_open_table;
    }




function unfoldMenu( event )
    {
    var doc_x;
    var doc_y;
    var new_width= -1;
    
    var tid;
    // we have to fold all not connected menus
    
    cancelRequestHide();
    var target;


    if( event )
        {
        
        target= event.target;
    

        // falling back to the real event sender
        while( !( ( target.id ) && ( target.id.substr( 0, 9 ) == "menulink_" ) ) )
            target= target.parentNode;
    
        // if it's the first level of our menu
        if( target.nodeName == "LI" )
            {
            // we have to go down to the body recursively
            offs= getElemPos( target );
            doc_x= offs[ 0 ];
            doc_y= offs[ 1 ]+MENU_BAR_HEIGHT; // hardcoded height of menu bar
            new_width= target.clientWidth;
            }
        else
            {
            
            // else parent has already calculated offset
            doc_x= target.offsetLeft+MENU_WIDTH;
            doc_y= target.offsetTop;
            }
      //  all_open_menus[ all_open_menus.length ]= target.id;

        // target id data
        tid= target.id.split( "_" );
        }
    else              // internet exploder
        {
        event= window.event;

        target= event.srcElement;

        xoff= 0;
        yoff= 0;
        
        while( !( ( target.id ) && ( target.id.substr( 0, 9 ) == "menulink_" ) ) )
            {
            xoff+= target.offsetLeft;
            yoff+= target.offsetTop;

            target= target.parentNode;
            
            }


        if( target.nodeName == "LI" ) // main node
            {
            
            doc_x= event.clientX - event.offsetX-xoff;
            doc_y= event.clientY - event.offsetY+MENU_BAR_HEIGHT-yoff;
            new_width= target.clientWidth;
            }
        else // submenu
            {
            
            doc_x= event.clientX - event.offsetX-xoff+MENU_WIDTH;
            doc_y= event.clientY - event.offsetY+yoff;
            
            }

        doc_y+= document.body.scrollTop;
        doc_x+= document.body.scrollLeft;

        tid= target.id.split( "_" );
        }
    
    target_menu= "menulist_"+tid[ 1 ];
    

    var menu= document.getElementById( target_menu );

    all_open_menus.push( target_menu );


    foldInactiveMenus( target_menu );

    showMenu( menu );
    menu.style.left= String( doc_x )+"px";

    menu.style.top= String( doc_y )+"px";
    menu.style.zIndex= "150";
    
    if( new_width != -1 )
        menu.style.width= new_width+"px";
    }



function addMenuItem( mdiv, label, data )
    {
    var itemwrapper= document.createElement( "div" );
    
    itemwrapper.onmouseover= requestHide;

    var itemlink= document.createElement( "a" );

    if( typeof( data ) == 'object' )
        {
        itemlink.href= "javascript:void(0)";
        itemlink.id= "menulink_"+current_menu_index;
        itemlink.onmouseover= unfoldMenu;
        
        createMenu( itemwrapper, data, current_menu_index++ );
        }
    else
        itemlink.href= base+data;
    
    itemlink.appendChild( document.createTextNode( menu_labels[ label ] ) );
    itemwrapper.appendChild( itemlink );
    

    mdiv.appendChild( itemwrapper );
    }



function createMenu( mroot, item_data, data_id )
    {

    var mdiv= document.createElement( "div" );
    mdiv.id= "menulist_"+data_id;
    mdiv.className= "menu-list";

    mdiv.onmouseover= cancelRequestHide;
    mdiv.onmouseout= requestHide;

    for( item_label in item_data )
        {
        addMenuItem( mdiv, item_label, item_data[ item_label ] );
        }
   

    mdiv.style.display= "none";
    mroot.appendChild( mdiv );
    
     


    }



function appendMenus()
    {
    nav_bar= document.getElementById( "bar_nav" );

   
    var menu_list= nav_bar.firstChild;  // list of menu elements
    var r_nodes= 0;                      // real nodes found in menu


    var itc= menu_list.childNodes.length;

    current_menu_index= itc;    // before starting recursive calls we make sure that
                                // we have enough free ids to hande our menu

    //document.body.childNodes[ 0 ].onmouseover= requestHide;

    for( itr= 0; itr < itc; itr++ )
        {
        if( menu_list.childNodes[ itr ].nodeName == "LI" )      // real nodes - real list elements
            {
            if( menu_list.childNodes[ itr ].firstChild.innerHTML == "STR GŁÓWNA" )
                continue;
            
            if( menu_list.childNodes[ itr ].firstChild.innerHTML == "ZASTOSOWANIE" )
                return false;
            
            
            // if we have data for current element we handle it
            if( main_menu[ String( r_nodes ) ] )
                {

                // create menu containing our data
                createMenu( document.body, main_menu[ String( r_nodes ) ], r_nodes );

                // set proper id
                menu_list.childNodes[ itr ].id= "menulink_"+r_nodes;

                // and bind onmouseover event handles
                menu_list.childNodes[ itr ].onmouseover= unfoldMenu;
                menu_list.childNodes[ itr ].onmouseout= requestHide;
                }

            r_nodes++;
            }

        }

    }

