$(function() {

    var $body = $("body")
            .css({
                overflowY: "auto"
            }),
        $tests = new Ox.Bar({
                size: 20
            })
            .css({
                padding: "6px 0 6px 8px",
                fontSize: "16px",
                fontWeight: "bold"
            })
            .appendTo($("body"));
        colors = [
            ["255, 64, 64", "224, 32, 32", "240, 16, 16"],
            ["64, 192, 64", "32, 160, 32", "40, 176, 48"],
            ["96, 96, 255", "64, 64, 224", "80, 80, 240"]
        ],
        gradients = [
            "-moz-linear-gradient(", 
            "-webkit-gradient(linear, "
        ];
    setBackground($tests, true);

    tests("../build/js/ox.js", "../build/js/ox.data.js");

    function tests() {
        var succeeded = 0, failed = 0,
            lines, spaces, command, expected, result, success,
            replace = ["", ""], fns = [], $test
        $.each($.isArray(arguments[0]) ? arguments[0] : arguments, function(i, script) {
            $.get(script, function(data) {
                Ox.Bar({size: 17})
                    .css({padding: "3px 0 0 8px"})
                    .html(Ox.basename(script))
                    .appendTo($body);
                lines = data.split("\n");
                $.each(lines, function(l, line) {
                    if (line.indexOf("/*") > -1 && lines[l + 1].indexOf("====") > -1) {
                        Ox.Bar({size: 17})
                            .css({padding: "3px 0 0 24px"})
                            .html($.trim(lines[l + 2]))
                            .appendTo($body);
                        //setBackground(x, 2)
                    }
                    spaces = line.indexOf(">>> ");
                    if (spaces > -1) {
                        command = $.trim(line).substr(4).split(" // ")[0];
                        fn = command.match(/Ox\.\w+/);
                        expected = eval(lines[l + 1].substr(spaces, lines[l + 1].length));
                        // fixme: if object, recursively check identity
                        result = eval(command);
                        if (fn) {
                            fn = fn[0];
                            success = typeof expected == "object" ?
                                expected.toString() == result.toString() : expected === result;
                            succeeded += success;
                            failed += !success;
                            $tests.html((succeeded + failed) + " tests, " +
                                succeeded + " succeeded, " + failed + " failed");
                            if (!success) {
                                setBackground($tests, success);
                            }
                            if ($.inArray(fn, fns) == -1) {
                                fns.push(fn);
                                $test = Ox.CollapsePanel({
                                        collapsed: true,
                                        title: fn + "()"
                                    })
                                    .appendTo($body);
                                setBackground($test.find(".OxBar"), true);
                            }
                            ///*
                            Ox.Bar({size:17}) // fixme: Ox.Object() used to work
                                .css({
                                    //padding: "2px 0 2px 8px",
                                    background: "rgb(" + colors[+success][2] + ")"
                                })
                                .html(
                                    Ox.basename(script) + ", line " + l + ": <span style=\"font-weight: bold\">" +
                                    Ox.encodeHTML(command).replace(replace[0], replace[1]) + " " +
                                    (success ? "=" : "!") + "=&gt; " + Ox.encodeHTML(expected.toString()) +
                                    (success ? "" : " ==&gt; " + Ox.encodeHTML(result.toString())) + "</span>"
                                )
                                .appendTo($test.$content);
                            //*/
                            // to be fixed in ui:
                            // /*
                            $test.$content
                                .css({
                                    marginTop: -$test.$content.height() + "px"
                                });
                            //*/
                            // /*
                            if (!success) {
                                setBackground($test.find(".OxBar"), success);
                            }
                            //*/
                        } else {
                            replace = command.split(" = ")
                        }
                    }
                });
            });
        });
    }

    function setBackground($element, success) {
        $.each(gradients, function(i, v) {
            $element.css({
                background: v + "left top, left bottom, from(rgb(" +
                    colors[+success][0] + ")), to(rgb(" +
                    colors[+success][1] + ")))"
            });
        });
    }

});