// ==UserScript==
// @name          EvriTwitterWidget
// @namespace     http://www.evri.com
// @description
// @author        Carsten Tusk, Satish Bhatti
// @homepage      http://www.evri.com
// @include       http://www.evri.com/*
// ==/UserScript==

// Check if jQuery's loaded
function GM_wait() {
	if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
	else { $ = unsafeWindow.jQuery; letsJQuery(); }
}
GM_wait();

// All the GM code must be inside this function
function letsJQuery()
{
    var entityName = encodeURIComponent( $('li#entity-name').html() );
    var rdUrl = 'http://api.evri.com/v1/mashups/twitter/search';

    $.getJSON( rdUrl +'?q=' + entityName + '&format=json&callback=?', null,
    function( data )
    {
        var jsonData = eval(data);
        var value = jsonData['evriThing']['twitterNet'];
        var entryContentArray = getContentArray( value.entries );

        if ( entryContentArray.length > 0 )
        {
            setupDestinationDiv();
            var entityArray = getEntityArray( value.entities );
            insertEntityHrefsIntoTweets( entityArray, entryContentArray );
            $("ul.myList li:nth-child(even)").addClass("even-striped");
            $("ul.myList li:nth-child(odd)").addClass("odd-striped");
        }
    })
}

function setupDestinationDiv()
{
    $('head').append('<style> li.even-striped {background-color: lightGrey; padding: 4px 5px 4px 10px; color: black;} </style>');
    $('head').append('<style> li.odd-striped {padding: 4px 5px 4px 10px; color: black;} </style>');

    $('div.google-ads:last').width( 600 );
    $('div.google-ads:last').prepend('<h2>Twitter Tweets</h2><p class=\'ad_unit\'><ul class="myList"> </ul></p>');
}

function insertEntityHrefsIntoTweets( entityArray, entryContentArray )
{
    $.each(entryContentArray, function(index, content)
    {
        processContent( content, entityArray );
    });
}

function processContent( contentData, entityArray )
{
    // Fix up relative hrefs in content.
//    contentData.content = contentData.content.replace( 'href=\"/', 'href=\"http://twitter.com/' );
    contentData.content = contentData.content.replace( /href=\"\/search/g, 'href=\"http://search.twitter.com/search' );
    contentData.content = contentData.content.replace( /href=\"\//g, 'href=\"http://twitter.com/' );

    $.each(entityArray, function(iname, ivalue)
    {
        contentData.content = contentData.content.replace( ivalue.name.$, getEntityUri( ivalue ) );
    });

    var authorUri = '<a href="' + contentData.authorURI + '">' + contentData.authorName + '</a>'
    $('ul.myList').append( '<li>' + authorUri + ': ' + contentData.content + '</li>');
}

function getContentArray( entries )
{
    var entryContentArray = new Array;
    var entryArry = entries.entry;
    var i = 0;
    $.each(entryArry, function(iname, ivalue)
    {
        if ( ivalue.title )
        {
            entryContentArray[i] = new Object();
            entryContentArray[i].content = ivalue.content.$;
            entryContentArray[i].authorName = ivalue.authorName.$;
            entryContentArray[i].authorURI = ivalue.authorURI.$;
            i++;
        }
    });

    return entryContentArray;
}

function getEntityArray( entities )
{
    return entities.entity;
}

function getEntityUri( entity )
{
    var url = 'http://www.evri.com';
    var str = '<a href="' + url + entity.@href + '">' + entity.name.$ + '</a>'
    return str;
}

