

if( isUndefinedOrNull(Widget) ) {
    var Widget = {};
}

Widget._MenuItem = function( menu, activator, menu_item, parent )
{
    this.menu = menu;
    this.activator = activator;
    this.menu_item = menu_item;
    this.parent = parent;
    this.update_position();
    this.timeout = null;
    this.childs = []

    if( this.activator ){
        connect( this.activator, 'onmouseenter', this, this.onmouseenter_activator );
        if( ! this.parent )
            connect( this.activator, 'onmouseleave', this, this.remove_root_style );
    }
    if( this.menu_item ){
        connect( this.menu_item, 'onmouseenter', this, this.abort_timeout );
        connect( this.menu_item, 'onmouseleave', this, this.onmouseleave_menu_item );
    }
    if( this.parent ){
        this.parent.childs.push( this );
    }
}
Widget._MenuItem.prototype = {
    update_position: function() {
        if( ! this.activator )
            return;
        if( ! this.menu_item )
            return;
        if( this.parent )
            showElement(this.parent.menu_item);
        var r= elementPosition(this.activator).x;
        var t = elementPosition(this.activator).y + elementDimensions(this.activator).h;
        if( this.parent )
            hideElement(this.parent.menu_item);
        // logDebug( r, t );
        setElementPosition( this.menu_item, {x:r, y:t} );
    },

    close_all: function(recurse)
    {
        for( var i = 0; i < this.childs.length; i ++ ){
            var item = this.childs[i];
            item.close();
        }
    },

    close: function(recurse)
    {
        if( this.menu_item ){
            hideElement( this.menu_item );
        }
        if( this.activator )
            removeElementClass( this.activator, 'opened' );
        if( recurse ){
            for(var i = 0; i < this.childs.length; i ++ ){
                this.childs[i].close(recurse);
            }
        }
    },

    open: function()
    {
        if( this.menu_item )
            showElement( this.menu_item );
        if( this.activator )
            addElementClass( this.activator, 'opened' );
        this.update_position();
    },

    abort_timeout: function()
    {
        if( this.timeout )
            clearTimeout(this.timeout);
        this.timeout = null;
        if( this.parent ){
            this.parent.abort_timeout();
        }
    },

    onmouseenter_activator: function(e)
    {
        if( this.parent ) {
            this.parent.close_all(false);
        }
        else {
            this.menu.close_all();
        }
        this.open();
    },

    onmouseleave_menu_item: function(e)
    {
        this.timeout = setTimeout( bind(function(){this.close(true);}, this), 200 );
    },
    
    remove_root_style: function(e)
    {
        if( this.activator ){
            removeElementClass( this.activator, 'opened' );
        }
    }
}

Widget.Menu = function()
{
    this.items = []
}

Widget.Menu.prototype = {
    add: function( activator, menu_item, options ){
        activator = $(activator);
        if(menu_item)
            menu_item = $(menu_item);
        if( isUndefinedOrNull( options ) )
            options = {}
        var parent = null;
        if( ! isUndefinedOrNull( options['parent'] ) ){
            var par_node = $(options['parent']);
            for( var i = 0; i < this.items.length; i ++ ){
                var item = this.items[i];
                if( ! item.menu_item )
                    continue;
                if( ! item.menu_item.id == par_node.id )
                    continue;
                parent = item;
                break;
            }
        }

        var item = new Widget._MenuItem( this, activator, menu_item, parent );
        this.items.push(item);
    },

    close_all: function() {
        for( var i = 0; i < this.items.length; i ++ ){
            this.items[i].close(true);
        }
    }
}


















