function CategoriesMenu(el) {
    
    this._onTitleClick = function(e) {
        el = Event.element(e);
        li = Element.up(el, 'li', 0);
        ul = li.select('ul')[0]
        if (ul) {
            Element.toggle(ul)
        }
    }
    
    this._init = function(el) {
        titles = [];
        
        
        try {
            matches = location.href.match(/^.*category\/([0-9]+).*$/);
            var cc = matches[1]
            lookForCurrentCategory = true
        } catch(e) {
            var cc = null;
            lookForCurrentCategory = false
        }
        
        el.select('a').each(function(a) {
          if (matches = a.href.match(/^.*category\/([0-9]+).*$/)) {
            if (cc == matches[1]) {
              a.ancestors().each(function(aa) {
                if (aa.nodeName.toLowerCase() == 'ul') {
                  Element.show(aa);
                }
              });
            }
          }
        });
        
        el.select('li').each(function(li) {
            li.select('.title').each(function(tt) {
                if (titles.indexOf(tt) == -1) {
                    titles.push(tt)
                }
            }.bind(this))
        }.bind(this))
        
        titles.each(function(tt) {
            tt.style.cursor = 'pointer';
            Event.observe(tt, 'click', this._onTitleClick.bind(this))
            lnks = tt.select('a');
            if (lnks.length > 0) {
                p = lnks[0].href.split('/').pop();
                
                if (cc == p) {
                    this._setCurrent(lnks[0].up('li', 0));
                    Element.show(lnks[0].up('ul', 0));
                }
            }
        }.bind(this))
    }
    
    this._setCurrent = function(el) {
        if (el == undefined) {
            return;
        }
        if (Element.hasClassName(el, 'categories-menu')) {
            return;
        }
        
        tt = el.select('.title a');
        
        t = tt[0];
        
        try {            
            Element.addClassName(t, 'current');            
        } catch(e) {
            return;
        }
    }
    
    this._init(el)
}

document.observe("dom:loaded", function() {
    $$('.categories-menu').each(function(el) {
        new CategoriesMenu(el)
    })
});