(function(){
    var CommentReactions = Class.create({
        initialize: function() {
          this.reactionLinks = $$(".reaction_link");
          this.foldLinks = $$(".fold");
          this.comments = $$(".comment");
          this.newCommentDiv = $("new_comment");
          this.commentedOn = $("commented_on");

          this.bindEvents();
        },

        bindEvents: function() {
          this.reactionLinks.each(function(singleReaction){
              singleReaction.on("click", "a", this.goToCopyCommentDivClickEvent.bind(this));
            }, this);

          $('resetNewCommentForm').observe('click', this.resetNewCommentDiv);
        },

        goToCopyCommentDivClickEvent: function(event){
          event.stop();
          var parentComment = event.srcElement.up('div').up('div');
          var commentId = parentComment.id;
          this.commentedOn.value = commentId;
          var selectedComment = $(commentId).select(".comment_content")[0];
          selectedComment.insert({'after': this.newCommentDiv});
        },

        resetNewCommentDiv: function(event){
          //event.stop();

          $("commented_on").value = '';
          comments = $("comments");
          comments.insert({'after': $('new_comment')});
        }

    });    

    document.observe("dom:loaded", function(){
        if(!$('resetNewCommentForm')){ return };
        
        window.comment_reactions = new CommentReactions();
    });
})();

