(function($) {
  $.fn.tweet = function(o){
    var s = {
      username: null,                         // [string]   required, it can be an array ["username1","username2","etc"]
      count: 1,                               // [integer]  how many tweets to display?
      join_text:  null,                       // [string]   optional text in between date and tweet, try setting to "auto"
      loading_text: null,                     // [string]   optional loading text, displayed while tweets load
      prepend_text: ""                        // [string]   optional text, displayed before tweets
    };
        
    if(o) $.extend(s, o);
    return this.each(function(){
      var list = $('<ul class="tweet_list">').appendTo(this);
      var loading = $('<p class="loading">'+s.loading_text+'</p>');
      if(typeof(s.username) == "string"){
        s.username = [s.username];
      }
      var url = 'http://search.twitter.com/search.json?q=from:'+s.username.join('%20OR%20from:')+'&rpp='+s.count+'&callback=?';
      if (s.loading_text) $(this).append(loading);
      $.getJSON(url,  function(data){
        if (s.loading_text) loading.remove();
        $.each(data.results, function(i,item){
          var join_text = s.join_text;
          var join = '<span class="tweet_join"> '+join_text+' </span>';

          list.append('<li><span class="tweet_text">' + s.prepend_text + item.text.replace(/(\w+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&#\?\/.=]+)/gi, '<a href="$1">$1</a>').replace(/[\@]+([A-Za-z0-9-_]+)/gi, '<a href="http://twitter.com/$1">@$1</a>').replace(/ [\#]+([A-Za-z0-9-_]+) /gi, ' <a href="http://search.twitter.com/search?q=&tag=$1&lang=all&from='+s.username.join("%2BOR%2B")+'">#$1</a> ').replace(/[&lt;]+[3]/gi, "<tt class='heart'>&#x2665;</tt>") + '</span></li>');
        });  
        $('.tweet_list li:odd').addClass('tweet_even');
        $('.tweet_list li:odd').addClass('tweet_even');
        $('.tweet_list li:even').addClass('tweet_odd');
        $('.tweet_list li:first').addClass('tweet_first');
        $('.tweet_list li:last').addClass('tweet_last');
      });
    });
  };
})(jQuery);

