Ext.ns('BIC');

/**
 * @class BIC.Menu
 * @extends Ext.util.Observable
 * @constructor
 * @param {Object} config
 */
BIC.Menu = function(config) {
    this.el = Ext.get(config.el);
    delete config.el;
        
    Ext.apply(this, config);
    
    BIC.Menu.superclass.constructor.call(this);
    
    this.init();
}

Ext.extend(BIC.Menu, Ext.util.Observable, {
    /**
     * @type Ext.Element 
     */
    el: null,
    
    /**
     * @type Array
     */
    expandableOptions: null,
    
    expandedMenu: null,
    
    init: function() {
        var currentOption = this.el.child('li.current_item'),
            itemLinks     = Ext.select(this.el.query('> li a')),
            itemIndex, innerMenu, currentExpandableLink;
            
        if (currentOption) {
            currentExpandableLink = currentOption.child('a');
            var currentOptionParentMenu = currentOption.findParent('ul', null, true);
            
            if (currentOptionParentMenu && false === currentOptionParentMenu.hasClass('menu-root')) {
                this.expandedMenu     = currentOptionParentMenu;
                currentExpandableLink = currentOptionParentMenu.prev('a');
            } else if (currentExpandableLink) {
                itemIndex = this.expandableOptions.indexOf(currentExpandableLink.dom.text.toLowerCase());
                if (itemIndex > -1) {
                    this.expandedMenu = currentOption.child('ul');
                }
            }
        }
            
        itemLinks.each(function(link) {
            itemIndex = this.expandableOptions.indexOf(link.dom.text.toLowerCase());
            
            if (itemIndex > -1) {
            	link.on('click', this.onLinkClick, this);
                
                innerMenu = link.next('ul');
                
                if (null !== innerMenu) {
                    innerMenu.setVisibilityMode(Ext.Element.DISPLAY);
                    
                    if (!currentExpandableLink || currentExpandableLink.dom.text != link.dom.text) {
                        innerMenu.hide();
                    }
                }
            }
        }, this);
        
        delete itemIndex, innerMenu;
    },
    
    /**
     * @param {Ext.EventObject} event
     * @param {HTMLElement} link
     */
    onLinkClick: function(event, link) {
    	event.stopEvent();
    	
        link = Ext.get(link);
        var targetMenu  = link.next('ul');
            
        if (null !== targetMenu) {
        	if (targetMenu == this.expandedMenu) {
        		targetMenu.hide();
        		this.expandedMenu = null;
        	} else {
        		if (this.expandedMenu) {
        			this.expandedMenu.hide();
        		}
        		
        		targetMenu.show();
                
                this.expandedMenu = targetMenu;
        	}
        }
    }
});

BIC.Menu.load = function() {
    new this({
        el                : Ext.DomQuery.selectNode('#left_sidebar .menu-root'),
        expandableOptions : [
            'about us',
            'resources'
        ]
    });
}

Ext.onReady(BIC.Menu.load, BIC.Menu);
