1 madrona.menu_items = (function(){
  2     var that = {};
  3     
  4     that.panels = [];
  5     
  6     var init = function(el){
  7         that.el = el;
  8         that.el.find('li.item_trigger').each(function(){
  9             var self = $(this);
 10             var els = $('<a style="display:none;" class="close" href="#"><img src="'+madrona.options.media_url+'common/images/tool_window_pointer.png" width="23" height="36" /></a></a>');
 11             els.click(function(){
 12                 closeAll();
 13                 return false;
 14             });
 15             self.append(els)
 16             menuLink = self.find('span a');
 17             menuLink.click(function(){
 18                 if(!that.el.hasClass('disabled')){
 19                     closeAll();
 20                     var parent = $(this).parent().parent()[0];
 21                     openItem(parent);                    
 22                 }
 23                 return false;
 24             });
 25             var href = menuLink.attr('href');
 26             var content = $(href);
 27             if(content){
 28                 var panel = madrona.panel({
 29                     scrollable: !self.hasClass('autosize'),
 30                     content: content,
 31                     hideOnly: true,
 32                     showCloseButton: false,
 33                     appendTo: $('#panel-holder')
 34                 });
 35                 panel.getEl().css('z-index', '11');
 36                 panel.getEl().addClass('madrona-menu-items');
 37                 $.data(self[0], 'panel', that.panels.length)
 38                 that.panels.push(panel);
 39             }
 40         });
 41     }
 42     
 43     that.init = init;
 44     
 45     var closeAll = function(){
 46         var open = that.el.find('li.item_trigger.toggled');
 47         if(open.length === 1){
 48             $('.madrona-panel:visible').each(function(){
 49                 $(this).find('.panelMask').hide();
 50             });
 51             open.removeClass('toggled')
 52             open.find('a.close').hide();
 53             open.each(function(){
 54                 var item = $(this);
 55                 var panel = that.panels[item.data('panel')];
 56                 if(panel){
 57                     panel.hide();
 58                 }else{
 59                 }
 60             });
 61         }
 62         // madrona.unmaskSidebar();
 63         
 64         // This is to prevent multiple menu items from appearing at once
 65         // BUT it causes some strange behavior: clicking on the bookmark edit form panel
 66         // causes the panel to close. Maybe having the bookmark panel PLUS the data layers
 67         // panel open at the same time *would* be desireable... "it's a feature not a bug" ? 
 68         // $("#bookmark-close").click();
 69     }
 70     
 71     that.closeAll = closeAll;
 72     
 73     var openItem = function(item){
 74         madrona.maskSidebar();
 75         item = $(item);
 76         item.addClass('toggled');
 77         item.find('a.close').show();
 78         if(item.data('panel') || item.data('panel') === 0){
 79             $('.madrona-panel:visible').each(function(){
 80                 $(this).find('.panelMask').show();
 81             });
 82             var panel = that.panels[item.data('panel')];
 83             panel.show();
 84         }else{
 85             // couldn't get index
 86         }
 87     }
 88     
 89     that.openItem = openItem;
 90     
 91     var disable = function(){
 92         that.el.addClass('disabled');
 93     };
 94     
 95     that.disable = disable;
 96     
 97     var enable = function(){
 98         that.el.removeClass('disabled');
 99     };
100     
101     that.enable = enable;
102     
103     return that;
104 })();
105