jQuery(function() {
    jQuery.ajax({
        method: "POST",
        url: AjaxRoot + "/login?action=login",
        dataType: "json",
        data: { name: "viktor", password: "netline" },
        success: function(data) {
            session = data.session;
            jQuery.ajax({
                url: AjaxRoot + "/config?session=" + session,
                dataType: "json",
                success: function(reply) {
                    temporary.configuration.root = reply.data;
                    run();
                }
            });
        }
    });
});

function imgPath(path) { return "../themes/default/" + path; }

function newtext(text) { return document.createTextNode(text); }

var corewindow = window;

var temporary = {
    configuration: {
        get: function(path) {
            if (typeof path == "string") path = path.split("/");
            var data = this.root;
            for (var i = 0; i < path.length; i++) {
                if (!(path[i] in data)) return null;
                data = data[path[i]];
            }
            return data;
        },
        set: function(path, value) {
            if (typeof path == "string") path = path.split("/");
            var data = this.root;
            for (var i = 0; i < path.length - 1; i++) {
                if (!(path[i] in data)) data[path[i]] = {};
                data = data[path[i]];
            }
            data[path[path.length - 1]] = value;
        }
    },
    mailAccounts: {
        get: function(id, callback, context) {
            if (this.accounts) {
                callback.call(context, this.accounts[id]);
                return;
            } else {
                this.pending.push(
                    { id: id, callback: callback, context: context });
                if (this.pending.length == 1) {
                    var self = this;
                    jQuery.ajax({
                        method: "get",
                        url: AjaxRoot + "/account",
                        data: { action: "all",
                                columns: "1001,1026,1027,1028,1029",
                                session: session },
                        dataType: "json",
                        success: function(reply) {
                            self.accounts = {};
                            for (var i = 0; i < reply.data.length; i++) {
                                var account = reply.data[i];
                                self.accounts[account[0]] = {
                                    trash_fullname: account[1],
                                    sent_fullname: account[2],
                                    drafts_fullname: account[3],
                                    spam_fullname: account[4]
                                }
                            }
                            for (var i = 0; i < self.pending.length; i++) {
                                var cb = self.pending[i];
                                cb.callback.call(cb.context,
                                                 self.accounts[cb.id]);
                            }
                            self.pending = null;
                        }
                    });
                }
            }
        },
        pending: [],
        accounts: null
    }
};

function run() {
    var tree = new FolderTree({});
    var widget = new FolderTree.Widget(tree);
    document.body.appendChild(widget.node);
}
