

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 );
    }
    //connect(window, 'onresize', bind(this.update_position_to_parent, 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 + elementDimensions(this.activator).w;
        var t = elementPosition(this.activator).y;
        if (elementDimensions(this.menu_item).h > 550) {
            t = t - 174;
            var img = findChildElements(this.menu_item, ["img"]);
            setStyle(img[0], {'margin-top' : '174px'});
        }
        if( this.parent )
            hideElement(this.parent.menu_item);
        setElementPosition( this.menu_item, {x:r, y:t} );
    },

    update_position_to_parent : function() {
        if( ! this.activator )
            return;
        if( ! this.menu_item )
            return;
        var r = elementPosition(this.activator).x + elementDimensions(this.activator).w;
        var t = elementPosition(this.activator).y;
        if (elementDimensions(this.menu_item).h > 550) {
            t = t - 174;
            var img = findChildElements(this.menu_item, ["img"]);
            setStyle(img[0], {'margin-top' : '174px'});
        }
        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_to_parent();
    },

    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' );
        }
    },

    getId : function() {
        if( this.menu_item )
            return this.menu_item.id;
        else
            return '?';
    }
}

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);
        }
    }
}

















