/* All related comment actions will be managed here */
/* appending comment see views/comments/create.js.erb */

$(function(){

  // declare variables for link, button, form ids
  var viewCommentForm = '#view_comment_form';
  var commentForm = '#comment_form';
  var newComment = '#new_comment';

  toggleLink(commentForm);  // initially hide comment form

  // show/hide form when user clicks on comment
  $('h3').on('click', viewCommentForm, function(e) {
    e.preventDefault();
    toggleLink(commentForm);
  });

  // edit form text when user clicks on comment
  $('h3').on('click', viewCommentForm, function(e) {
    e.preventDefault();
    $(viewCommentForm).text(toggleLinkText($(viewCommentForm)));  // set text depending on current text
  });

  // remove comments form when user submits
  $(commentForm).on('submit', newComment, function(e) {
    e.preventDefault();
    toggleLink(commentForm);
    $(viewCommentForm).text(toggleLinkText($(viewCommentForm)));
  });

});

/*** Helper Methods Below ***/

// toggle between show and hidden
function toggleLink(selector) {
  $(selector).toggle("medium");
}

// toggle text on link/button depending on what is currently set
function toggleLinkText(linkText) {
  text = linkText.text();
  var text = text === "Add a comment" ? "Cancel comment" : "Add a comment";
  return text;
}

@@ -644,49 +644,23 @@
     
-$(viewCommentForm).text(toggleLink
+changeForm
 Text(
-$(
 view
@@ -675,12 +675,9 @@
 orm)
-)); 
+;
  // 
@@ -850,78 +850,187 @@
     
-toggleLink(commentForm);%0A    $(viewCommentForm).text(toggleLink
+clearFormText();                                      // clear form contents%0A    toggleLink(commentForm);                              // remove form from view%0A    changeForm
 Text(
-$(
 view
@@ -1045,11 +1045,57 @@
 orm)
-));
+;                      // toggle link/button text
 %0A  %7D
@@ -1452,12 +1452,246 @@
 urn text;%0A%7D%0A
+%0A// change text on comment form link/button%0Afunction changeFormText(form) %7B%0A  $(form).text(toggleLinkText($(form)));%0A%7D%0A%0A// clear tiny mce form text after user submits%0Afunction clearFormText() %7B%0A  tinyMCE.activeEditor.setContent('');%0A%7D

Clean up file and remove editor text on submit