// ==UserScript==
// @name           Bottom Comment Link
// @descritpion    Add "Comment", "Like", "Hide" links AFTER long threads of comments
// @namespace      http://kapranoff.ru
// @include        http://friendfeed.com/*
// @exclude        http://friendfeed.com/account*
// @exclude        http://friendfeed.com/settings*
// @version        0.21
// ==/UserScript==

var BOTTOM_COMMENT_LINK_THRESHOLD = 6;

(function() {

window.frf_process_entry = function() {
    var entry = unsafeWindow.$(this);

    var comments = unsafeWindow.$('.comments > .comment', entry).length;
    var exp_comments = unsafeWindow.$('.comments > .expandcomment > a', entry);
    if (exp_comments) {
        var match;
        if (match = /^(\d+) /.exec(exp_comments.text()))
            comments += parseInt(match[1]);
    }

    if (comments > BOTTOM_COMMENT_LINK_THRESHOLD) {
        var new_info = unsafeWindow.$('<div class="info">');

        new_info.append(unsafeWindow.$('div.info > a.l_comment', entry).clone())
                .append(unsafeWindow.$('div.info > span.like',   entry).clone())
                .append(' - ')
                .append(unsafeWindow.$('div.info > a.l_hideone', entry).clone())
        ;

        entry.append(new_info);
    }
}

window.frf_process = function() {
    unsafeWindow.$('div.entry').each( frf_process_entry );
};

window.frf_process();

if (typeof unsafeWindow.autoRefresher == 'undefined') {
    window.setTimeout(function() { window.frf_process(); }, 2000);
}
else {
    var old_dprint = unsafeWindow.dprint;
    unsafeWindow.dprint = function(msg) {
        old_dprint(msg);
        if (msg.match(/^updating /i))
            window.setTimeout(function() { window.frf_process(); }, 50)
    };
}

})();

