/**
 * @author Dipl.-Ing. (FH) André Fiedler <a.fiedler@oe-konzept.de>
 */
 
var OeMenu = new Class({

    Implements: Options,
    
    options: {
        activeIndex: null
    },

    initialize: function(containerElement, mainNaviElements, subNaviElements, options){
        this.setOptions(options);
    
        this.container = $(containerElement);
        this.mainNavis = $$(mainNaviElements);
        this.subNavis = $$(subNaviElements);
        
        if(!this.subNavis[this.options.activeIndex])
            this.options.activeIndex = null;
        
        this.currentIndex = this.options.activeIndex;
        
        this.container.addEvent('mouseleave', function(){
            this.showMenu(this.options.activeIndex);
        }.bind(this));

        this.mainNavis.each(function(item, i) {
            if(i !== null && i != this.options.activeIndex) {
                this.subNavis[i].setStyles({
                    'opacity'   : 0,
                    'visibility': 'visible'
                });
                this.subNavis[i].fade('hide');
                this.mainNavis[i].removeClass('active');
            }
            item.store('oe:menu:index', i);
            
            this.subNavis[i].set('tween', { duration: 'short' });
            this.subNavis[i].get('tween').addEvent('complete', function() {
                if(this.currentIndex !== null && this.subNavis[this.currentIndex].get('opacity') != 1) {
                    // aktuelles Menü einblenden
                    this.mainNavis[this.currentIndex].addClass('active');
                    this.subNavis[this.currentIndex].fade('in');
                }
            }.bind(this));
          
            new HoverGroup({
                elements: [item, this.subNavis[i]],
                delay: 50,
                onEnter: function(e){
                    this.showMenu($(e.target).retrieve('oe:menu:index'));
                }.bind(this)
            });
        }, this);
    },
    
    showMenu: function(i){
        if(i === null) i = this.options.activeIndex;
        if(i === this.currentIndex) return;
        this.currentIndex = i;
        // alle ausblenden
        this.subNavis.fade('out');
        this.mainNavis.removeClass('active');
    }
});

