var initMSA = function() {
        // Exclusive menu selection for catalog
        // TODO: Code review
        var oldSetActive = MB.SetActive;
        var mySetActive = function(li, activate) { 
                if(li.className.match("catalogcategory")) {
                        var activeLists = MB.GetOpenState(li).split(",");
                        for (var i=0,id; i<activeLists.length; i++) {
                                if(!(id=activeLists[i])) {continue;}
                                var el = document.getElementById(id);
                                if (el) { MB.SetInActive(el); }
                        }
                        MB.SetOpenState(li, null);
                }
                oldSetActive(li,activate);
        };
        MB.SetActive = mySetActive;
};

var activateCategory = function(){
        // To activate the menu for the selected product
        // TODO: Clean up
        var links,i,el,par;
        links = document.getElementById("prod_sidenav").getElementsByTagName("a");
        for (i=0; i < links.length; i++) {
                el = links[i];
                if (el.href == window.location.href) {
                        par = el.parentNode.parentNode.parentNode;
                        MB.SetActive(par);
                }
        }
};

// To run the functions as soon as the DOM is available.
// Dean Edwards/Matthias Miller/John Resig OnDomLoad event
var PMHInited = false;
var PMHInit = function(){
  if(PMHInited){return;}  // to avoid multiple calls
  PMHInit = true;
  if (_timer) clearInterval(_timer);

  // Functions to run on DOM load
  initMSA();
  activateCategory();
};
/* for Mozilla/Opera9 */
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", PMHInit, false);  // Register listener
}
/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
  document.write("<script id=__PMH_ie_onload defer " + "src='//:'><\/script>");
    var script = document.getElementById("__PMH_ie_onload");
    script.onreadystatechange = function() {
      if (this.readyState == "complete") {
        PMHInit(); // call the onload handler
      }
    };
/*@end @*/
/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
  var _timer = setInterval(function() {
    if (/loaded|complete/.test(document.readyState)) {
      PMHInit(); // call the onload handler
    }
  }, 10);
}
/* for other browsers */
addLoadEvent(PMHInit);

