(function(jQuery){
  jQuery.fn.twitter = function (options) {
    var opts = jQuery.extend({}, jQuery.fn.twitter.defaults, options);
    var _this = this;
    var _i = -1;
    var _tweets = [];

    var _rotate = function() {
      _i++;
      if(_i==0) {
        $(_this).html("<div>"+_tweets[_i]+"</div>").fadeIn(opts.speed,_rotate);
        return;
      }
      if(_i==_tweets.length) _i=0;
      $(_this).fadeOut(opts.speed,function(){
        $(_this).html("<div>"+_tweets[_i]+"</div>").fadeIn(opts.speed,_rotate);
      });
    };

    var _htmlify = function (text) {
      var htmltext = text
        .replace(/(https?:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)/g, '<a href="$1" target="_blank" class="'+(opts.linkclass)+'">$1</a>')
        .replace(/@([a-zA-Z0-9_]+)/g, '<a href="http://twitter.com/$1" target="_blank" class="'+(opts.linkclass)+'">@$1</a>')
        .replace(/#([a-zA-Z0-9_]+)/g, '<a href="http://twitter.com/search?q=%23$1" target="_blank" class="'+(opts.linkclass)+'">#$1</a>');
      return htmltext;
    };

    jQuery.ajax({
      url: 'http://twitter.com/statuses/user_timeline/' + opts.user + '.json',
      data: {count:opts.count},
      dataType: 'jsonp',
      success: function (o) {
          if(o.length==0) return false;
          $.each(o, function(index, value) {
            _tweets.push(_htmlify(value.text));
          });
          _rotate();
        }
      });
    return true;
  };

  jQuery.fn.twitter.defaults = {
    user: "PwC_LLP",
    count: 5,
    speed: 5000,
    linkclass: "twitter-link"
  };
})(jQuery);

