(function($) {
 
  $.fn.tweet = function(o){
    var s = {
      username: ["lakshmivyas","ringce","slammerapp"],
      count: 3,
      loading_text:"loading",
      faveMode: false,
      completion_handler: null
    };

    $.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 class=\"tweet-link\" 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 class=\"tweet-user\" href=\"http://twitter.com/$1\">@$1</a>"))
        });
        return $(returning);
      },
      linkHash: function() {
        var returning = [];
        var regexp = / [\#]+([A-Za-z0-9-_]+)/gi;
        this.each(function() {   
          var user = 
          returning.push(this.replace(regexp, ' <a class="tweet-hash" href="http://search.twitter.com/search?q=&tag=$1&lang=all&from='+s.username.join("%2BOR%2B")+'">#$1</a>'))
        });
        return $(returning);
      }
    });
   

    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 = "";
      if (s.faveMode){
          url = 'http://twitter.com/favorites/' +  s.username[0] + '.json?callback=?';
      }  else {
          
          var query = '';
          query += '&q=from:'+s.username.join('%20OR%20from:');
          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);    
        var results = s.faveMode? data : data.results;
        $.each(results, function(i,item){      
          var userName = s.faveMode ? item.user.screen_name : item.from_user;  
          var tweet = '<a class="tweet-tweet" href="http://twitter.com/'+
                        userName +
                        '/statuses/' + item.id + 
                        '" title="See this on twitter">';
                        
          if (s.faveMode) {
              tweet += "-&nbsp;" + item.user.name + "</a>"; 
          }  else {
              tweet +='<img src="/media/images/source.png" width="16px" height="16px"/></a>';
          }
          var text = '<span class="tweet_text">' +$([item.text]).linkUrl().linkUser().linkHash()[0]+ '</span>';
          
          // until we create a template option, arrange the items below to alter a tweet's display.   
          if (s.faveMode){
              list.append('<li>' +  text  + tweet + '</li>');                            
          }  else {
              list.append('<li>' + tweet + text + '</li>');              
          }


          list.children('li:first').addClass('tweet_first');
          list.children('li:odd').addClass('tweet_even');
          list.children('li:even').addClass('tweet_odd');
        });
        if (s.outro_text) list.after(outro);  
        if (s.completion_handler) {
            s.completion_handler();
        }
      });

    });
  };
})(jQuery);