/**
 * TwitterReader Class
 *
 * This class leverages Twitter Search API to retrieve recent Twitter statuses
 * with specified keyword.
 *
 * @author  Jakub Karlec
 * @created 08/14/2009
 * @updated 08/14/2009
 */
var TwitterReader = new Class({

  Implements: Options,

  //basic options
  options: {
    url: 'http://search.twitter.com/search.json?',
    rpp: '20',
    since_id: null,
    loader: new Element('div',{'text':'loading ...', 'class':'feedLoader'})             // loader element
  },

//  class variables
//  url: 'http://search.twitter.com/search.json?',  // twitter search address
//  rpp: 20,                    // number of retrieved results
//  since_id: null,             // if set only newer tweets are retrieved
//  loader                      // loader element to be attached
//  
//  container: null,            // container element or element id
//  keyword: null,              // searched term
//  tweets: [],                 // fetched tweets
  
  /**
   * Class constructor
   */
  initialize: function(container, keyword, options){
    this.setOptions(options);
    this.container = $(container);
    this.keyword = keyword;
    this.tweets = [];

    this.retrieveItems();
  },


  /**
   * Retrieve feed items
   */
  retrieveItems: function(){
    this.options.loader.inject(this.container, 'top');
    new Request.JSONP({url: this.options.url,
      data: {
        q: this.keyword,
        rpp: this.options.rpp,
        since_id: this.options.since_id
      },
      onComplete: function(data){
        this.options.loader.dispose();
        var totalTweets = this.tweets.length;
        if(data.max_id > 0){
          this.options.since_id = data.max_id;
        }
        $each(data.results, function(item){
          this.tweets.include(item);
        }, this);
        if(totalTweets < this.tweets.length){
          this.showItems(totalTweets);
        }
      }.bind(this)
    }).send();
  },

  /**
   * show tweet items
   */
  showItems: function(newItemsIndex){
    if(!this.container.getElement('ul')){
      this.container.adopt(new Element('ul'));
    }
    var list = this.container.getElement('ul');
    for(var i = newItemsIndex; i < this.tweets.length; i++){
      tweet = this.tweets[i]
      var listItem = new Element("li",{'html':'<img src="'+tweet.profile_image_url+'" alt="" /><a href="http://twitter.com/'+tweet.from_user+'">'+tweet.from_user+'</a>:<p>'+this.chat_string_create_urls(tweet.text)+'</p>'});
      listItem.inject(list, 'top');
      listItem.set('tween',{duration:2000}).highlight();      
    }
  },

  /**
   * Taken from Tori's eyes page: http://toriseye.quodis.com/
   *
   * Parse tweets to make links to links, @usernames and #hashtags
   */
  chat_string_create_urls: function(input) {
      return input
      .replace(/(ftp|http|https|file):\/\/[\S]+(\b|$)/gim, '<a href="$&">$&</a>')
      .replace(/([^\/])(www[\S]+(\b|$))/gim, '$1<a href="http://$2">$2</a>')
      .replace(/(^|\s)@(\w+)/g, '$1<a href="http://twitter.com/$2">@$2</a>')
      .replace(/(^|\s)#(\S+)/g, '$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>');
  }

});