var Comment = Behavior.create({
  initialize: function(){
    if(this.element.down('a.read_comment')) 
      this.element.down('a.read_comment').removeAttribute('onclick');
    if(this.element.down('a.remove_comment'))
      this.element.down('a.remove_comment').removeAttribute('onclick');
  },
  onclick: function(e){
    var link = e.findElement('a');
    if(link.hasClassName('read_comment')){
      e.stop();
      this.readComment(link);
    }
    if(link.hasClassName('close_comment')){
      e.stop();
      this.closeComment();
    }
    if(link.hasClassName('remove_comment')){
      e.stop();
      this.element.highlight({startcolor: '#FBE3E4', duration: 0.4, afterFinish: function(){ this.removeComment(link); }.bind(this)});
    }
  },
  removeComment: function(link){
    new Ajax.Request(link.getAttribute('href'), {
      'method': 'put', 
      parameters: { 'authenticity_token': window._authenticity_token }
      }
    );
  },
  readComment: function(link){
    new Ajax.Request(link.getAttribute('href'), {
        'method':'put', 
        onComplete: this.onComplete.bind(this), 
        parameters: { 'authenticity_token': window._authenticity_token }
      }
    );
  },
  closeComment: function(){
    new Ajax.Request('/comments/' + this.commentID(), {'method': 'get', onComplete: this.onComplete.bind(this)});
  },
  onComplete: function(response){
    var id = this.element.identify();
    Element.replace(this.element.identify(), response.responseText);
    $(id).highlight();
  },
  commentID: function(){ return this.element.identify().split('_').last(); }
});