var EuroMenu =
{
    _currentMenu: null,
    _hideTimer: null,
    _showTimer: null,
    _currentShow: null,
    _currentHide: null,
    
    init: function()
    {
        var m = $('menu');
        if(m == null || typeof(m) == 'undefined')
            return;
        $$('.submenu').invoke('hide').invoke('observe', 'mouseover', this.cancelHide.bindAsEventListener(this)).invoke('observe', 'mouseout', this.hide.bindAsEventListener(this));
        var ovf = function(){ this.addClassName('hover'); }
        var ouf = function(){ this.removeClassName('hover'); }
        $$('.submenu li').each(function(l)
        {
            l.observe('mouseover', ovf.bind(l));
            l.observe('mouseout', ouf.bind(l));
        });
        $A($('menu').getElementsByTagName('a')).each(function(l)
        {
            l = $(l);
            l.observe('mouseout', this.hide.bindAsEventListener(this));
            l.observe('mouseover', function(e){ if($(e.target.id+'-sub') != null){ e.stop(); this.show(e); } }.bindAsEventListener(this));
        }.bind(this));
        
        m.up().insert('<iframe id="euromenu-frame" style="filter: progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0); position: absolute; opacity: 0; top: 0; left: 0; width: 1px; height: 1px;" border="0" frameborder="0"></iframe>')
    },
    
    cancelHide: function(e)
    {
        if(this._hideTimer)
            clearTimeout(this._hideTimer);
        this._hideTimer = null;
    },
    
    show: function(e)
    {
        if(this._showTimer)
            clearTimeout(this._showTimer);
        this._showTimer = setTimeout(this.realShow.bind(this, e), 200);
    },
    
    realShow: function(e)
    {
        try
        {
            var t = $(e.target.id + '-sub');
            if(t == null || typeof(t) == 'undefined' || t == this._currentMenu)
                return;

            this.realHide();
            this._currentMenu = $(e.target.id + '-sub');
            
            var p = $(e.target).cumulativeOffset();
			var d = this._currentMenu.getDimensions();
            var s = {left: p[0]+'px', top: (p[1]-d.height)+'px', zIndex: 100};
            this._currentMenu.setStyle(s);
            this._currentShow = new Effect.Appear(this._currentMenu, {duration: .10, afterFinish: function()
            {
                var p = this.positionedOffset();
                d.left = p[0]+'px';
                d.top = p[1]+'px';
                d.width += 'px';
                d.height += 'px';
                d.zIndex = 99;
                $('euromenu-frame').setStyle(d).show();
            }.bind(this._currentMenu)});
        }
        catch(e)
        {
            console.debug(e);
        }
        this._showTimer = null;
    },
    
    hide: function()
    {
        this._hideTimer = setTimeout(this.realHide.bind(this), 250);
    },
    
    realHide: function()
    {
        this.cancelHide();
        if(this._currentMenu)
        {
            $('euromenu-frame').hide();
            this._currentMenu.hide();
            //this._currentHide = new Effect.BlindUp(this._currentMenu, {duration: .3, queue: 'end'});
        }
        this._currentMenu = null;
    }
}