// ==UserScript==
// @name           GoogleTwitterMashup
// @namespace      Designalism
// @description    Google search + twitter search mashup
// @include        http://*.google.*/search?*
// ==/UserScript==


// ---- Hide ad panel and insert results div before we load jQuery. It's faster this way. ----

// nifty function to dynamically inject <style> into <head>
function add_style_tag(rules) {
    var head = document.getElementsByTagName('head')[0],
        style = document.createElement('style'),
        rulenode = document.createTextNode(rules);
        
    style.type = 'text/css';
    if(style.styleSheet)
        style.styleSheet.cssText = rulenode.nodeValue;
    else style.appendChild(rulenode);
    head.appendChild(style);
}

// inject custom style tag into header
var rules = "";
rules += "#tsr {border-left: 1px solid rgb(201, 215, 241); padding-left: 10px; float: right; width: 35%;}";
rules += "li.result {padding: 5px 10px 10px 5px;}"
rules += "div.timestamp {color: #AAAAAA;}";
add_style_tag(rules);

// if #mbEnd already exists then remove it
var mbEnd = document.getElementById("mbEnd");
if (mbEnd) mbEnd.parentNode.removeChild(mbEnd);

// Create new div for twitter search results and append before #res
var tsr = document.createElement("div");
tsr.id = "tsr";
var spinner = "<div id='spinner'><h2><img src='http://img413.imageshack.us/img413/8999/ajaxloaderbs5.gif' /> searching Twitter </h2></div>"
tsr.innerHTML = "<ol class='results'>"+spinner+"</ol>";
var res = document.getElementById("res");
res.parentNode.insertBefore(tsr, res);


// ---- Now load jQuery, which we need to get the JSON and manipulate the results ----

// <snip --------------------->
//jQuery wrapper from http://wiki.greasespot.net/Code_snippets
//"Use jQuery in a GreaseMonkey script"
var $;
var GM_JQ = document.createElement('script');
with(GM_JQ) {
    src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js';
    type = 'text/javascript';
}
document.body.previousSibling.appendChild(GM_JQ);
var checker=setInterval(function(){
    if(typeof unsafeWindow.jQuery != "undefined") {
        $ = unsafeWindow.jQuery;
        clearInterval(checker);
        runWithJQuery();
    }
}, 100);
// </snip --------------------->

function runWithJQuery() {
    // grab the search term from the google page, tokenise it at join with + for twitter
    var q = $("input[name=q]")[0].value;
    var esc_q = q.split(' ').join('+');
    
    // get twitter search results and add the first 10 to the results <ol>
    $.getJSON("http://search.twitter.com/search.json?q="+esc_q+"&callback=?", function(data) {
        $("#spinner").hide();
        $("#tsr > ol.results").append("<li><h2>Twitter search for '"+$("input[name=q]").text(q).html()+"'</li>");
        
        $.each(data.results, function(i, result) {
            var r = "";
            r += "<li class='result'>";
            r += "<a target='_none' href='http://twitter.com/"+result.from_user+"'>"+"<strong>"+result.from_user+"</strong>"+"</a>";
            r += " ";
            r += result.text.replace(/(\bhttp[s]?\:\/\/.*?(\s|$))/ig, "<a target='_none' href='$1'>$1</a>");
            r += "<div class='timestamp'>"+result.created_at+"</div>";
            r += "</li>";
            
            $("#tsr > ol.results").append(r);
            
            if (i==10) return false;
        });
    });
}
