window.GitForked = function () {
    var h = e(),
        a = {
            buttonCssClass: "gitforked-button",
            watchersCssClass: "gitforked-watchers",
            forksCssClass: "gitforked-forks",
            splitterCssClass: "gitforked-splitter",
            containerCssClass: "gitforked-button-container",
            bubbleCssClass: "gitforked-bubble",
            bubbleInnerCssClass: "gitforked-bubble-inner",
            cssUrl: "http://hotglue.me/scripts/gitforked-button.css",
            githubRepoQuery: "http://github.com/api/v2/json/repos/show/"
        };
    i();
    return {
        createButton: c
    };

    function i() {
        f();
        var a = g(),
            b = a.length;
        while (b--) c(a[b])
    }
    function e() {
        var a = document.getElementsByTagName("script"),
            c = a.length;
        while (c--) {
            var d = a[c].getAttribute("src"),
                b = /^(.*hotglue.*\/scripts\/.*\/)gitforked-button-unfold\.js/.exec(d);
            if (b) return b[1]
        }
        return null
    }
    function f() {
    }
    function g() {
        var c = [],
            f = new RegExp("\\b" + a.buttonCssClass + "\\b"),
            d = document.getElementsByTagName("a"),
            e = d.length;
        while (e--) {
            var b = d[e];
            if (!b.getAttribute("href")) continue;
            var g = b.getAttribute("class");
            f.test(g) && c.push(b)
        }
        return c
    }
    function c(c) {
        var k = i(c),
            e = c.getAttribute("title");
        if (e) e += " - Button by gitforked.com";
        else e = "Button by gitforked.com";
        c.setAttribute("title", e);
        var h = c.getAttribute("href"),
            g = d(c, a.forksCssClass),
            f = d(c, a.watchersCssClass);
        (g || f) && b(h, function (a) {
            var b = j(h, g ? a.forks : 0, f ? a.watchers : 0);
            k.appendChild(b)
        });

        function j(h, i, g) {
            var d = document.createElement("span");
            d.setAttribute("class", a.bubbleCssClass);
            var c = document.createElement("span");
            c.setAttribute("class", a.bubbleInnerCssClass);
            var b = [];
            i > 0 && b.push(f(i, h + "/network", "Forks"));
            if (g > 0) {
                b.length > 0 && b.push(j());
                b.push(f(g, h + "/watchers", "Watchers"))
            }
            for (var e = 0, k = b.length; e < k; e++) c.appendChild(b[e]);
            d.appendChild(c);
            return d;

            function f(b, d, c) {
                var a = document.createElement("a");
                a.setAttribute("href", d);
                a.setAttribute("title", c);
                a.appendChild(document.createTextNode(b.toString()));
                return a
            }
            function j() {
                var b = document.createElement("span");
                b.setAttribute("class", a.splitterCssClass);
                return b
            }
        }
        function i(c) {
            var b = document.createElement("span");
            b.setAttribute("class", a.containerCssClass);
            c.parentNode.insertBefore(b, c);
            b.appendChild(c);
            return b
        }
    }
    function d(b, c) {
        var a = b.getAttribute("class");
        return a && a.match(new RegExp("\\b" + c + "\\b"))
    }
    function b(f, e) {
        var g = f.match(/[^/]+\/[^/]+$/)[0];
        b.nextId = (b.nextId || 0) + 1;
        var c = "forkButtonCallback" + b.nextId,
            h = a.githubRepoQuery + g + "?callback=" + c,
            d = document.createElement("script");
        d.setAttribute("src", h);
        document.body.appendChild(d);
        window[c] = function (a) {
            e(a.repository);
            try {
                delete window[c]
            } catch (b) {
                window[c] = null
            }
            d.parentNode.removeChild(d)
        }
    }
}()

