///////////
// tweet //
///////////
tr.tweet = function(){
  var _cid = 0;
  return {
    /**
    * extend an object as a tweet
    **/
    extend : function(object){
      object.cid = _cid++;
      return Object.extend(object, this.methods);
    },
    
    /**
    * utilty methods for tweets
    **/
    methods : {
      
      /**
      * this tweet needs conversion
      **/
      convert : function() {
        if (this.converting) return;
        this.converting = true;
        new Ajax.Request('/convert', {
          method      : 'post',
          parameters  : {
            tweet : Object.toJSON(this) 
          }
        });
      },
      
      /**
      * url for this tweet
      **/
      url : function() {
        if (window.location.host.match(/tweetrad\.io/)) {
          return 'http://audio.tweetrad.io/tweets/' + this.id + '.mp3?ts=' + new Date().getTime();
        } else {
          return '/tweets/' + this.id + '.mp3?ts=' + new Date().getTime();
        }
      },
      
      /**
      * add to crawler
      **/
      addToCrawler : function(){
        var elem = new Element('DIV');
        elem.innerHTML = this.toHTML();
        $(document.body).addClassName('has_tweet');
        $('tweets').insert({top : elem});
        if (!tr.app.singleTweetMode) {
          setTimeout(function(){elem.morph('opacity:0', {duration:1, after : function(){elem.remove();}});}, 60000);
        }
      },
      
      /**
      * to html
      **/
      toHTML : function(){
        var tmpl = new Template('\
          <div class="tweet">\
            <div class="avatar"><img src="#{profile_image_url}" height="50" width="50"></a></div>\
            <div class="text"><span class="from_user">#{from_user}</span>\
            #{text}&nbsp;&nbsp;<br />\
            <span class="timestamp">#{timestamp}</span>\
            <a href="javascript:void(tr.app.share(#{twid}))">tweet this!</a></div>\
          </div>\
        ');
        
        ts = humane_date((this.created_at || '').gsub(/\+0000/,''));
        return tmpl.evaluate({
          profile_image_url : this.user ? this.user.profile_image_url : this.profile_image_url,
          from_user : this.from_user,
          text : this.text.autoLink(),
          twid : this.id,
          timestamp : ts || ''
        });
      }
    }
  };
}();
