if (typeof $Tree == "undefined") var $Tree = {

    /*
     * tree format is not yet documented, but you can
     * check it in the Resource API methods
     */
    tree: new Array(),

    /*
     * cookieId used to persist opened branches
     */
    cookie_id: '',

    /*
     * base id used to form ids for referencing tree elements 
     */
    tree_id: 'dtree',

    /*
     * dom element where this tree is rendered
     */
    dom_container: '',


    /*
     * this callback is used to get the Html text that is shown
     * right after every <ul> element of the tree
     * 
     * it recieves the node Id
     */
    node_html_callback: null,

    /*
     * when leaves are found, instead of childs (check tree structure)
     * this callback is called
     *
     * it receives a dom element, and the list of leaves
     */
    write_leaves_callback: null,

    lazy_load_callback: null,

    /*
     * its true only before the first (root) node is rendered
     */
    initial_node: true,

    /*
     * when false, it will hide branches that do not contain any resources
     */
    show_empty_branches: null,

    appendTree: function(tpart) {
        for (x in tpart) {
            this.tree[x] = tpart[x];
        }
    },

    reset: function() {
      this.tree = new Array();
      this.node_html_callback = null;
      this.write_leaves_callback = null;
      this.lazy_load_callback = null;
      this.initial_node = true;
      this.show_empty_branches = true;
      this.cookie_id = '';
      this.dom_container = '';
      this.tree_id = 'dtree';
    },

    /*
     * after defining the object properties
     * this is the only function you should call
     *
     * usually it will receive a div DOM element
     * and the node Id where we should start
     * showing the tree structure
     */
    writeTree: function(e, root_nid) {
        etree = $("#" + this.tree_id + "-" + root_nid);
        if (this.initial_node) {
            $(e).html('<ul id="' + this.tree_id + '-root" class="treeview-gray"></ul>');
            this.writeNode($("#" + this.tree_id + "-root"), root_nid, true, true);
            etree = e;
        }
        var f_lazy_load = this.lazy_load_callback;
        this.buildNodes($("#" + this.tree_id + "-" + root_nid), root_nid);
        if (this.cookie_id == '') {
            $(etree).treeview({
                speed: 1,
                toggle: function() {if (typeof(f_lazy_load) == 'function') f_lazy_load(this);}
            });
        } else {
            $(etree).treeview({
                persist: "cookie",
                cookieId: this.cookie_id,
                collapsed: true,
                speed: 1,
                toggle: function() {if (typeof(f_lazy_load) == 'function') f_lazy_load(this);}
            });
        }
        this.initial_node = false;
    },

    /*
     * write the node, and call me recursivelly
     */
    buildNodes: function(e, parent) {
        for (var x in this.tree[parent]['childs']) {
            var first = false;
            var last = false;
            if (x == 0) first = true;
            if (x == this.tree[parent]['childs'].length - 1) last = true;
            var nid = this.tree[parent]['childs'][x];
            if (this.show_empty_branches || this.tree[nid]['num_resources'] > 0) {
                var e_child = this.writeNode(e, nid, first, last);
                if (e_child) {
                    this.buildNodes(e_child, nid);
                }
            }
        }
    },

    /*
     * just call writenodehtml
     */
    writeNode: function(e, nid, first, last) {
        if (typeof(this.node_html_callback) == 'function') {
            var e_child = this.writeNodeHtml(e, nid, first, last);
            return e_child;
        } else {
            return null;
        }
    },

    /*
     * writes a node under the DOM element 'e'
     * nid is the actual nide Id in the tree
     */
    writeNodeHtml: function(e, nid, first, last) {
        if (this.tree[nid]) {
            var e_child = null;
            var node_html = this.node_html_callback(nid, this, first, last);
            if (this.tree[nid]['childs'].length > 0 || (this.tree[nid]['leaves'] && this.tree[nid]['leaves'].length > 0)) {
                $(e).append('<li class="">' + node_html + '<ul rel="' + nid + '" id="' + this.tree_id + '-' + nid + '" class="node-sort"></ul></li>');
                e_child = $("#" + this.tree_id + "-" + nid);
            } else {
                $(e).append("<li>" + node_html + "</li>");
            }
            if (this.tree[nid]['leaves'] && this.tree[nid]['leaves'].length > 0) {
                if (typeof(this.write_leaves_callback) == 'function') {
                    this.write_leaves_callback($("#"+this.tree_id+"-" + nid), nid, this.tree[nid]['leaves']);
                }
            }
            return e_child;
        } else {
            return null;
        }
    }
}

