var MenuStrip=Class.create({opts:{linkDepth:2,showMenuDelay:300,hideMenuDelay:700,rootMenuCss:"rootMenu",highlightCss:"childHover",menuDataAttr:"data-mi"},html:{menu:null,root:null},initialize:function(b,a){a=a||{};this.opts=Object.extend(Object.clone(this.opts),a);this.html=Object.clone(this.html);this.html.menu=$(b);this.initElements()},initElements:function(){this.html.root=this.html.menu.down("."+this.opts.rootMenuCss);if(this.html.root){this.initHierarchy(this.html.root)}},initHierarchy:function(a){a._childLinks=new Array();a.select("a").each(function(c){c._parentMenu=a;a._childLinks.push(c);var b=this.getMenuFromLink(c);if(b){Event.observe(b,"mouseenter",this.evSubMenuHover.bindAsEventListener(this,b));Event.observe(b,"mouseleave",this.evSubMenuLeave.bindAsEventListener(this,b));Event.observe(c,"mouseenter",this.evLinkHover.bindAsEventListener(this,c));
Event.observe(c,"mouseleave",this.evLinkLeave.bindAsEventListener(this,c));c._childMenu=b;b._parentLink=c;this.initHierarchy(b)}else{Event.observe(c,"mouseenter",this.evLinkHover.bindAsEventListener(this,c));Event.observe(c,"mouseleave",this.evLinkLeave.bindAsEventListener(this,c))}}.bind(this))},getMenuFromLink:function(a){var b=a.getAttribute(this.opts.menuDataAttr);if(b){return $(b)}return null},setHideMenuTimer:function(a){a._hideTimer=setTimeout(this.hideSubMenuDelayed.bind(this,a),this.opts.hideMenuDelay)},setShowMenuTimer:function(a){a._showTimer=setTimeout(this.showSubMenu.bind(this,a),this.opts.showMenuDelay)},clearMenuTimers:function(a){if(a._hideTimer){clearTimeout(a._hideTimer)}if(a._showTimer){clearTimeout(a._showTimer)}a._hideTimer=null;a._showTimer=null;if(a._parentMenu!==this.html.root){this.clearMenuTimers(a._parentMenu._parentLink)
}},ensureActive:function(c,b){if(c&&c._parentLink){var a=c._parentLink;if(a._parentMenu==this.html.root){var d=a.up("li",0);if(b){a.addClassName(this.opts.highlightCss);if(d){d.addClassName(this.opts.highlightCss)}}else{c._parentLink.removeClassName(this.opts.highlightCss);if(d){d.removeClassName(this.opts.highlightCss)}}}else{if(a._parentMenu!=this.activeMenu||b){this.ensureActive(c._parentLink._parentMenu,b)}}}},getTopMenu:function(a){if(a._parentMenu!=this.html.root){return this.getTopMenu(a._parentMenu._parentLink)}else{return a}},showSubMenu:function(a){this.hideSiblingMenus(a);var b=a&&a._childMenu;if(b){b.show();this.ensureActive(b,true)}},hideSubMenu:function(a){var b=a&&a._childMenu;if(b&&b!==this.html.root&&b.visible()){this.clearMenuTimers(a);b._childLinks.each(function(c){this.hideSubMenu(c)
}.bind(this));b.hide();this.ensureActive(b,false)}},hideSubMenuDelayed:function(a){if(!this.activeMenu){this.hideSubMenu(this.getTopMenu(a))}else{this.hideSubMenu(a)}},hideSiblingMenus:function(a){a._parentMenu._childLinks.each(function(b){this.clearMenuTimers(b);this.hideSubMenu(b)}.bind(this))},evSubMenuHover:function(a,b){this.activeMenu=b;this.clearMenuTimers(b._parentLink)},evSubMenuLeave:function(a,b){this.activeMenu=null;this.clearMenuTimers(b._parentLink);this.setHideMenuTimer(b._parentLink)},evLinkHover:function(a,b){this.clearMenuTimers(b);this.setShowMenuTimer(b)},evLinkLeave:function(a,b){this.clearMenuTimers(b);if(b._parentMenu==this.html.root){this.setHideMenuTimer(b)}}});
