(function($) {

  $.fn.tweetHud = function(o){
    var s = {
      username: null,                   // username
      showAvatar:true,                  // show avatar image ?
      showDate:true,
      avatar_width: 48,                 // [integer] width of avatar image if displayed
      avatar_height: 48,                 // [integer] height of avatar image if displayed
      count: 5,                         // [integer]  how many tweets to display?
      loading_text: null,               // [string]   optional loading text, displayed while tweets load
      query: null                       // [string]   optional search query
    };

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

    $.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\">$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\">@$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' + (s.username ? '&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);
      }
    });

    return this.each(function(){
      var contentDiv = $('<div>').appendTo(this);
      var loading = $('<p class="loading">'+s.loading_text+'</p>');
      if(s.username && typeof(s.username) == "string"){
        s.username = [s.username];
      }
      var query = '';
      if(s.query) {
        query += 'q='+ escape(s.query);
      }

      if (s.username) {
        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();
        $.each(data.results, function(i,item){
          
        	var searched = '<p class="cwy fb24u plr6">' + s.query + '</p>'; 
        	var text = '<p class="cwb hudTweetText">' +$([item.text]).linkUrl().linkUser().linkHash().makeHeart().capAwesome().capEpic()[0]+ '</p>';
        	var divClear = '<div class="clear"><!-- --></div>';

          // until we create a template option, arrange the items below to alter a tweet's display.
          contentDiv.append(searched + text);
        });
      });

    });
  };
})(jQuery);
