(function($) {

 

  $.fn.tweet = function(o){

    var s = {

      username: ["MikuniSushi"],			              // [string]   required, unless you want to display our tweets. :) it can be an array, just do ["username1","username2","etc"]

      avatar_size: 0,                      // [integer]  height and width of avatar if displayed (48px max)

      count: 1,                               // [integer]  how many tweets to display?

      intro_text: null,                       // [string]   do you want text BEFORE your your tweets?

      outro_text: null,                       // [string]   do you want text AFTER your tweets?

      join_text:  null,                       // [string]   optional text in between date and tweet, try setting to "auto"

      auto_join_text_default: "",      // [string]   auto text for non verb: "i said" bullocks

      auto_join_text_ed: "",                 // [string]   auto text for past tense: "i" surfed

      auto_join_text_ing: "",             // [string]   auto tense for present tense: "i was" surfing

      auto_join_text_reply: "",   // [string]   auto tense for replies: "i replied to" @someone "with"

      auto_join_text_url: "", 		// [string]   auto tense for urls: "i was looking at" http:...

      loading_text: "Loading Tweet...",                     // [string]   optional loading text, displayed while tweets load

      query: null                             // [string]   optional search query

    };



    $.fn.extend({

      linkUrl: function() {

        var returning = [];

        var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;

        this.each(function() {
          returning.push(this.replace(regexp,"<a href=\"$1\" target=\"_blank\">$1</a>"))
        });

        return $(returning);

      },

      linkUser: function() {

        var returning = [];

        var regexp = /[\@]+([A-Za-z0-9-_]+)/gi;

        this.each(function() {
          returning.push(this.replace(regexp,"<a href=\"http://twitter.com/$1\" target=\"_blank\">@$1</a>"))
        });

        return $(returning);

      },

      linkHash: function() {

        var returning = [];

        var regexp = / [\#]+([A-Za-z0-9-_]+)/gi;

        this.each(function() {

          returning.push(this.replace(regexp, ' <a href="http://search.twitter.com/search?q=&tag=$1&lang=all&from='+s.username.join("%2BOR%2B")+'">#$1</a>'))

        });

        return $(returning);

      },

      capAwesome: function() {

        var returning = [];

        this.each(function() {

          returning.push(this.replace(/(a|A)wesome/gi, 'AWESOME'))

        });

        return $(returning);

      },

      capEpic: function() {

        var returning = [];

        this.each(function() {

          returning.push(this.replace(/(e|E)pic/gi, 'EPIC'))

        });

        return $(returning);

      },

      makeHeart: function() {

        var returning = [];

        this.each(function() {

          returning.push(this.replace(/[&lt;]+[3]/gi, "<tt class='heart'>&#x2665;</tt>"))

        });

        return $(returning);

      }

    });



    if(o) $.extend(s, o);

    return this.each(function(){
		$(this).empty();

      var list = $('<ul class="tweet_list">').appendTo(this);

      var intro = '<p class="tweet_intro">'+s.intro_text+'</p>'

      var outro = '<p class="tweet_outro">'+s.outro_text+'</p>'

      var loading = $('<p class="loading">'+s.loading_text+'</p>');

      if(typeof(s.username) == "string"){

        s.username = [s.username];

      }

      var query = '';

      if(s.query) {

        query += 'q='+s.query;

      }

      query += '&q=from:'+s.username.join('%20OR%20from:');

      var url = 'http://search.twitter.com/search.json?&'+query+'&rpp='+s.count+'&callback=?';

      if (s.loading_text) $(this).append(loading);

      $.getJSON(url, function(data){

        if (s.loading_text) loading.remove();

        if (s.intro_text) list.before(intro);

        $.each(data.results, function(i,item){



          var text = $([item.text]).linkUrl().linkUser().linkHash().makeHeart().capAwesome().capEpic()[0];

		  var retweet = 'http://twitter.com/home/?status=' + escape('RT @MikuniSushi+' + item.text);

          

          list.append('<li>' + text + '</li>');

          list.append('<li class="retweet"><a href="' + retweet + '" target="_blank">retweet</a> <a href="http://twitter.com/MikuniSushi" target="_blank">follow</a></li>');



        });

        if (s.outro_text) list.after(outro);

      });



    });

  };

})(jQuery);