var g_us_mom_showTr = null;
var g_us_mom_timeOutId = null;
var g_us_mom_currMainItemId = null;
var g_mom_delay = 100;

function us_mom_showSubItems( mainItemId )
{
    us_mom_clearTimeOut();
    us_mom_hideSubItems();
    g_us_mom_currMainItemId = mainItemId;
    var dummyTr = document.getElementById( "us_mom_dummy_tr" );
    g_us_mom_showTr = document.getElementById( "us_mom_sub_tr_" + mainItemId );

    if ( g_us_mom_showTr == null )
    {
        dummyTr.style.display = "";
    }
    else
    {
        dummyTr.style.display = "none";
        g_us_mom_showTr.style.display = "";
    }

    var currTd = document.getElementById( "us_mom_main_item_" + mainItemId );
    currTd.oldClass  = currTd.className; 
    currTd.className = currTd.attributes[ "overClass" ].value;
}

function us_mom_hideSubItems()
{
    if ( g_us_mom_showTr != null )
    {
        g_us_mom_showTr.style.display = "none";
        g_us_mom_showTr = null;
        var dummyTr = document.getElementById( "us_mom_dummy_tr" );
        dummyTr.style.display = "";
    }

    if ( g_us_mom_currMainItemId != null )
    {
        var currTd = document.getElementById( "us_mom_main_item_" + g_us_mom_currMainItemId );
        currTd.className = currTd.oldClass;
    }
}

function us_mom_clearTimeOut()
{
    if ( g_us_mom_timeOutId != null )
    {
        window.clearTimeout( g_us_mom_timeOutId );
        g_us_mom_timeOutId = null;
    }
}

function us_mom_delayedHideSubItems()
{
    us_mom_clearTimeOut();
    g_us_mom_timeOutId = window.setTimeout( "us_mom_hideSubItems();",
                                            g_mom_delay );
}

function valueOrDefault( value, defaultValue )
{
    if ( !value )
    {
        return defaultValue;
    }

    return value;
}

function MouseOverMenuSeparator( html )
{
    this.m_html = html;

    this.toString = function()
    {
        return '<td>' +
               this.m_html +
               '</td>';
    }
}

function MouseOverMenuSubItem( name, link, classNameCell )
{
    this.m_name = name;
    this.m_link = link;
    this.m_classNameCell = classNameCell;

    this.toString = function()
    {
        return '<td>' +
               '<a href="' + valueOrDefault( this.m_link, "#" ) +
               '" class="' + valueOrDefault( this.m_classNameCell,
                                             "us_mom_main_sub_item_link" ) +
               '">' + this.m_name + '</a></td>';
    }
}

function MouseOverMenuItem( name,
                            link,
                            subItemsCollection,
                            className,
                            overClass )
{
    this.m_name = name;
    this.m_link = link;
    this.m_subItemsCollection = subItemsCollection;
    this.m_className = className;
    this.m_overClass = overClass;

    this.toString = function( id )
    {
        return '<td class="' + valueOrDefault( this.m_className,
                                               "us_mom_main_item" ) +
               '" id="us_mom_main_item_' + id + '" ' +
               'onmouseover="us_mom_showSubItems( ' + id + ' );" ' +
               'onmouseout="us_mom_delayedHideSubItems();" ' +
               'overClass="' + valueOrDefault( this.m_overClass,
                                               "us_mom_main_item_over" ) + '">' +
               '<a href="' + valueOrDefault( this.m_link,
                                             "#" ) +
               '">' + this.m_name + '</a></td>';
    }
}

function MouseOverMenu( menuItemsCollection,
                        dir,
                        classNameMainTable )
{
    this.m_menuItemsCollection = menuItemsCollection;
    this.m_dir = dir;
    this.m_classNameMainTable = classNameMainTable; 

    this.toString = function()
    {
        var mainHtmlStr = '<table border="0" dir="' + this.m_dir +
                          '" class="' +
                          valueOrDefault( this.m_classNameMainTable,
                                          "us_mom_main_table" ) +
                          '" cellspacing="0" cellpadding="0"><tr><td>' +
                          '<table cellspacing="0" cellpadding="0"><tr>';

        var secondHtml = '';

        for ( var i = 0;
              i < this.m_menuItemsCollection.length;
              i++ )
        {
            var currItem = this.m_menuItemsCollection[ i ];
            mainHtmlStr += currItem.toString( i );
            if ( currItem.m_subItemsCollection )
            {
                secondHtml += '<tr class="us_mom_sub_tr" id="us_mom_sub_tr_' + i +
                              '" style="display: none;" ' +
                              'onmouseover="us_mom_clearTimeOut();" ' +
                              'onmouseout="us_mom_delayedHideSubItems();">' +
                              '<td><table cellspacing="0" cellpadding="0"><tr>';

                for ( var j = 0;
                      j < currItem.m_subItemsCollection.length;
                      j++ )
                {
                    secondHtml += currItem.m_subItemsCollection[ j ].toString();
                }

                secondHtml += '</tr></table></td></tr>';
            }
        }

        mainHtmlStr += '</tr></table></td></tr>' +
                       '<tr id="us_mom_dummy_tr" class="us_mom_dummy_sub_tr">' +
                       '<td><table cellspacing="0" cellpadding="0"><tr><td>' +
                       '&nbsp;</td></tr></table></td>' +
                       '</tr>' +
                       secondHtml +
                       '</table>';

        return mainHtmlStr; 
    }
}

