<?php

//error_reporting (E_ALL);

/**
 * @file
 * AJAX comments module file.
 */

/**
 * Implements hook_menu().
 */
function ajax_comments_menu() {
  $items['ajax_comments/reply/%node/%'] = array(
    'page callback' => 'ajax_comments_reply',
    'page arguments' => array(2,3),
    'access callback' => 'node_access',
    'access arguments' => array('view', 2),
    'delivery callback' => 'ajax_deliver',
    'type' => MENU_CALLBACK,
  );
  $items['ajax_comments/edit/cancel/%node/%comment'] = array(
    'page callback' => 'ajax_comments_edit_cancel',
    'page arguments' => array(3,4),
    'access callback' => 'node_access',
    'access arguments' => array('view', 3),
    'delivery callback' => 'ajax_deliver',
    'type' => MENU_CALLBACK,
  );

  $items['ajax_comments/edit/%comment'] = array(
    'page callback' => 'ajax_comments_edit',
    'page arguments' => array(2),
    'access callback' => 'comment_access',
    'access arguments' => array('edit', 2),
    'delivery callback' => 'ajax_deliver',
    'type' => MENU_CALLBACK,
  );

  $items['ajax_comments/delete/%'] = array(
    'page callback' => 'ajax_comments_delete',
    'page arguments' => array(2),
    'access arguments' => array('delete comments'),
    'delivery callback' => 'ajax_deliver',
    'type' => MENU_CALLBACK,
  );
/*
  $items['ajax_comments/comment_form/ajax/%node'] = array(
    'page callback' => 'ajax_comments_comment_form',
    'page arguments' => array(2, 3),
    'access arguments' => array('post comments'),
    'type' => MENU_CALLBACK,
  );
*/

  $items['admin/config/content/ajax_comments'] = array(
    'title' => 'AJAX comments',
    'description' => 'AJAXifies comments on site.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('ajax_comments_settings'),
    'access arguments' => array('administer site configuration'),
    'file' => 'ajax_comments.admin.inc',
  );

  return $items;
}
/**
*Create new permission to remove comment 
*/
function ajax_comments_permission()
{
return array(
    'delete comments' => array(
      'title' => t('Delete own comments'),
      'description' => t('Perform administration tasks for my module.'),
    ),
  );
}


/**
 * Implements hook_views_api().
 */
function ajax_comments_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'ajax_comments'),
  );
}

/**
 * Implements hook_views_data().
 */
function ajax_comments_views_data() {
  
  $data['node']['list_comments'] = array(
    'title' => t('List of comments'),
    'help' => t("Display the node's list of comments."),
    'field' => array(
      'handler' => 'ajax_comments_handler_field_list_comments',
    ),
  );
  
  $data['node']['ajax_comment'] = array(
    'title' => t('AJAX Add Comment'),
    'help' => t('Adds an inline AJAX comment form.'),
    'field' => array(
      'handler' => 'ajax_comments_handler_field_ajax_add_comment',
    ),
  );

  return $data;
}

/**
 * Implements hook_preprocess_node().
 *
 * AJAXify "Add new comment" link when there is no default form.
 */
function ajax_comments_preprocess_node(&$variables) {
  // Only show on full nodes.
  /*
  if ($variables['teaser'] || $variables['view_mode'] != 'full') {
    return;
  }
  */
  $node = $variables['node'];
  $view_mode = $variables['view_mode'];

  if (!ajax_comments_node_type_active($node->type, $view_mode)) {
    return;
  }

  if (variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW) == COMMENT_FORM_BELOW) {
    return;
  }

  if (empty($node->comment) || $node->comment == COMMENT_NODE_CLOSED) {
    return;
  }

  drupal_add_library('system', 'drupal.ajax');

  $variables['content']['links']['comment']['#links']['comment-add']['attributes']['class'] = 'use-ajax';
  $variables['content']['links']['comment']['#links']['comment-add']['href'] = 'ajax_comments/reply/' . $node->nid;
  $variables['content']['links']['comment']['#links']['comment-add']['fragment'] = NULL;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function ajax_comments_form_comment_form_alter(&$form, &$form_state, $form_id) {
  // Check to see if this node type uses ajax comments.
  if (!ajax_comments_node_type_active($form['#node']->type, 'any')) {
    return;
  }
  $node = menu_get_object();
  // there should be a better way to determine the current view mode
  if (is_object($node)){
    foreach($node->content as $field){
      if(!empty($field['#view_mode']) && is_array($field)){
        $view_mode = $field['#view_mode'];
      }
    }
  }
  else {
    // TODO: we are not in a node context and have no clue to modify the form or not
  }
  $micro = microtime();
  $id = drupal_html_id('ajax-comment-'.$micro);
  $form['#attributes'] = array('id' => array($id));
  $form['actions']['submit']['#ajax'] = array(
    'callback' => 'ajax_comments_submit_js',
    'wrapper' => $id,
    'method' => 'replace',
    'effect' => 'fade',
  );
  $form['actions']['preview']['#ajax'] = array(
    'callback' => 'ajax_comments_preview_js',
    'wrapper' => $id,
    'method' => 'replace',
    'effect' => 'fade',
  );
  // If this a reply to comment offer a 'cancel' link for reply. This will just remove the reply form so
  // there is no callback.
  if (isset($form_state['comment']->pid)) {
    $form['actions']['cancel'] = array(
      '#markup' => '<a id=\'ajax-comments-reply-' . $form_state['comment']->pid . '\' class=\'ajax-comments-reply-cancel\' href="#' . $id . '">' . t('Cancel') . '</a>',
      '#weight' => 21,
    );
  }

  $form['#attached'] = array(
    'js' => array(drupal_get_path('module', 'ajax_comments') . '/ajax_comments.js'),
  );
  // HACK, stop ctools from modifying us in node_comment_form.inc
  $form_state['ctools comment alter'] = FALSE;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function ajax_comments_form_comment_confirm_delete_alter(&$form, &$form_state, $form_id) {
  if (ajax_comments_node_type_active(substr($form['#comment']->node_type, strlen('comment_node_')), 'any')) {
    $form['actions']['submit']['#ajax'] = array(
      'callback' => 'ajax_comments_delete_js',
      'wrapper' => $form['#id'],
      'method' => 'replace',
      'effect' => 'fade',
    );

    $form['actions']['cancel']['#attributes']['onclick'][] = 'jQuery(\'#' . $form['#id'] . '\').siblings().show().end().remove(); return false;';
  }
}

/**
 * Previews the comment.
 */
function ajax_comments_preview_js($form, &$form_state) {
  // Return the actual form if it contains errors.
  if (form_get_errors()) {
    return $form;
  }
  
  $comment = comment_form_submit_build_comment($form, $form_state);
  $comment_pre_render = comment_preview($comment);
  $comment_output = drupal_render($comment_pre_render['comment_preview']);
  
  $notify_text = variable_get('ajax_comments_notify', '') ? theme('ajax_comments_notify_text', array('type' => 'preview', 'comment' => $comment)) : '';

  // This is a reply.
  if (isset($form_state['values']['pid'])) {
    $new_form_state = array();
    $new_form_state['build_info']['args'][] = (object) array('nid' => $comment->nid);
    $new_form_state['input'] = $form_state['input'];
    $new_form_state['rebuild_info'] = $form_state['rebuild_info'];
    $form['#action'] = '/ajax_comments/reply/' . $comment->nid . '/' . $comment->pid;
    $new_form_build = drupal_build_form($form['#form_id'], $new_form_state);
    // Don't build comment and body.
    unset($new_form_build['comment_preview']);
    unset($new_form_build['comment_output_below']);
    $new_form_output = drupal_render($new_form_build);

    $commands[] = ajax_command_replace('[action*="/ajax_comments/reply/' . $comment->nid . '/' . $comment->pid . '"]', $new_form_output);
    // TODO: fix this. Needed because action changes after multiple previews.
    $commands[] = ajax_command_replace('[action*="/system/ajax"]', $new_form_output);
    //$commands[] = ajax_command_prepend('#comment-wrapper-' . $comment->pid . ' .comment-form', $notify_text . $comment_output);
    $commands[] = ajax_command_prepend('#comment-wrapper-' . $comment->pid . ' > form[id*="ajax-comment"]', $notify_text . $comment_output);
  }
  // Or is this a brand new comment.
  else {
    if (variable_get('comment_preview_' . $form['#node']->type, DRUPAL_OPTIONAL)) {
      $commands[] = ajax_command_remove('.messages.warning');
    }
    $commands[] = ajax_command_remove('#comment-wrapper-:last');
    //$commands[] = ajax_command_append('.comment-wrapper', $notify_text . $comment_output);
    $commands[] = ajax_command_before('[action*="/comment/reply/' . $comment->nid . '"]', $notify_text . $comment_output);
    $new_form_state = array();
    $new_form_state['build_info']['args'][] = (object) array('nid' => $comment->nid);
    $new_form_state['input'] = $form_state['input'];
    $new_form_state['rebuild_info'] = $form_state['rebuild_info'];
    $new_form_build = drupal_build_form($form['#form_id'], $new_form_state);
    // Don't build comment and body.
    unset($new_form_build['comment_preview']);
    unset($new_form_build['comment_output_below']);
    $new_form_output = drupal_render($new_form_build);

    $commands[] = ajax_command_replace('[action*="/comment/reply/' . $comment->nid . '"]', $new_form_output);

  }

  return array('#type' => 'ajax', '#commands' => $commands);
}

/**
 * Re-grabs comment after editing is cancelled.
 */
function ajax_comments_edit_cancel($node, $comment) {
  $comment_build = comment_view($comment, $node);
  unset($comment_build['#prefix']);
  unset($comment_build['#suffix']);
  $comment_output = trim(drupal_render($comment_build));
  $commands[] = ajax_command_replace('#comment-wrapper-' . $comment->cid . ' form', $comment_output);
  return array('#type' => 'ajax', '#commands' => $commands);
}

/**
 * Builds the comment.
 */
function ajax_comments_submit_js($form, &$form_state) {
  // Return the actual form if it contains errors.
  if (form_get_errors()) {
    return $form;
  }

  // This is to remove the "Your comment has been posted" status message that
  // will appear upon refresh. This seems dirty but it means we don't have to
  // rewrite the whole comment_form_submit(). Please chime in if you think this
  // is dumb.
  ajax_comments_remove_status();

  $comment = $form_state['comment'];
  $node = $form['#node'];
  $notify_text = variable_get('ajax_comments_notify', '') ? theme('ajax_comments_notify_text', array('comment' => $comment)) : '';

  $comment_build = comment_view($comment, $node);

  if (variable_get('ajax_comments_notify')) {
    $commands[] = ajax_command_remove('.comment-preview');
    $commands[] = ajax_command_remove('.messages.warning');
    $commands[] = ajax_command_remove('.messages.status');
  }

  // Don't display as a preview as this is being submitted.
  unset($comment_build['comment_body']['#object']->in_preview);
  unset($form_state['comment_preview']);

  // Are we editing a comment.
  if (isset($form['cid']['#value'])) {
    // Show notify
    if (!empty($notify_text)) {
      $commands[] = ajax_command_before('#comment-wrapper-' . $comment->cid, $notify_text);
    }
    // Remove wrapper because the form we replace exists inside the wrapper.
    unset($comment_build['#prefix']);
    unset($comment_build['#suffix']);

    // Trim surrounding whitespace so ajax.js doesn't wrap us in a new div.
    $comment_output = trim(drupal_render($comment_build));
    $commands[] = ajax_command_replace('#' . $form['#attributes']['id']['0'], $comment_output);
  }
  // Or are we replying to another comment.
  elseif (isset($form_state['values']['pid'])) {
    // Append comment to parent wrapper.
    $comment_output = drupal_render($comment_build);
    $commands[] = ajax_command_append('#comment-wrapper-' . $comment->pid, $notify_text . $comment_output);
    // Delete the form.
    $commands[] = ajax_command_invoke('#' . $form['#attributes']['id']['0'], 'remove');
  }
  // If this is being submitted via the views ajax add comment field.
  // TODO: this isn't working anymore since we depreciated: ajax_comments_comment_form
  /*
  elseif (isset($form_state['comment']->views_ajax_submission)){
    $message = t('Your comment has been posted.');
    $commands[] = ajax_command_replace('#' . $form['#id'], $message);
  }
  */
  // Or is this a brand new comment?
  else {
    // Append comment to root comment wrapper.
    $comment_output = drupal_render($comment_build);

    if ($node->comment_count == 0) {
      $commands[] = ajax_command_before('.title.comment-form', $notify_text . $comment_output);
    }
    else {
      //$commands[] = ajax_command_after('#comment-wrapper-nid-' . $node->nid . " .ajax-comment-wrapper:last", $notify_text . $comment_output);
      $commands[] = ajax_command_before('.title.comment-form', $notify_text . $comment_output);
    }

    // If we have a default form, update it with a new one.
    if (variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW) == COMMENT_FORM_BELOW) {
      $node = $form['#node'];

      $new_form_state = array();
      $new_form_state['build_info']['args'][] = (object) array('nid' => $node->nid);
      // Don't pull from cache.
      $new_form_state['input'] = array();
      $new_form_build = drupal_build_form($form['#form_id'], $new_form_state);
      $new_form_output = drupal_render($new_form_build);

      $commands[] = ajax_command_replace('#' . $form['#attributes']['id']['0'], $new_form_output);
    }
    // Otherwise, delete it.
    else {
      $commands[] = ajax_command_remove('#' . $form['#attributes']['id']['0']);
    }
  }
  $output = array('#type' => 'ajax', '#commands' => $commands);
  return $output;
}

/**
 * Removes the comment.
 */
function ajax_comments_delete_js($form, &$form_state) {
  $comment = $form['#comment'];

  ajax_comments_remove_status();

  $notify_text = variable_get('ajax_comments_notify', '') ? theme('ajax_comments_notify_text', array('type' => 'delete', 'comment' => $comment)) : '';

  if ($notify_text) {
    $commands[] = ajax_command_remove('.messages.status');
    $commands[] = ajax_command_replace('#comment-wrapper-' . $comment->cid, $notify_text);
  }
  else {
    $commands[] = ajax_command_remove('#comment-wrapper-' . $comment->cid);
  }

  return array('#type' => 'ajax', '#commands' => $commands);
}

/**
 * Implements hook_comment_view().
 */
function ajax_comments_comment_view($comment, $view_mode, $langcode) {

			

  if (ajax_comments_node_type_active(substr($comment->node_type, strlen('comment_node_')), $view_mode)) {
    // Reply.
    if (isset($comment->content['links']['comment']['#links']['comment-reply'])) {
      $comment->content['links']['comment']['#links']['comment-reply']['attributes']['class'] = array('use-ajax', 'ajax-comment-reply');
      $comment->content['links']['comment']['#links']['comment-reply']['attributes']['id'] = array('reply-' . $comment->cid);
      $comment->content['links']['comment']['#links']['comment-reply']['href'] = 'ajax_comments/reply/' . $comment->nid . '/' . $comment->cid;
    }
    // Edit.
    if (isset($comment->content['links']['comment']['#links']['comment-edit'])) {
      $comment->content['links']['comment']['#links']['comment-edit']['attributes']['class'] = array('use-ajax', 'ajax-comment-edit');
      $comment->content['links']['comment']['#links']['comment-edit']['href'] = 'ajax_comments/edit/' . $comment->cid;
    }
    // Delete.
	global $user;
    if (isset($comment->content['links']['comment']['#links']['comment-delete']) || ($comment->uid==$user->uid) ) {
	  $comment->content['links']['comment']['#links']['comment-delete']['title']=t('Delete');
      $comment->content['links']['comment']['#links']['comment-delete']['attributes']['class'] = array('use-ajax');
      $comment->content['links']['comment']['#links']['comment-delete']['href'] = 'ajax_comments/delete/' . $comment->cid;
    }
		
	
  }
}
/**
test function to print array, remove after code
*/
function test($arr)
{
print "<pre>";
print_r($arr);
print "</pre>";
}
/**
 * Implements hook_node_view_alter().
 * Wrap all comments in #comment-wrapper
 */
function ajax_comments_node_view_alter(&$build) {
  $active = ajax_comments_node_type_active($build['#node']->type);
  if($active){
    $build['comments']['comments']['#prefix'] = '<div id="comment-wrapper-nid-' . $build['#node']->nid . '">';
    $build['comments']['comments']['#suffix'] = '</div>';
  }
}

/**
 * Implements hook_panels_pane_content_alter().
 * Wrap all comments in #comment-wrapper
 */
function ajax_comments_panels_pane_content_alter(&$content, $pane, $args, $context) {
  if ($pane->type == 'node_comments') {
    $content->content = '<div id="comment-wrapper-nid-' . $content->delta . '">' . $content->content . '</div>';
  }
}

/**
 * Implements hook_comment_view_alter().
 * Wrap comments and their replies in a #comment-wrapper-(cid) div
 */
function ajax_comments_comment_view_alter(&$build, $view_mode) {
  $comment = $build['#comment'];
  $node = $build['#node'];

  $prefix = '';

  // Close any previous wrapper elements.
  if ($comment->wrappers_to_close > 0) {
    $prefix .= str_repeat('</div>', $comment->wrappers_to_close);
  }

  // Add 'new' anchor if needed.
  if (!empty($comment->first_new)) {
    $prefix .= "<a id=\"new\"></a>\n";
  }

  // Add wrapper tag.
  $indent = $comment->pid != 0 && variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED) == COMMENT_MODE_THREADED;
  $prefix .= '<div class="ajax-comment-wrapper' . ($indent == TRUE ? ' indented' : '') . '" id="comment-wrapper-' . $comment->cid . '">';

  // Add anchor tag.
  $prefix .= "<a id=\"comment-$comment->cid\"></a>\n";

  $build['#prefix'] = $prefix;

  // Close last wrapper element.
  if (!empty($comment->final_wrappers_to_close)) {
    $build['#suffix'] = str_repeat('</div>', $comment->final_wrappers_to_close);
  }
}

/**
 * Prepares entity view.
 */
function ajax_comments_entity_prepare_view($entities, $entity_type, $langcode) {
  if ($entity_type == 'comment') {
    $i = 0;
    $opened = 0;

    foreach ($entities as $id => $entity) {

      $depth = isset($entity->thread) ? count(explode('.', $entity->thread)) - 1 : 0;

      $entity->wrappers_to_close = 0;

      if ($depth > $opened) {
        $opened++;
      }
      else {
        $entity->wrappers_to_close = ($depth == $opened)? 1 : ($opened - $depth)+1;

        while ($depth < $opened) {
          $opened--;
        }
      }
      // This is a very poor hack to get user pictures to show up. Alternatives
      // gladly accepted.
      if (isset($entity->form_build_id)) {
        if (isset($entity->uid) && theme_get_setting('toggle_comment_user_picture')) {
          $result = db_select('users', 'u')
                 ->fields('u', array('picture'))
                 ->condition('uid', $entity->uid, '=')
                 ->execute()
                 ->fetchAssoc();
          $entity->picture = isset($result['picture']) ? $result['picture'] : '';
        }
      }

      $i++;
    }

    $entities[$id]->final_wrappers_to_close = $opened;
  }
}

/**
 * Callback for clicking "reply".
 * Note: $pid is an optional parameter. This functionality is utilized by the
 * "Add new comment" link on pages where there is no default comment form
 * (comment_form_location is COMMENT_FORM_SEPARATE_PAGE)
 */
function ajax_comments_reply($node, $pid = NULL, $flag = 'node') {
  if (!user_access('post comments')) {
    return MENU_ACCESS_DENIED;
  }
  // If there is a pid this is a reply to a comment.
  if ($pid == "NULL") $pid = NULL;
  if (!empty($pid)) {
    if (!user_access('access comments')) {
      return MENU_ACCESS_DENIED;
    }

    // Make sure the comment is valid and published.
    if (!($comments = comment_load_multiple(array($pid), array('status' => COMMENT_PUBLISHED)))) {
      return MENU_NOT_FOUND;
    }
    $comment = $comments[$pid];

    // Make sure the comment belongs to this node.
    if ($comment->nid != $node->nid) {
      return MENU_NOT_FOUND;
    }
  }

  // Build form.
  $form_build = drupal_get_form("comment_node_{$node->type}_form", (object) array('nid' => $node->nid, 'pid' => $pid));

  // Offer 'cancel' link for reply. This will just remove the reply form so
  // there is no callback.
  $form_build['actions']['cancel'] = array(
    '#markup' => '<a id=\'ajax-comments-reply-' . $pid . '\' class=\'ajax-comments-reply-cancel\' href="#' . $form_build['#attributes']['id']['0'] . '">' . t('Cancel') . '</a>',
    '#weight' => 21,
  );
  $form = drupal_render($form_build);

  // Add the new form.
  if (isset($pid)) {
    $commands[] = ajax_command_after('#comment-wrapper-' . $pid . '>.comment', $form);
  }
  else {
    $commands[] = ajax_command_append('#comment-wrapper-nid-' . $node->nid, $form);
  }

  return array('#type' => 'ajax', '#commands' => $commands);
}

/**
 * Callback for clicking "edit".
 */
function ajax_comments_edit($comment) {
  $node = node_load($comment->nid);

  // Build form.
  $form_build = drupal_get_form("comment_node_{$node->type}_form", $comment);
  // Allow cancelling of edit.
  $form_build['actions']['cancel'] = array(
    '#markup' => l(t('Cancel'), 'ajax_comments/edit/cancel/' . $node->nid . '/' . $comment->cid, array('attributes' => array('class' => array('use-ajax')))),
    '#weight' => 21,
  );
  $form = drupal_render($form_build);

  // Replace comment with form.
  $commands[] = ajax_command_html('#comment-wrapper-' . $comment->cid, $form);

  return array('#type' => 'ajax', '#commands' => $commands);
}

/**
 * Callback for clicking "delete".
 */
function ajax_comments_delete($cid) {

  if (!($comment = comment_load($cid))) {
    return MENU_NOT_FOUND;
  }

  // Need to include comment module admin file for delete form.
  $form_state = array();
  $form_state['build_info']['args'] = array($comment);

  // Load this using form_load_include so it's cached properly and works in the
  // ajax callback.
  form_load_include($form_state, 'inc', 'comment', 'comment.admin');
  $form_build = drupal_build_form('comment_confirm_delete', $form_state);
  $form = drupal_render($form_build);

  // Hide contents.
  $commands[] = ajax_command_invoke('#comment-wrapper-' . $cid . ' >*', 'hide');

  // Put form inside main comment wrapper.
  $commands[] = ajax_command_prepend('#comment-wrapper-' . $cid, $form);

  return array('#type' => 'ajax', '#commands' => $commands);
}

/**
 * Removes "Your comment has been posted." or "Your comment has been queued.."
 *   from the status message.
 */
function ajax_comments_remove_status() {
  $deleted = t('The comment and all its replies have been deleted.');
  $published = t('Your comment has been posted.');
  $not_published = t('Your comment has been queued for review by site administrators and will be published after approval.');
  foreach ($_SESSION['messages']['status'] as $key => $value) {
    if ($value == $published || $value == $not_published || $value = $deleted) {
      unset($_SESSION['messages']['status'][$key]);
    }
  }
  if (empty($_SESSION['messages']['status'])) {
    unset($_SESSION['messages']['status']);
    if (empty($_SESSION['messages'])) {
      unset($_SESSION['messages']);
    }
  }
}

/**
 * Returns TRUE if this node uses ajax comments or if no nodes are selected.
 */
function ajax_comments_node_type_active($node_type, $view_mode = '') {
  $types = array_filter(variable_get('ajax_comments_node_types', array()));
  $view_modes = variable_get('ajax_comments_view_modes', array());
  // return TRUE if nothing select
  if (empty($types) && empty($view_modes[$view_mode])) {
    return TRUE;
  }
  // return TRUE if node type and view mode match
  if (!empty($types[$node_type]) && !empty($view_modes[$view_mode])) {
    return TRUE;
  }
  // return TRUE if node type match but mode is empty (hack for altered forms)
  if (!empty($types[$node_type]) && empty($view_modes[$view_mode])) {
    return TRUE;
  }
}

 /**
  * Implements hook_theme().
  */
function ajax_comments_theme($existing, $type, $theme, $path) {
  return array(
    'ajax_comments_notify_text' => array(
      'variables' => array('type' => NULL, 'comment' => NULL),
    ),
  );
}

 /**
  * Returns text to notify user their comment has been added.
  */
function theme_ajax_comments_notify_text($vars = array()) {
  $text = t('Your comment has been posted');
  $status = 'status';
  // If the comment is unapproved, alter the message
  if (isset($vars['comment']) && !$vars['comment']->status) {
    $text = t('Your comment has been queued for review by site administrators and will be published after approval.');
    $status = 'warning';
  }
  if ($vars['type'] == 'delete') {
    $text = t('Your comment has been deleted');
  }
  elseif ($vars['type'] == 'preview') {
    $text = t('This is the preview for your comment. You must click SAVE or your comment will be lost.');
    $status = 'warning';
  }
  drupal_set_message($text, $status);
  return theme('status_messages');
}

/**
 * Delivers comment form via AJAX.
 * This is depreciated due to usage of ajax_comments_reply() 
 */
 /*
function ajax_comments_comment_form($type = 'ajax', $node) {
  // Validate token.
  if (empty($_GET['tok']) || !drupal_valid_token($_GET['tok'], 'ajax-comments-add-comment-' . $node->nid)) {
    return MENU_ACCESS_DENIED;
  }

  // Return rendered form.
  if ($type == 'ajax') {
    // Build form.
    $comment = new stdClass;
    $comment->nid = $node->nid;
    $comment->views_ajax_submission = TRUE;
    $form = drupal_get_form('comment_form', $comment);
    
    $markup = drupal_render($form);
    // Construct AJAX.
    //$commands[] = ajax_command_replace('.ajax-comments-add-comment-wrapper:has(.ajax-comments-add-comment-nid-' . $node->nid . ')', $markup);
    $commands[] = ajax_command_replace('.ajax-comments-add-comment-nid-' . $node->nid, $markup);
    $page = array('#type' => 'ajax', '#commands' => $commands);
    ajax_deliver($page);
  }
  // Return normal page callback.
  else {
    drupal_goto('node/' . $node->nid);
  }
}
*/
