﻿

var Tree = {
    setup: function() {
        var tree = $$('.tree');
        if (tree.length > 0)
            tree = tree[0];
        else
            return;

        tree.getElements('a').addEvent('click', function(x) {
            x.stop();
            var hr = x.target.get('href');
            location.href = '#' + hr;
            var ok = false;
            $$('#navigation a').each(function(y) {
                if (y.get('href') == x.target.get('href')) {
                    location.href = '#' + y.get('href');
                    Common.LastClick = y;
                    Common.SetEnabled(y);
                    Common.LoadPage(y.get('href'));
                }
            });
            if (!ok) {
                Common.LoadPage(hr);
            }
        });

        // recursive loop through each item
        var t = $$('.tree ul');
        t.toggleClass('hidden');

        t.each(function(ul) {
            var par = ul.getParent();
            par.addClass('parent');
            var el = new Element('a', { 'href': '#' });
            var im = new Element('img', { 'src': 'images/toggle_plus.png' });
            im.injectTop(el);
            el.addEvent('click', function(e) {
                ul.toggleClass('hidden');
                if (ul.hasClass('hidden'))
                    im.setProperty('src', 'images/toggle_plus.png');
                else
                    im.setProperty('src', 'images/toggle_minus.png');
                e.stop();
            });
            el.injectTop(par);

        });



    }
}

window.addEvent('domready', function() { Tree.setup(); });
