// ==UserScript==
// @name           twitterers_on_hatebu
// @namespace      http://jamadam.com/blog/
// @description    This shows social tags on Twitterers
//                 version 0.6
// @include        http://twitter.com/*
// ==/UserScript==

(function() {
        
    var $;
    var jversion = '1.3';
    var jexist = (typeof unsafeWindow.jQuery != 'undefined');
    var conflict = (jexist && unsafeWindow.jQuery.fn.jquery != jversion);
    
    // Add jQuery if not loaded
    if (! jexist || conflict) {
        var GM_JQ = document.createElement('script');
        GM_JQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/' + jversion + '/jquery.js';
        GM_JQ.type = 'text/javascript';
        document.getElementsByTagName('body')[0].appendChild(GM_JQ);
    }
    
    GM_wait();
    
    // Check if jQuery's loaded
    function GM_wait() {
        if (typeof unsafeWindow.jQuery == 'undefined' || unsafeWindow.jQuery.fn.jquery != jversion) {
            window.setTimeout(GM_wait,100);
        } else {
            if (conflict) {
                $ = unsafeWindow.jQuery.noConflict(true);
            } else {
                $ = unsafeWindow.jQuery;
            }
            letsJQuery();
        }
    }
    
    // All your GM code must be inside this function
    function letsJQuery() {
        
        var tagmst = {};

        $.fn.extend({
            showTags : function(id) {

                if (! id && this.attr('href')) {
                    id = (this.attr('href').match(/twitter.com\/(\w+)$/)||[])[1];
                }
                id = id || this.textContent;

                var url = 'http://twitter.com/' + id;

                // cache
                if (tagmst[id] != undefined) {
                    console.log("--cache");
                    this._setTagStr(id);
                }

                // no cache
                else {
                    var obj = this;
                    console.log("--no cache");
                    $.getJSON(
                        "http://b.hatena.ne.jp/entry/jsonlite/?callback=?&url="
                            + encodeURIComponent(url), function(json) {

                        var res = new Object();
                        var not_empty;
                        if (json) {
                            for (var i = 0; i < json.bookmarks.length; i++) {
                                var tags = json.bookmarks[i].tags;
                                for (var j = 0; j < tags.length; j++) {
                                    if (tags[j].match(/twitter/i)) continue;
                                    res[tags[j]] = (res[tags[j]] || 0) + 1;
                                    not_empty = true;
                                }
                            }
                        }
                        
                        if (! not_empty) {
                            res = {'No Tag' : 1};
                        }
                        
                        tagmst[id] = res;
                        obj._setTagStr(id);
                    });
                }
                return this;
            },

            // タグ表示のバックエンド
            _setTagStr : function(id) {

                if (this.next('.twitteres-on-hatebu').length) return;

                for (var key in tagmst[id]) {
                    $("<a>" + key + "</a>")
                        .attr('class', 'twitteres-on-hatebu')
                        .attr('href', "http://jamadam.com/th/?t=tag&str=" + encodeURIComponent(key) + "&nn=1")
                        .attr('target', "_blank")
                        .css({
                            'color':'#eee',
                            'background-color':'#55d',
                            'margin':2,
                            'padding':2,
                            'font-size':'12px',
                            'display':'none'
                        })
                        .fadeIn()
                        .insertAfter(this);
                }
                return this;
            }
        });

        // ユーザーページ専用処理
        var id = (document.location.toString().match(/twitter.com\/(\w+)$/)||[])[1];
        if (id) {
            $('div.screen-name').each(function(){
                $(this).showTags(id);
            });
        }

        // 一般要素
        $('.screenname, .screen-name, .username').live('mouseover', function(){
            $(this).showTags(null);
        });
    }
})();

