// ==UserScript==
// @name           FriendFeed In-place Preview
// @description    Open links in iframes
// @namespace      http://kapranoff.ru
// @include        http://beta.friendfeed.com/*
// @include        http://friendfeed.com/*
// @exclude        http://friendfeed.com/account*
// @exclude        http://friendfeed.com/settings*
// @exclude        http://beta.friendfeed.com/account*
// @exclude        http://beta.friendfeed.com/settings*
// @version        0.2
// ==/UserScript==

(function() {

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

    var a = unsafeWindow.$('div.title > a, div.link > a', entry);
    if (!a || a.length < 1) return;

    var preview_link = unsafeWindow.$('<a>Preview</a>')
        .attr('href', a.attr('href'))
        .click(function() {
            if (unsafeWindow.$('iframe', entry).toggle().length < 1) {
                var iframe = unsafeWindow.$('<iframe class="expando" />')
                    .attr('src', a.attr('href'))
                    .width(unsafeWindow.$("td#main, div#body").width() - 200)
                    .height(300)
                    .css('border', 'black solid 1px');
                unsafeWindow.$('div.info', entry).after(iframe);
            }
            return false
        });

    unsafeWindow.$('div.info', entry).append(' - ', preview_link);
}

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)
    };
}

})();

