Tweets = {
  tweeters: ['rupanews', 'drew_mitchell', 'faingaatwins', 'quadecooper', 'jamesoconnor832', 'kurtley_beale', 
                  'adamcoopy', 'jhorwill', 'nathansharpe5', 'pocockdavid', 'fanga9', 'tatafro', 'salesimafu',
                  'jamesslipper', 'stephenmoore83', 'digbyioane', 'benn_robbo', 'adam_freier', 'ewen_mckenzie',
                  'hill_damien', 'richardgraham15'],
  
  load: function(element) {
    var tweeter = this.tweeters[Math.floor(Math.random()*this.tweeters.length)];    
    $.ajax({
      url: 'http://api.twitter.com/1/statuses/user_timeline.json/',
      type: 'GET',
      dataType: 'jsonp',
      data: {
        screen_name: tweeter,
        include_rts: true,
        count: 10,
        include_entities: true
      },
      success: function(data, status, xhr) {
        if(data && data.length > 0) {
          var html = '<li class="tweet" data-id="TWEET_ID"><div class="thumb"><a class="popup" href="USER_URL"><img src="AVATAR_URL"/></a></div>';
          html += '<div class="details"><h5><a class="popup" href="USER_URL">USERNAME</a> <span>(AGO)</span></h5><p>TWEET</p></div></li>';          
          $(element).find('li.loading').remove();          
          $.each(data, function(i, tweet) {
            var text = tweet.text;
            if(tweet.truncated && tweet.retweeted_status) {
              text = tweet.retweeted_status.text;
            }
            $(element).append(html.replace('TWEET_ID', tweet.id_str)
                                  .replace('USERNAME', tweet.user.name)
                                  .replace('AGO', Tweets.ago(tweet.created_at))
                                  .replace('AVATAR_URL', tweet.user.profile_image_url)
                                  .replace(/USER_URL/g, 'http://twitter.com/#!/' + tweet.user.screen_name)
                                  .replace('TWEET', Tweets.ify.clean(text)));
          });
        } else {
          $(element).find('li.loading').html('Oops! Unable to load tweets.');
          $(element).find('li.loading').removeClass('loading');
        }
      }
    });
  },

  ago: function(dateString) {
    var rightNow = new Date();
    var then = new Date(dateString);         
    if($.browser.msie) {
      then = Date.parse(dateString.replace(/( \+)/, ' UTC$1'));
    } 
    var diff = rightNow - then; 
    var second = 1000,
    minute = second * 60,
    hour = minute * 60,
    day = hour * 24,
    week = day * 7;
    
    if(isNaN(diff) || diff < 0) {
      return '';
    } 
    if(diff < second * 2) {
      return 'just now';
    } 
    if(diff < minute) {
      return Math.floor(diff / second) + ' seconds ago';
    }
    if(diff < minute * 2) {
      return "about 1 minute ago";
    }
    if(diff < hour) {
      return Math.floor(diff / minute) + ' minutes ago';
    }
    if(diff < hour * 2) {
      return "about 1 hour ago";
    }
    if(diff < day) {
      return Math.floor(diff / hour) + ' hours ago';
    }
    if(diff > day && diff < day * 2) {
      return 'yesterday';
    }
    if(diff < day * 365) {
      return Math.floor(diff / day) + ' days ago';
    } else {
      return 'over a year ago';
    }
  },

  ify:  {
    link: function(tweet) {
      return tweet.replace(/\b(((https*\:\/\/)|www\.)[^\"\']+?)(([!?,.\)]+)?(\s|$))/g, function(link, m1, m2, m3, m4) {
        var http = m2.match(/w/) ? 'http://' : '';
        return '<a class="twtr-hyperlink" target="_blank" href="' + http + m1 + '">' + ((m1.length > 25) ? m1.substr(0, 24) + '...' : m1) + '</a>' + m4;
      });
    },
    
    at: function(tweet) {
      return tweet.replace(/\B[@＠]([a-zA-Z0-9_]{1,20})/g, function(m, username) {
        return '<a target="_blank" class="twtr-atreply" href="http://twitter.com/intent/user?screen_name=' + username + '">@' + username + '</a>';
      });
    },
    
    list: function(tweet) {
      return tweet.replace(/\B[@＠]([a-zA-Z0-9_]{1,20}\/\w+)/g, function(m, userlist) {
        return '<a target="_blank" class="twtr-atreply" href="http://twitter.com/' + userlist + '">@' + userlist + '</a>';
      });
    },
    
    hash: function(tweet) {
      return tweet.replace(/(^|\s+)#(\w+)/gi, function(m, before, hash) {
        return before + '<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23' + hash + '">#' + hash + '</a>';
      });
    },
    
    clean: function(tweet) {
      return this.hash(this.at(this.list(this.link(tweet))));
    }
  }
}

