var application = {
    tree: {
        nodes: {
            main: null
        },
        options: {
            n: {
                main: '.content-menu',
                menu: '.content-menu > li > ul'
            }
        },
        init: function()
        {
            var self = this;
            self.nodes.main = $(self.options.n.main);
            if (!self.nodes.main.length)
                return;
            //$('.content-menu > li > ul > li > ul').each(function(){
            $(self.options.n.menu).find('ul').each(function(){
                var ul = $(this).hide();
                $('<a>', {
                    href: '#',
                    text: '',
                    'class': 'tree-switcher'
                }).insertBefore(ul);
                
                ul.parent().children('a').each(function(){
                    var btn = $(this);
                    btn.click(function() {
                        ul.toggle();
                        link = (btn.hasClass('tree-switcher')) ? btn : btn.next() ;
                        link.toggleClass("active");
                        
                        id = link.prev().attr('href');
                        if (link.hasClass('active'))
                            self.cookies.set(id, 'set', {path: '/'});
                        else {
                            self.cookies.del(id);
                        }
                        return false;
                    });
                    if (!btn.hasClass('tree-switcher') && self.cookies.get(btn.attr('href')) == 'set') {
                        ul.toggle();
                        btn.next().toggleClass("active");
                    }
                });
            });
        },
        cookies: {
            get: function(name) {
                var matches = document.cookie.match(new RegExp(
                  "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
                ))
                return matches ? decodeURIComponent(matches[1]) : undefined;
            },
            set: function (name, value, props) {
                props = props || {}
                var exp = props.expires
                if (typeof exp == "number" && exp) {
                    var d = new Date();
                    d.setTime(d.getTime() + exp*1000);
                    exp = props.expires = d;
                }
                if(exp && exp.toUTCString) { props.expires = exp.toUTCString() }
                value = encodeURIComponent(value)
                var updatedCookie = name + "=" + value
                for(var propName in props){
                    updatedCookie += "; " + propName
                    var propValue = props[propName]
                    if(propValue !== true){ updatedCookie += "=" + propValue }
                }
                document.cookie = updatedCookie
            },
            del: function(name) {
                this.set(name, null, { expires: -1 , path: '/'})
            }
        }
	}
}

$(document).ready(function(){
    (function() {
        this.tree.init();
    }).call(application);
});

