/*
 * explorerMenu() v2.0
 *
 * Finds all lis within the given ul(s) and, if they have children, adds a trigger to expand/collapse them.
 * The class does not show/hide the elements directly, but rather appends/removes classnames that CSS can
 * use to show/hide the menus.
 * 
 * Example:
 * 
 * .expandTrigger {
 *      width: 15px;
 *      height: 15px;
 *      background: black;}
 *          
 * .collapsed ul {
 *      display: none;}
 * 
 * @param options Object:     Hash of the following options:
 *      selector:             CSS selector targeting the ULs to convert to an expandable menu
 *      expand_trigger:       HTML element type to use as the trigger element
 *      expand_trigger_props: HTML properties for the trigger
 *      expanded_title_text:  Title text to use on the trigger in its expanded state
 *      collapseTitleText:    Title text to use on the trigger in its collapsed state
 *      expanded_class:       Classname to apply to the root li of an expanded menu
 *      colapsed_class:       Classname to apply to the root li of a collapsed menu
 *      has_children_class:   Classname to apply to any li that has a child UL
 *      ignore_class:         Optional classname applied to any LIs that should *not* be made collapsible
 */

if (!Prototype.K)
    throw('ExplorerMenu_2-0.class.js requires the Prototype library.');
    
if (empty == "undefined")
    throw('ExplorerMenu_2-0.class.js requires the webassets/js/bootstrap library.');

if (Effect == "undefined")
    throw('ExplorerMenu_2-0.class.js requires the Scriptaculous Effect library.');

var ExplorerMenu = Class.create();

ExplorerMenu.prototype = {
    
    initialize: function(options)
    {
        this.default_options = {
            selector:             'ul.explorerMenu',
            expanded_title_text:  'Collapse',
            collapsed_title_text: 'Expand',
            expanded_class:       'expanded',
            colapsed_class:       'collapsed',
            has_children_class:   'hasChildren',
            ignore_class:         'ignore',
            expand_trigger:       'span',
            expand_trigger_props: {
                'class': 'expandTrigger'
            }
        };
        this.options = Object.extend(this.default_options, options || {}); // the || {} substitutes an empty object if options is undefined
        this.ul_objs = $$(this.options.selector);
        
        this.setup();
    },

    setup: function(options)
    {
        var menu = this;
        menu.ul_objs.each(function(ul_obj)
        {
            Element.extend(ul_obj); // If we don't manually extend the elements, IE just sits in the corner and drools on itself. The objects won't have any methods.    
            var lis = ul_obj.select('li');
            
            lis.each(function(li_obj)
            {
                Element.extend(li_obj);
                if(li_obj.down('ul') && !li_obj.hasClassName(menu.options.ignore_class)) // has children, isn't ignored
                {
                    li_obj.addClassName(menu.options.has_children_class);
                    li_obj.toggleClassName(menu.options.colapsed_class);
                    
                    // Create the trigger element to prepend to the li
                    li_obj.trigger = new Element(
                        menu.options.expand_trigger,
                        Object.extend(
                            menu.options.expand_trigger_props,
                            {
                                'title': menu.options.collapsed_title_text
                            }
                        )
                    );
                    
                    // Prepend the trigger
                    li_obj.insert({
                        top: li_obj.trigger
                    });
                    
                    li_obj.trigger.observe('click', menu.expand.bindAsEventListener(menu, li_obj));                    
                }
            });
        });
        
        //Expand the parent lis of the current node
        current_nodes = $$(menu.options.selector + ' a.current');
        current_nodes.each(function(current_node)
        {
            p_index = 0;
            while((cur_node = current_node.up('li.hasChildren', p_index)) != undefined)
            {
                menu.expand(menu, cur_node);
                p_index++;
            }
        });
    },
    
    expand: function(e)
    {
        var menu   = this;
        var li_obj = arguments[1];
        
        li_obj.toggleClassName(menu.options.colapsed_class);
        li_obj.toggleClassName(menu.options.expanded_class);
        
        if(li_obj.trigger.readAttribute('title') == menu.options.collapsed_title_text)
            li_obj.trigger.writeAttribute({'title': menu.options.expanded_title_text});
        else
            li_obj.trigger.writeAttribute({'title': menu.options.collapsed_title_text});
    }
};
