document.observe('dom:loaded', function() {
  when('left-menu', function(leftmenu){
    $$('.sbl').each(function(link){
      new SubCategoryLink(link);
    });
  });
});

// When object is available, do function fn.
function when(obj, fn) {
  if (Object.isString(obj)) obj = /^[\w-]+$/.test(obj) ? $(obj) : $(document.body).down(obj);
  if (Object.isArray(obj) && !obj.length) return;
  if (obj) fn(obj);
}

var SubCategoryLink = Class.create({
  initialize: function(element) {
    $(element).
        observe('click', this.onClick.bindAsEventListener(this));
  },

  onClick: function(event) {
    var products = event.element().next('ul.products');
    if (!products.visible()) {
      event.stop();
      products.show();
    }
  }
});


