
var request = require("request"),
    jsdom   = require("jsdom");
exports.cookies;
exports.varspace;
exports.root_url;
exports.arch_url;
exports.forums;
exports.fns = {
    make_request: function(url, callback, parent_scope) {
        request({url: url, jar: exports.cookies}, function(error, response, body) {
            if (error)
                throw error;
            jsdom.env(
                body,
                ['https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js'],
                {},
                function(error, window) {
                    if (error)
                        throw error;
                        (function($){
                            callback($, parent_scope);
                        })(window.jQuery);
                        window.close();
                });
        });
    },
    get_notifications: function(callback) {
        request({url: exports.root_url, jar: exports.cookies}, function(error, response, body){
            if (body != undefined) {
                var alerts = Number(body.match(/\<span class="icon\-alert"\>\<\/span\> (\d+)\<\/a\>\<\/li\>/)[1]);
                var pms = Number(body.match(/\<span class="icon-mail"\>\<\/span\> (\d+)\<\/a\>\<\/li\>/)[1]);
                var data = {alerts: alerts, pms: pms};
                callback(data);
            } else {
            }
        });
    },
    build_forum_tree: function($, scope) {
        var forums = {};
        scope.forum_list = {};
        scope.forum_cats = [];
        $("div.forums > ul > li").each(function(index, element){
            var anchor = $(element).children("strong").children("a");
            forums[anchor.text()] = {
                url: anchor.attr("href"),
                id: anchor.attr("href").match(scope.link_regex)[1],
                subforums: {
                }
            };
            scope.forum_cats.push(anchor.text());
            scope.forum_list[forums[anchor.text()].id] = anchor.text()
            var ordered_list_items = $(element).children("ol").children("li");
            ordered_list_items.each(function(index_0, element_0){
                var element_0 = $(element_0),
                    anchor_0 = element_0.children("a");
                if (anchor_0.text() == "") {
                    anchor_0 = element_0.children("strong").children("a");
                }
                forums[anchor.text()].subforums[anchor_0.text()] = {
                    url: anchor_0.attr("href"),
                    id: anchor_0.attr("href").match(scope.link_regex)[1],
                    subforums: {
                    }
                };
                scope.forum_list[forums[anchor.text()].subforums[anchor_0.text()].id] = anchor_0.text()
                if (element_0.children("ol").length > 0) {
                    var ordered_list_items_0 = element_0.children("ol").children("li");
                    ordered_list_items_0.each(function(index_1, element_1) {
                        var element_1 = $(element_1),
                            anchor_1 = element_1.children("a");
                        if (anchor_1.attr("href") == undefined) {
                            anchor_1 = element_1.children("strong").children("a");
                        }
                        forums[anchor.text()].subforums[anchor_0.text()].subforums[anchor_1.text()] = {
                            url: anchor_1.attr("href"),
                            id: anchor_1.attr("href").match(scope.link_regex)[1]
                        }
                        scope.forum_list[forums[anchor.text()].subforums[anchor_0.text()].subforums[anchor_1.text()].id] = anchor_1.text()
                    });
                } else {
                    delete forums[anchor.text()].subforums[anchor_0.text()].subforums;
                }
            });
        });
        scope.forum_tree = forums;
    }
};
