tr.twitter = function(){
  return {
    // status codes
    FAILURE  : 'failure',
    SUCCESS  : 'success',
    
    baseUrl  : 'http://search.twitter.com/search.json',
    trendUrl : 'http://search.twitter.com/trends/current.json',
    callback : 'tr.twitter.onComplete',
    
    /**
    * get a tweet by id
    **/
    getTweet : function(id) {
      this._writeScriptTag('http://twitter.com/status/show/' + id + '.json?callback=tr.twitter.onSingleTweetResponse');
    },
    
    /**
    * get trending topics
    **/
    getTrends : function() {
      var qs = $H({
        callback : 'tr.twitter.onTrendResponse'
      });
      this._writeScriptTag(this.trendUrl + '?' + qs.toQueryString());
    },
    
    shareTweet : function(url) {
      var qs =$H({
        callback : 'tr.twitter.onShortenResponse',
        apiKey   : 'R_bb91b1d6a11e76a87499da8a340632ef',
        login    : 'tweetradio',
        version  : '2.0.1',
        format   : 'json',
        longUrl  : url
      });
      this._writeScriptTag('http://api.bit.ly/shorten' + '?' + qs.toQueryString());
    },
    
    /**
    * search twitter with the given query
    **/
    search : function(query) {
      var qs = $H({
        q : query,
        rpp : 3,
        callback : this.callback
      });
      if (!query) return;
      this._writeScriptTag(this.baseUrl + '?' + qs.toQueryString());
    },
    
    /**
    * get next page
    **/
    refresh : function() {
      if (!this.refreshParams) return;
      this._writeScriptTag(this.baseUrl + this.refreshParams + '&callback=' + this.callback);
    },
    
    /**
    * get next page of results
    **/
    nextPage : function() {
      if (!this.nextPageParams) return;
      this._writeScriptTag(this.baseUrl + this.nextPageParams + '&callback=' + this.callback);
      this.nextPageParams = null;
    },
    
    /**
    * success json received
    **/
    onComplete : function(json) {
      if (!json) return;
      this.refreshParams = json.refresh_url;
      if (json.next_page) this.nextPageParams = json.next_page;
      tr.data.store(json.results);
      if (json.results.length === 0 && this.nextPageParams) this.nextPage();
    },
    
    /**
    * get the trends
    **/
    onTrendResponse : function(json) {
      if (json && json.trends) {
        var trends = $('trends');
        Object.values(json.trends).first().each(function(t){
          var elem = new Element('A');
          elem.innerHTML = t.name.toUpperCase();
          elem.href='http://tweetrad.io?q=' + t.query.gsub(/#/,'%23');
          // if (this._addDelimiter) {
          //   trends.appendChild(new Element('span').update('|').addClassName('delimiter'))
          // }
          // this._addDelimiter = true;
          trends.appendChild(elem);
        });
        trends.insert({bottom : '<br style="clear:both"/>'});
        // $('trends_and_instructions').setStyle({visibility:'visible'});
      }
    },
    
    /**
    * single tweet response
    **/
    onSingleTweetResponse : function(json) {
      if (!json) return;
      this.refreshParams = json.refresh_url;
      this.nextPageParams = json.next_page;
      tr.data.store([json]);
    },
    
    /**
    * received a short url
    **/
    onShortenResponse : function(json) {
      shortUrl = Object.values(json.results).first().shortUrl;
      qs = $H({
        status : 'I heard it on tweetrad.io ' + shortUrl
      });
      window.open('http://twitter.com?' + qs.toQueryString(), '', 'resizable=yes,toolbar=yes,location=yes,status=yes,scrollbars=yes,top=0,left=0,width=880,height=800');
    },
    
    /**
    * write out a script tag
    **/
    _writeScriptTag : function(url) {
      var script = new Element('script', {src : url});
      document.body.appendChild(script);
    }
  };
}();