/**
 * PullRight Menu
 * Author : Alexandre Gorobets [alexandre@peterstar.ru]
 * Version: v0.1
 * Updated: 08/2004
 */


/********************************************************************************
 * PullDown menu constructor
 */
function PullDownMenu(pm_name, pm_hide_timeout)
{
    this.name          = pm_name;
    this.hide_timeout  = /*pm_hide_timeout || */1500;

    this._top_menu     = new Array();
    this._all_items    = new Array();
    this._active_items = new Array();
    this._dividers     = new Array();
    this._hide_timer   = null;
        
    return this;
} 


/********************************************************************************
 * Add new item to menu
 */
PullDownMenu.prototype.addItem = function(pm_id, pm_parent, pm_label, pm_url) 
{
    if ( this._all_items[ pm_id ] ) _error('Item with id = "' + pm_id + '" is already exists in menu "' + this.name + '"!');
    
    var item = new Array();
    item['id']        = pm_id;
    item['label']     = pm_label;
    item['url']       = pm_url;
    item['items']     = new Array();
    item['parent']    = pm_parent;
    item['is_active'] = false;

    this._all_items[ pm_id ] = item;
    
    if (pm_parent)
    {
        if ( !this._all_items[ pm_parent ] ) _error('Item with id = "' + pm_parent + '" not found in menu "' + this.name + '"!');
        
        var items = this._all_items[ pm_parent ]['items'];
        item['level']  = this._all_items[ pm_parent ]['level'] + 1;
        item['index']  = items.length;
        items[ item['index'] ] = item;
    }
    else
    {
        item['level']  = 1;
        item['index']  = this._top_menu.length;
        this._top_menu[ item['index'] ] = item;
    }
}


/********************************************************************************
 * Add divider for level in given position
 */
PullDownMenu.prototype.addDivider = function(pm_level, pm_position) 
{
    if ( !this._dividers[pm_level] ) this._dividers[pm_level] = new Array();

    this._dividers[pm_level][ pm_position || 'between' ] = true;
}


/********************************************************************************
 * Change state for menu with given id
 *
PullDownMenu.prototype.changeState = function(pm_id) 
{
    var item = this._all_items[ pm_id ];
       
    if ( item['is_opened'] )
    {
        _hide( item );

        this._active_items[ item['level'] ] = null;
    }
    else
    {
        if (this.autocollapse && this._active_items[ item['level'] ] ) _hide( this._active_items[ item['level'] ] );
        
        _show( item );
        
        this._active_items[ item['level'] ] = item;
    }
}
*/

/********************************************************************************
 * Highlight / Dehighlight menu item with given id
 *
PullDownMenu.prototype.highlightItem = function(pm_flag, pm_id) 
{
    var item = this._all_items[ pm_id ];

    //if ( item['is_active'] ) return;

    item['is_active'] = pm_flag;
    
    var menu = document.getElementById('menu' + item['id']);
    
    menu.className = 'menuLvl' + item['level'] + (pm_flag ? 'active' : '');
}
*/

PullDownMenu.prototype.highlightItem = function(pm_flag, pm_id) 
{
    var item = this._all_items[ pm_id ];
    var menu = document.getElementById('menu' + item['id']);

    if ( item['is_active'] == pm_flag )
    {
         return;
    }
    else
    {
        item['is_active'] = pm_flag;    
    }

    
    if ( pm_flag )
    {
        if ( this._hide_timer ) window.clearTimeout( this._hide_timer );

        if ( old_active = this._active_items[ item['level'] ] )
        {
            if ( old_active['id'] != item['id'] ) 
                this._hide( old_active );
        }            
        
        if ( item['items'].length )
        {
            this._show( item );
        }
        
        this._active_items[ item['level'] ] = item;

        menu.className = 'menuLvl' + item['level'] + 'active';
    }
    else
    {

        this._hide_timer = window.setTimeout( this.name + '._hideAll()', this.hide_timeout );

        if ( !item['items'].length )
        {
            menu.className = 'menuLvl' + item['level'];
        }
    }

    
    
//    menu.className = 'menuLvl' + item['level'] + ( pm_flag ? 'active' : '' );
}










/********************************************************************************
 *
 *
PullDownMenu.prototype.showSubmenu = function(pm_id) 
{
    if ( this._hide_timer ) window.clearTimeout( this._hide_timer );
    
    var item = this._all_items[ pm_id ];

    if ( item['is_active'] ) return;

    this.highlightItem(true, pm_id);

    var menu = document.getElementById('menu' + item['id']);
    
    var submenu = document.getElementById('submenu' + item['id']);
    
    //submenu.style.left = menu.offsetWidth - menu.offsetHeight / 2;
    submenu.style.top  = menu.offsetTop + menu.offsetHeight / 2;

    submenu.style.visibility = 'visible';

    this._active_items.push( item );
}


/********************************************************************************
 *
 *
PullDownMenu.prototype.hideSubmenu = function(pm_id) 
{
/*
    var item = this._all_items[ pm_id ];

    if ( !item['is_active'] ) return;

    this.highlightItem(false, pm_id);
    
    var menu = document.getElementById('menu' + item['id']);

    var submenu = document.getElementById('submenu' + item['id']);
    
    submenu.style.visibility = 'hidden';

    x = window.setTimeout( this.name + '.showSubmenu(\'' + pm_id + '\')', 1000);
    alert(x);
*

    this._hide_timer = window.setTimeout( this.name + '.hideAllSubmenu()', this.hide_timeout );
}
*/





/********************************************************************************
 * Write menu in the document
 */
PullDownMenu.prototype.write = function() 
{
    document.writeln('<div id="' + this.name + '" class="menuLvl1">');
        
    for (var i = 0; i < this._top_menu.length; i++)
    {
        this._writeSubMenu( 1, this._top_menu[i], (i == this._top_menu.length - 1) );
    }
        
    //document.getElementById('menu' + this._top_menu[0]['id']).style.borderTopWidth = 0; 
    
    document.writeln('</div>');
}


/********************************************************************************
 * Write menu item and it's submenu
 */
PullDownMenu.prototype._writeSubMenu = function(pm_level, pm_item, pm_is_last)
{
    var items = pm_item['items'];

    if (items.length)
    {
        document.write('<table id="menu' + pm_item['id'] + '" border="0" cellspacing="0" cellpadding="0" width="100%" class="menuLvl' + pm_level + '"><tr><td class="haveSubmenu"' + (pm_is_last ? ' id="lastItem"' : '') + ' >');
        document.write('<a href="' + pm_item['url'] + '"' +
//                          ' onclick="' + this.name + '.changeState(\'' + pm_item['id'] + '\')"' +
//                          ' onmouseover="' + this.name + '.showSubmenu(\'' + pm_item['id'] + '\')"' +
//                          ' onmouseout="'  + this.name + '.hideSubmenu(\'' + pm_item['id'] + '\')">' + pm_item['label'] + '</a>');
                          ' onmouseover="' + this.name + '.highlightItem(true, \'' + pm_item['id'] + '\')"' +
                          ' onmouseout="'  + this.name + '.highlightItem(false, \'' + pm_item['id'] + '\')">' + pm_item['label'] + '</a>');

        document.write('</td></tr></table>');

        document.write('<div id="submenu' + pm_item['id'] + '" class="menuLvl' + (pm_level + 1) + '" style="visibility: hidden; position: absolute; z-index: ' + pm_level + ' ">');
/*
        if ( this._dividers[pm_level] && this._dividers[pm_level]['before'] )
        {
            document.write('<table border="0" cellspacing="0" cellpadding="0" width="100%" class="menuDividerLvl' + pm_level + 'BeforeSubmenu"><tr><td>');
            document.write('</td></tr></table>');
        }
*/        
        for (var i = 0; i < items.length; i++)
        {
            this._writeSubMenu( pm_level + 1, items[i], (i == items.length - 1) );
        }
/*
        if ( this._dividers[pm_level] && this._dividers[pm_level]['after'] )
        {
            document.write('<table border="0" cellspacing="0" cellpadding="0" width="100%" class="menuDividerLvl' + pm_level + 'AfterSubmenu"><tr><td>');
            document.write('</td></tr></table>');
        }
*/
        document.writeln('</div>');

        //if (pm_level == 1 || !pm_is_last)
        if ( !pm_is_last && this._dividers[pm_level] && this._dividers[pm_level]['between'] )
        {
            document.write('<table border="0" cellspacing="0" cellpadding="0" width="100%" class="menuDividerLvl' + pm_level + '"><tr><td>');
            document.write('</td></tr></table>');
        }
    }
    else
    {
        document.write('<table id="menu' + pm_item['id'] + '" border="0" cellspacing="0" cellpadding="0" width="100%" class="menuLvl' + pm_level + '"><tr><td' + (pm_is_last ? ' class="lastItem"' : '') + '>');
        document.write('<a href="' + pm_item['url'] + '"' +
                          ' onmouseover="' + this.name + '.highlightItem(true, \'' + pm_item['id'] + '\')"' +
                          ' onmouseout="'  + this.name + '.highlightItem(false, \'' + pm_item['id'] + '\')">' + pm_item['label'] + '</a>');
//                          ' onmouseover="' + this.name + '.showSubmenu(\'' + pm_item['id'] + '\')"' +
//                          ' onmouseout="'  + this.name + '.hideSubmenu(\'' + pm_item['id'] + '\')">' + pm_item['label'] + '</a>');

        document.write('</td></tr></table>');

        //if (pm_level == 1 || !pm_is_last)
        if ( !pm_is_last && this._dividers[pm_level] && this._dividers[pm_level]['between'] )
        {
            document.write('<table border="0" cellspacing="0" cellpadding="0" width="100%" class="menuDividerLvl' + pm_level + '"><tr><td>');
            document.write('</td></tr></table>');
        }
    }
}


/********************************************************************************
 * Show submenu
 */
PullDownMenu.prototype._show = function(pm_item) 
{ 
    var menu    = document.getElementById('menu'    + pm_item['id']);
    var submenu = document.getElementById('submenu' + pm_item['id']);
    
    submenu.style.top        = menu.offsetTop + menu.offsetHeight / 2;
    submenu.style.visibility = 'visible';
}


/********************************************************************************
 * Hide submenu
 */
PullDownMenu.prototype._hide = function(pm_item) 
{
    var menu    = document.getElementById('menu'    + pm_item['id']);
    var submenu = document.getElementById('submenu' + pm_item['id']);

    menu.className = 'menuLvl' + pm_item['level'];

    if ( submenu ) 
        submenu.style.visibility = 'hidden';

    pm_item['is_active'] = false;
    
    this._active_items[ pm_item['level'] ] = null;

    if ( next_item = this._active_items[ pm_item['level'] + 1 ] )
        this._hide( next_item );
    
/*    

//    
    if ( pm_item['items'].length )
    {
        
        
    }

    pm_item['is_opened'] = false;
        
    var menu = document.getElementById('menu' + pm_item['id']);

    menu.className = 'menuLvl' + pm_item['level'];

    if (pm_item['items'].length)
    {
        var submenu = document.getElementById('submenu' + pm_item['id']);
        submenu.style.visibility = 'hidden';
    }
*/    
}


/********************************************************************************
 *
 */
PullDownMenu.prototype._hideAll = function() 
{
    if ( this._active_items[1] ) 
        this._hide( this._active_items[1] )
/*
    while ( this._active_items.length )
    {
        this._hide( this._active_items.pop() )
    }
/*    
    for ( var i = this._active_items.length - 1; i >= 0; i-- )
    {
        this._hide( this._active_items[i] )
    }
*/    
}



/********************************************************************************
 * Error handler
 */
function _error(pm_text) 
{
    alert(pm_text);
}
