(function() {
  var $, defaults;

  $ = jQuery;

  $.fn.extend({
    Twidget: function(options) {
      var self, settings;
      self = $.fn.Twidget;
      settings = $.extend(defaults, options);
      return this.each(function(i, el) {
        return self.init(el, settings);
      });
    }
  });

  $.extend($.fn.Twidget, defaults = {
    username: null,
    showNum: null,
    height: 250,
    debug: false,
    atify: true
  }, {
    init: function(el, options) {
      this.opts = options;
      $(el).css('height', this.opts.height);
      this.createLoader(el);
      this.log("initting " + el);
      if (!(this.opts.username != null)) {
        return this.log('You must specify a username.');
      } else {
        return this.getAvatar(el);
      }
    },
    createLoader: function(el) {
      var html;
      this.log("attaching loader to " + el);
      html = "<div id='twidget-loader'></div>";
      return $(el).append(html);
    },
    getAvatar: function(el) {
      var t, url;
      url = "http://api.twitter.com/1/users/show.json?include_entities=true&screen_name=" + this.opts.username + "&callback=?";
      t = this;
      return $.getJSON(url, function(r) {
        var html;
        html = "<div id='twitter-title'>";
        html += "<img src='" + r.profile_image_url + "' />";
        html += "<span>@<a href='http://twitter.com/" + t.opts.username + "' target='_blank'>" + t.opts.username + "</a> on Twitter</span>";
        html += "</div>";
        $(el).before(html);
        return t.getTweets(el);
      });
    },
    getTweets: function(el) {
      var t, url;
      url = "http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=" + this.opts.username + "&callback=?";
      t = this;
      return $.getJSON(url, function(r) {
        $.each(r, function(i, v) {
          var html;
          html = "<div class='tweet' id='tweet" + i + "'><p>" + v.text + "</p></div>";
          return $(el).append(html);
        });
        if (t.opts.atify) t.atifyTweets(el);
        return t.showContent(el);
      });
    },
    showContent: function(el) {
      var hght, num;
      if (this.opts.showNum != null) {
        hght = 0;
        num = this.opts.showNum + 1;
        while (num -= 1) {
          hght += $("#tweet" + (num - 1)).height() + 24;
        }
        $(el).css('height', "" + (hght - 13) + "px");
      }
      return $('#twidget-loader').remove();
    },
    atifyTweets: function(el) {
      return $(el).atify({
        hashtag: true,
        links: true
      });
    },
    log: function(msg) {
      if (this.opts.debug) {
        return typeof console !== "undefined" && console !== null ? console.log(msg) : void 0;
      }
    }
  });

}).call(this);

