/* BOF Class TwitterFeed */
function TwitterFeed()
{
	// Store all tweets in array
  this.tweets = new Array();
	this.tweet_pointer = 0;
	this.page = 1;
	
	this.init = function(keyword, refresh_rate)
	{
	  /* Whether to add the first 3 tweets or not */
	  this.add = true;
	  this.keyword = keyword;
	  this.get_tweets();
	  
	  var instance = this;
	  setInterval(function(){instance.slide()}, refresh_rate*1000);
    return this;
	}
	
	this.get_tweets = function()
	{
	  /**
	  Changed from search API to user timeline
	  */
    // var url = "http://search.twitter.com/search.json?q=%23"+this.keyword+"&rpp=100&page="+this.page+"&callback=?";
    var url = "http://twitter.com/statuses/user_timeline/19038794.json?count=100&callback=?";
    instance = this;
  	$.getJSON(url, function(data)
  	{
      // if(data.results[0])
      if(data[0])
  	  {
        // $.each(data.results, function(i, item)
        $.each(data, function(i, item)
    	  {
    	    instance.tweets.push(item);
    	  });
      
	      if(instance.add)
	      {
          // $("#tweets #loader").remove();
      	  // Populate twitter feed
        	for(i=0; i<4; i++)
        	{
        	  instance.add_tweet(instance.tweets[i]);
        	  instance.tweet_pointer++;
        	}
        }
      }
      else
      {
        instance.tweet_pointer = 0;
      }
  	});
    return instance;
  }
  
  this.slide = function()
  {
    if(this.tweet_pointer < this.tweets.length-1)
    {
      instance = this;
      $("div#tweets div:first-child").fadeOut("slow", function(){
        $("div#tweets div:first-child").remove();
        instance.tweet_pointer++;
        instance.add_tweet();
        return instance;
      });
    }
    else
    {
      // this.add = false;
      // this.get_tweets(this);
      this.tweet_pointer = 0;
    }
    return this;
  };

  /**
  Changed this function to add twteet from user timeline
  */
  this.add_tweet = function()
  {
    this.tweet = this.tweets[this.tweet_pointer];
    user = this.tweet.from_user ? this.tweet.from_user : this.tweet.user.screen_name;
    img = this.tweet.profile_image_url ? this.tweet.profile_image_url : this.tweet.user.profile_image_url;
    
    var $tweet_div = $("<div/>").attr({"id": this.tweet.id}).css({"display": "none"});
    $tweet_div.append($("<img/>").attr({"src": img, "alt": this.tweet.from_user}));
    $tweet_div.append($("<a>").text(user).attr({"class": "username", "href": "http://www.twitter.com/"+user}));
    $tweet_div.append($("<p>").html(this.tweet.text.replace(/@(\w*)/, "<a href=\"http://www.twitter.com/$1\">@$1</a>")));
    $tweet_div.append($("<p>").html("Posted " + this.time_since(this.tweet.created_at) + " by " + user).attr({"class": "since"})).fadeIn("slow");
    //  + " via " + tweet.source.replace(/<a\b[^>]*>(.*?)<\/a>/i,"$1")
    $("div#tweets").append($tweet_div);
  }

  this.time_since = function(a)
  {
      var b = new Date();
      var c = new Date(a);
      if (navigator.userAgent.match(/MSIE\s([^;]*)/)) {
          c = Date.parse(a.replace(/( \+)/, ' UTC$1'))
      }
      var d = b - c;
      var e = 1000,
          minute = e * 60,
          hour = minute * 60,
          day = hour * 24,
          week = day * 7;
      if (isNaN(d) || d < 0) {
          return ""
      }
      if (d < e * 7) {
          return "right now"
      }
      if (d < minute) {
          return Math.floor(d / e) + " seconds ago"
      }
      if (d < minute * 2) {
          return "about 1 minute ago"
      }
      if (d < hour) {
          return Math.floor(d / minute) + " minutes ago"
      }
      if (d < hour * 2) {
          return "about 1 hour ago"
      }
      if (d < day) {
          return Math.floor(d / hour) + " hours ago"
      }
      if (d > day && d < day * 2) {
          return "yesterday"
      }
      if (d < day * 365) {
          return Math.floor(d / day) + " days ago"
      } else {
          return "over a year ago"
      }
  };
}
/* EOF Class TwitterFeed */