// Twitter Widget
// May 1, 2009
// Steve Graham
// NextSelection

if (!window.NS) {
 NS = {}; 
}

NS.Twitter = {
  // Inserts Twitter feed into DOM.
  getTimeline : function(user, count){
    // Throw exception is user is not given
    if (typeof user == 'undefined' ) throw("Argument Error: user is required argument for NS.Twitter.getTimeline");
    // Set count variable to 10 if value is not passed at invocation.
    if (typeof count == 'undefined' ) count = '10';
    var timelineURL = 'http://twitter.com/statuses/user_timeline/' + user + '.json?callback=NS.Twitter.callBack&count=' + count;
    var script = document.createElement('script');
    script.setAttribute('src', timelineURL);
    script.setAttribute('type', 'text/javascript');
    document.body.appendChild(script);
  },
  // Parses out username into link to twitter profile page. @ symbol not part of link as per behavior of official Twitter widget.
  parseUsername: function(string){ 
    return string.replace(/\B@([_\w])+/g, function(match) { var user = match.substr(1); return match.charAt(0) + user.link('http://www.twitter.com/' + user) } ); 
  },
  // Parses URLs and replaces with links.
  parseURL: function(string){
    return string.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(match) { return match.link(match) } );
  },
  // ActionView::Helpers::DateHelper#distance_of_time_in_words
  niceTime: function(time){
    timeNow = new Date();
    time = time.split(' ');
    time = time[1] + " " + time[2] + ", " + time[5] + " " + time[3];
    time = Date.parse(time);
    time = parseInt((timeNow.getTime() - time) / 1000);
    time = time + timeNow.getTimezoneOffset() * 60;
    if (time < 60) {
      return 'about ' + time + ' seconds ago';
    } else if (time < 120){
      return 'about a minute ago'
    } else if (time < 60*60) {
      return parseInt(time / 60).toString() + ' minutes ago';
    } else if (time < 120*60) {
      return 'about an hour ago'
    } else if (time < 24*60*60) {
      return 'about ' + parseInt(time / 3600).toString() + ' hours ago ';
    } else if (time < 48*60*60) {
      return 'yesterday'
    } else {
      return parseInt(time / 86400).toString() + ' days ago';
    }
  },
  callBack: function(tweet){
    for (var i=0; i < tweet.length; i++) {
      // If the tweet text begins with an @reply, skip and progress to next tweet.
      if (tweet[i].text.match(/^@\w*/)) continue;
      var processedTweet = NS.Twitter.parseURL(tweet[i].text);
      processedTweet = NS.Twitter.parseUsername(processedTweet);
      document.getElementById('twitter_update_list').innerHTML = '<span>' + processedTweet + '</span>' + '<a href="http://twitter.com/' + tweet[i].user.screen_name + '/statuses/' + tweet[i].id + '"> - ' + NS.Twitter.niceTime(tweet[i].created_at) + '</a>';
      // Break out of loop as soon as the most recent tweet, that is not a reply is found.
      break;
    };
  },
  init: function(){
    NS.Twitter.getTimeline('kubaoms');
  },
}

window.onload = NS.Twitter.init;