
StatusWidget = function(id, net) {
    this.id = id;

    if (net) {
        var self = this;
        net.asyncRequestJson('getStats.json', function(result) {
            self.__setText("name", result.name);
            self.__setText("hit_point_score", result.hit_points);
            self.__setText("kickboxing_score", result.kickboxing);
            self.__setText("linear_algebra_score", result.linear_algebra);
            self.__setText("cross_stitching_score", result.cross_stitching);
        });
    }

    var self = this;
    this.__getChild("equipment").onclick = function() {
        self.toggleInventoryDisplay();
    };
};

StatusWidget.prototype = {
    toggleInventoryDisplay : function() {
        var n = this.__getChild("inventory");
        if (n.style['display'] == 'block') {
            n.style['display'] = 'none';
        } else {
            n.style['display'] = 'block';
        }
    },

    __getChild : function(className) {
        var xpath = "//div[@id='" + this.id + "']//*[@class='" + className + "']";
        var i = document.evaluate(
            xpath, document, null, XPathResult.ANY_TYPE, null
        );
        return i.iterateNext();
    },

    __setText : function(classname, value) {
        var c = this.__getChild(classname);
        c.innerHTML = value;
    },

    __getInt : function(className) {
        return parseInt(this.__getChild(className).innerHTML);
    },

    getName : function() {
        return this.__getChild("name").innerHTML;
    },

    getHitPoints : function() {
        return this.__getInt("hit_point_score");
    },

    getKickboxingScore : function() {
        return this.__getInt("kickboxing_score");
    },

    getLinearAlgebraScore : function() {
        return this.__getInt("linear_algebra_score");
    },

    getCrossStitchingScore : function() {
        return this.__getInt("cross_stitching_score");
    }
};

function Network() {
}

Network.prototype = {
    asyncRequestJson : function (url, onComplete) {
        var req = new XMLHttpRequest();
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                if (req.status == 200) {
                    var result = jsonParse(req.responseText);
                    onComplete(result);
                } else {
                    throw new Error("Request failed: " + req.status);
                }
            }
        }

        req.open('GET', url, true);
        req.send(null);
    }
}
