Index: modules/ajax_comments/ajax_comments.js
===================================================================
--- modules/ajax_comments/ajax_comments.js	(revision 1324)
+++ modules/ajax_comments/ajax_comments.js	(revision 1328)
@@ -243,9 +243,8 @@
     form_action = $('#comment-form').attr('action');
     link_cid = ajax_comments_get_cid_from_href(action);
     rows = Drupal.settings.ajax_comments_rows_default;
+
     if ($('#comment-form-content').attr('cid') != link_cid) {
-      // We should remove any WYSIWYG before moving controls.
-      ajax_comments_remove_editors();
 
       // Move form from old position.
       if (ajax_comments_is_reply_to_node(action)) {
@@ -281,6 +280,9 @@
 
     // We don't need to load everything twice.
     if (!$(this).is('.last-clicked')) {
+      // We should remove any WYSIWYG before moving controls.
+      ajax_comments_remove_editors();
+
       // Reload form if preview is required.
       if ((Drupal.settings.comment_preview_required && $('#ajax-comments-submit').length) ||
         // Or if quoted comment or custom reload trigger.
@@ -292,10 +294,11 @@
         $('.editing').fadeTo('fast', 1);
       }
       else {
-        ajax_comments_rewind(link_cid, rows);
+        ajax_comments_rewind(link_cid, rows, true);
         ajax_comments_scroll_to_comment_form();
       }
     }
+
     // ...and show the form after everything is done.
     ajax_comments_expand_form();
     
@@ -332,6 +335,10 @@
   $edit_link.parents(commentbox).fadeTo('fast', 0.5).addClass('editing');
 
   ajax_comments_show_progress();
+
+  // We should remove any WYSIWYG before moving controls.
+  ajax_comments_remove_editors();
+
   $('#comment-form-content').addClass('indented');
   if ($('#comment-form-content:visible').length) {
     $('#comment-form-content').after('<div style="height:' + $('#comment-form-content').height() + 'px;" class="sizer"></div>');
@@ -453,7 +460,8 @@
  */
 Drupal.turn_over_page = function(target, url, scroll, success_callback, error_callback) {
   if (target.length && url) {
-    ajaxPath = url.replace(/(.*?)\?(.*?)/g, Drupal.settings.basePath + 'ajax_comments/js_load_thread/' + Drupal.settings.ajax_comments_nid + '?$2');
+    // page=num may not be in the url
+    ajaxPath = url.replace(/(.*?)(\?.*)?$/g, Drupal.settings.basePath + 'ajax_comments/js_load_thread/' + Drupal.settings.ajax_comments_nid + '$2');
     $.ajax({
       url: ajaxPath,
       type: 'GET',
@@ -463,12 +471,19 @@
             offset = $('#comments').offset();
             $('html').animate({scrollTop: offset.top}, 'slow');
           }
-          target.parent().parent().animate({opacity: 0.2}, 'fast', function() { 
+          target.parent().parent().animate({opacity: 0.2}, 'fast', function() {
+            // Remove editors before replacing DOM and re-attaching behaviors
+            ajax_comments_remove_editors();
+            
             var $newContent = $(response.content);
+            // we remove comment controls
+            $('#comment-controls,#comment-controls-1,#comment-controls-2',$newContent).remove();
+            
             target.parent().parent().replaceWith($newContent);
             firsttime_init = true;
+            
             Drupal.attachBehaviors($newContent.parent());
-
+            
             if (scroll) {
               offset = $('#comments').offset();
               $('html').animate({scrollTop: offset.top}, success_callback);
@@ -520,7 +535,10 @@
  * @param $rows
  *
  */
-function ajax_comments_rewind(pid, rows){
+function ajax_comments_rewind(pid, rows, attach){
+
+  if(typeof(attach)=='undefined') attach=true;
+  
   // Resizing and clearing textarea.
   $('#comment-form textarea').attr('rows', rows);
   $('#comment-form:not(.fresh) textarea').attr('value','');
@@ -533,7 +551,9 @@
   $('#comment-form input[name=pid]').val(pid)
 
   // Now we can attach previously removed editors.
-  ajax_comments_attach_editors();
+  if(attach)
+    ajax_comments_attach_editors();
+
   submit = false;
 }
 
@@ -543,15 +563,16 @@
 function ajax_comments_close_form(reload) {
   ajax_comments_remove_editors();
   pid = $('#comment-form-content').attr('cid');
-  $('#comment-form-content').animate({height:'hide'}, speed, function(){
-    if (reload) {
-      ajax_comments_reload_form(pid, 'pid', function() {
-        if (Drupal.settings.ajax_comments_always_expand_form) {
-          $('#comment-form-title').click();
-        }
-      });
-    }
-  });
+  // Can't do animation. ahah attachBehaviors is sometimes executed before reload_form
+  $('#comment-form-content').hide();
+  if (reload) {
+    ajax_comments_reload_form(pid, 'pid', function() {
+      if (Drupal.settings.ajax_comments_always_expand_form) {
+        $('#comment-form-title').click();
+      }
+    });
+  }
+  
   $('.pressed').removeClass('pressed');
   $('#comment-form-content').attr('cid', -1);
   ajax_comments_hide_progress();
@@ -564,9 +585,9 @@
   rows = Drupal.settings.ajax_comments_rows_default;
   if (type == 'pid') {
     action = $('#comment-form').attr('action');
-    action = action.replace(/comment\/reply\/([0-9]+?)(\/*[0-9]*)$/, 'ajax_comments/js_reload/$1$2');
-    action = action.replace(/ajax_comments\/js_reload\/([0-9]+?)\/([0-9]+?)\/cid/, 'ajax_comments/js_reload/$1');
-
+    //var quoted = action.match('quote=1');
+    action = action.replace(/comment\/reply\/([0-9]+?)(\/*[0-9]*)(\?.*)?$/, 'ajax_comments/js_reload/$1$2$3');
+    action = action.replace(/ajax_comments\/js_reload\/([0-9]+?)\/([0-9]+?)\/cid(\?.*)?/, 'ajax_comments/js_reload/$1$2$3');
     if (id > 0) {
       action = action.replace(/([?])$/, '/' + id + '?');
       action = action.replace(/#comment-form/, '');
@@ -593,8 +614,10 @@
         $('#comment-form').attr('class', saved_class);
 
         $('#comment-form').addClass('fresh');
+
+        // attachBehaviors calls wysiwygAttach
         Drupal.attachBehaviors($('#comment-form-content'));
-        ajax_comments_rewind(id, rows);
+        ajax_comments_rewind(id, rows, false);
         ajax_comments_hide_progress();
 
         $('#comment-form').removeClass('fresh');
@@ -634,16 +657,24 @@
     $('.editing').remove();
     $('#comment-form-content').attr('cid', 0);
   }
-  else if ($('#comment-form-content').attr('cid') <= 0) {
-    if ($('#comments .pager').length) {
-      $('#comment-preview').prev('.item-list').before($comment);
+  else if (($('#comment-form-content').attr('cid') <= 0) || !Drupal.settings.ajax_comments_nested) { // insert as last
+    var new_id = $(".comment",$comment).attr('id');
+    var $where;
+
+    if (!Drupal.settings.ajax_comments_oldest_first) {
+      $where = $('#comments .comment:not(#'+new_id+'):first').before($comment)
+      if(!$where.length)
+        $where = $('#comments > :last').after($comment)
     }
     else {
-      $('#comment-preview').before($comment);
+      $where = $('#comments .comment:not(#'+new_id+'):last')
+      if(!$where.length)
+        $where = $('#comment-preview');
+      $where.after($comment);
     }
   }
-  else {
-    if ($('#comment-form-content').next().is('.indented')) {
+  else { // reply as a nested comment
+    if (Drupal.settings.ajax_comments_oldest_first && $('#comment-form-content').next().is('.indented')) {
       $('#comment-form-content').next().append($comment);
     }
     else {
@@ -696,14 +727,25 @@
     $('#comment-preview').slideUp(speed, function(){ $(this).empty(); });
 
     // If there are more than one page in comments tthread, we should firstly turn over to a last page.
-    $last_page = $('#comments .pager:first li.pager-last a');
-    if (!$last_page.length) {
-      $last_page = $('#comments .pager:first li.pager-next').prev().children('a');
+    if(Drupal.settings.ajax_comments_oldest_first){
+      $last_page = $('#comments .pager:first li.pager-last a');
+      if (!$last_page.length) {
+        $last_page = $('#comments .pager:first li.pager-next').prev().children('a');
+      }
     }
+    else{
+      $last_page = $('#comments .pager:first li.pager-first a');
+      if (!$last_page.length) {
+        $last_page = $('#comments .pager:first li.pager-previous').next().children('a');
+      }
+    }
+
     if ($('#comment-form-content').attr('cid') <= 0 && $last_page.length) {
       Drupal.turn_over_page($('#comments .pager:first'), $last_page.attr('href'), false, function(){}, function(){ $(this).removeClass('throbber'); });
     }
     else {
+      // we can't assume everything is loaded after submitting a form
+      $("a.ajax-comments-processed").removeClass('last-clicked');
       // Place new comment in proper place.
       ajax_comments_insert_new_comment(obj);
 
Index: modules/ajax_comments/ajax_comments.module
===================================================================
--- modules/ajax_comments/ajax_comments.module	(revision 1324)
+++ modules/ajax_comments/ajax_comments.module	(revision 1328)
@@ -89,7 +89,9 @@
   $mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE);
   $languages = language_list('enabled');
   $languages = $languages[1];
-
+  $order = _comment_get_display_setting('sort', $node);
+  $mode = _comment_get_display_setting('mode', $node);
+  
   drupal_add_js('misc/jquery.form.js');
   drupal_add_js($path .'/ajax_comments.js', 'module');
   drupal_add_js(array(
@@ -102,6 +104,8 @@
     'ajax_comments_always_expand_form' => variable_get('ajax_comments_always_expand_form', TRUE),
     'ajax_comments_blink_new' => variable_get('ajax_comments_blink_new', TRUE),
     'comment_bonus_api_fold_comments' => variable_get('comment_bonus_api_fold_comments', FALSE),
+    'ajax_comments_oldest_first' => ($order == COMMENT_ORDER_OLDEST_FIRST) ? 1 : 0,
+    'ajax_comments_nested' => ($mode == COMMENT_MODE_THREADED_COLLAPSED|| $mode==COMMENT_MODE_THREADED_EXPANDED) ? 1 : 0,
     'ajax_comments_nid' => $node->nid,
   ), 'setting');
 }
@@ -174,6 +178,7 @@
  * Comment submit routine.
  */
 function ajax_comments_submit($form, &$form_state) {
+  $output = "";
   //remove self
   unset($form_state['submit_handlers']);
   // ..and standart comments submit handler
@@ -210,7 +215,7 @@
       if ($comment = db_fetch_object($result)) {
         $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
         $links = module_invoke_all('link', 'comment', $comment, 0);
-        drupal_alter('link', $links, $node);
+        drupal_alter('link', $links, $node, $comment);
 
         //render our comment and get it back to AHAH handler
         $output .= theme('comment_view', $comment, $node, $links);
@@ -237,11 +242,7 @@
 
   if ($all_allowed || isset($allowed_node_types[$node->type]) && $allowed_node_types[$node->type]) {
     if (is_array($links) && isset($links['comment_delete']) && isset($comment->cid)) {
-      $links['comment_delete'] = array(
-        'title' => t('delete'),
-        'href' => 'comment/delete/'. $comment->cid,
-        'query' => 'token='. drupal_get_token($comment->cid),
-      );
+      $links['comment_delete']['query'] = 'token='. drupal_get_token($comment->cid);
     }
   }
 }
@@ -252,17 +253,6 @@
 function ajax_comments_preprocess_comment(&$variables) {
   $comment = $variables['comment'];
 
-  // Insert proper delete links (fix until hook_link_alter will not work propertly)
-  if ($variables['links'] && user_access('administer comments')) {
-    $links = module_invoke_all('link', 'comment', $comment, 0);
-    $links['comment_delete'] = array(
-      'title' => t('delete'),
-      'href' => 'comment/delete/'. $comment->cid,
-      'query' => 'token='. drupal_get_token($comment->cid),
-    );
-    $variables['links'] = isset($links) ? theme('links', $links) : '';
-  }
-
   if (!$comment->cid) {
     $variables['new'] = t('preview');
     $variables['comment']->new = TRUE;
@@ -270,4 +260,4 @@
   else {
     $variables['title'] = l($comment->subject, 'node/' . $comment->nid, array('attributes' => array('class' => 'active'), 'fragment' => "comment-$comment->cid"));
   }
-}
+}
\ No newline at end of file
Index: modules/ajax_comments/ajax_comments.pages.inc
===================================================================
--- modules/ajax_comments/ajax_comments.pages.inc	(revision 1324)
+++ modules/ajax_comments/ajax_comments.pages.inc	(revision 1328)
@@ -42,7 +42,7 @@
   $form_state['post'] = $form['#post'] = $_POST;
   $form['#programmed'] = $form['#redirect'] = FALSE;
 
-  if ($form_state['post']['op'] == $form_state['values']['preview']) {
+  if ($form_state['post']['op'] == t('Preview')) {
     $form['#after_build'] = array('comment_form_add_preview');
   }
   else {
@@ -208,8 +208,9 @@
 function ajax_comments_js_load_thread($nid) {
   global $conf;
   $node = node_load($nid);
-  // Hide comments form from thread.
-  $conf['comment_controls_'. $node->type] = COMMENT_CONTROLS_HIDDEN;
+  // We cannot hide controls because we'll get a wrong comments_per_page value.
+  // $conf['comment_controls_'. $node->type] = COMMENT_CONTROLS_HIDDEN;
+
   // Render the thread.
   $response = array(
     'status' => TRUE,
