(function($){
    /**
     * @todo Find a way to calculate the dates relatively
     * @todo Test is a cookie plugin is available so that some of this data can be 'cached'
     */
    $.jTweet = function(tag, total, Callbacks) {
        var url = "http://search.twitter.com/search.json?q=%23" + tag;
        if (Callbacks.last_id) {
            url += "&since=" + Callbacks.last_id;
        } 
        Callbacks.old_id = Callbacks.last_id
        url += "&rpp=" + total;
        $.getJSON(url + "&callback=?", function(response) {
            $.each(response["results"], function(i, tweet) {
                if (tweet.id > Callbacks.last_id) {
                    Callbacks.last_id = tweet.id;
                }
                if (tweet.id > Callbacks.old_id) {
                    var text = tweet.text.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
                    return m.link(m);
                });
                    $.jGrowl( text , { header: '<img src="' + tweet.profile_image_url + '" width=32> <h2>' + tweet.from_user + '</h2>', life: 15000 - i*1000} );
                    }
            });
        });
    };
})(jQuery);


