clicked = [];
$(document).ready(function(){
    /* request thumb data for all games */
    updateGameCollection();

    $(".game_link_oneup").each(function(){
        $(this).click(function(){
            /* check if this is clicked */
            if ($(this).hasClass("not_clickable")) return false;

            /* extract game id from url */
            var gameid = $(this).attr("href");
            /* get the digits */
            gameid = gameid.match(/\d+/);

            /* check if it has been clicked. quit if it has been. */
            var clicked = getClickedGames();
            if (clicked.indexOf(parseInt(gameid)) >= 0) return false;

            /* do the incrementation */
            $.post('/api/thumbs/increment/' + gameid);

            /* find current count */
            var count = $(this).find(".count:first").text();

            /* add 1 */
            var newcount = parseInt(count) + 1;

            /* display the new count to user */
            $(this).find(".count:first").text(newcount);

            /* push the clicked gameid to our window clicked object */
            pushClickedGames(parseInt(gameid));

            /* add not clickable class. lazy :S */
            $(this).addClass("not_clickable");
            return false;
        });
    });
});
function updateGameCollection(){
    /* fetch the json with the csv created in scripts_footer */
    if ( typeof gamecsv != 'undefined' )
        $.getJSON('/api/thumbs/total?' + gamecsv, function(data){
            $.each(data.thumbs, function(id, count) {

                /* iterate over the returned data */
                $(".gameid-" + id).each(function(){

                    /* update the count */
                    $(this).find(".count:first").text(count);
                });
            });
        });
}
function getClickedGames(){
    return clicked;
}
function pushClickedGames(id){
    clicked.push(id);
}

