// Usage:
//   declare GITHUB_USERNAME in your javascript section
//   and insert <div id="github-badge"></div> into html
//   finally require this file and jquery.js
// Courtesy of http://navyblueshellingford.blogspot.com/2008/12/simple-clone-of-github-badge.html

if (typeof jQuery == 'undefined') {
    document.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script>');
}

document.write('<link rel="stylesheet" href="http://drnicjavascript.rubyforge.org/github_badge/dist/ext/stylesheets/badge.css" type="text/css"></link>');

function loadGitHubJson(data) {
    user = data["user"]

    $("#github-badge").empty().append(
        "<div class='header'>" +
            "<span><a href='http://github.com/" + GITHUB_USERNAME +"/'>" + user["login"] + "</a></span>" + "<span calss='title'>'s Projects</span>" +
        "</div>" +
        "<div class='body'>" +
            "<div class='repos'>" +
                "<ul class='repos_listing'>" +
                "</ul>" +
            "</div>" +
        "</div>" +
        "<div class='footer'>Powered by <a href=\"http://navyblueshellingford.blogspot.com/\">shelling</a>, <a href=\"http://navyblueshellingford.googlepages.com/github-widget.js\">get this widget</a></div>"
    );

    $.each(user["repositories"], function(){
        if (this['private'] == false) {
            $("#github-badge .body .repos_listing").append(
                    "<li class='public clickable'>" + 
                        "<img alt='public' src='http://github.com/images/icons/public.png' />" +
                        "<strong><a href='http://github.com/" + GITHUB_USERNAME + "/" + this['name'] + "'>" + this["name"] + "</a></strong>" +
                        "<div class='description' style='display: none;'>" + this["description"] + "</div>" +
                    "</li>"
            )
        }
    });

    $("#github-badge .clickable img").click(function(){
        var target = $(this).parents()[0];
        $(target).children(".description").toggle()
    });
}

$(function(){
    $.getScript("http://github.com/api/v1/json/" + GITHUB_USERNAME +"?callback=loadGitHubJson")
});