<!--

// @TODO: 1. Set button names with current locale
// @TODO: 2. Make form field generator

// Current form token
var drupal_form_token = '';
// Quick comments form field list
var form_fields = new Array('comment', 'nid', 'form_token', 'form_id');


// Get Node container for child object
function getNodeDiv(t_o) {
  while (typeof t_o != 'undefined') {
    if ((typeof t_o['tagName']   != 'undefined') && (t_o['tagName']   == 'DIV') && $(t_o).hasClass('node')) {
      return t_o;
    }
    t_o = t_o['parentNode'];
  }
  return null;
}

// Get Node ID for child object
function getNodeId(t_o) {
  if (t_node = getNodeDiv(t_o)) {
    return t_node.id;
  }
  return false;
}


// Add quick comments form to the page
function quickCommentsForm(t_a) {

  if (!(t_node = document.getElementById(t_a['node_id']))) {
    return false;
  }

  // Remove other quick comments forms
  clearForm();

  var t_node_id = parseInt(t_a['node_id'].replace('node-', ''));

  var t_div = document.createElement('div');
  t_div.id        = 'quickcomments_' + t_node_id;
  t_div.sending   = false;
  t_div.className = 'node-form';

  // Create form
  var t_form = document.createElement('form');
  t_form.id = 'quickcomments_form';
  t_form.style.margin = '0';
  t_form.style.padding = '0';
  t_form.onsubmit = function() {
    return submitQuick(this);
  }

  // Create 'User comments' field and append it to the form
  var t_area = document.createElement('textarea');
  t_area.name = 'comment';
  t_area.id   = 'field_comment';
  t_area.style.width = '80%';
  t_area.style.height = '100px';
  t_form.appendChild(t_area);

  // Create 'Node ID' field and append it to the form
  var t_nid = document.createElement('input');
  t_nid.type = 'hidden';
  t_nid.name = 'nid';
  t_nid.id   = 'field_nid';
  t_nid.value = t_node_id;
  t_form.appendChild(t_nid);

  // Create 'Form token' field and append it to the form
  var t_form_token = document.createElement('input');
  t_form_token.type = 'hidden';
  t_form_token.name = 'form_token';
  t_form_token.id   = 'field_form_token';
  t_form_token.value = drupal_form_token;
  t_form.appendChild(t_form_token);

  // Create 'Form ID' field and append it to the form
  var t_form_id = document.createElement('input');
  t_form_id.type = 'hidden';
  t_form_id.name = 'form_id';
  t_form_id.id   = 'field_form_id';
  t_form_id.value = 'quickcomments_form';
  t_form.appendChild(t_form_id);

  // Create submit button and append it to the form
  var t_submit = document.createElement('input');
  t_submit.type = 'submit';
  t_submit.name = 'op';
  t_submit.id   = 'form_submit';
  t_submit.className = 'form-submit';
  t_submit.value = 'Добавить комментарий';
  t_form.appendChild(t_submit);
  t_form.appendChild(document.createTextNode(' '));

  // Create cancel button and append it to the form
  var t_cancel = document.createElement('input');
  t_cancel.type = 'button';
  t_cancel.value = 'Отказаться';
  t_cancel.onclick = function() {
    clearForm();
  }
  t_form.appendChild(t_cancel);

  // Append form to the page
  t_div.appendChild(t_form);
  t_node.appendChild(t_div);

  t_area.focus();

  // Request form token from server with AJAX
  $.ajax({
    type: 'GET',
    url: '/get_token/quickcomments_form_' + t_node_id + '/',
    success: function(t) {
      drupal_form_token = t;
      if (t_token = document.getElementById('field_form_token')) {
        t_token.value = drupal_form_token;
      }
    }
  });

  return false;
}


// Process form submit
// @TODO: Check presence of valid form_token before form submiting
function submitQuick(t_form) {
  var t_data = new Array();

  // Get values
  for (var i = 0; i < form_fields.length; i++) {
    t_field = form_fields[i];
    if (t_input = t_form[t_field]) {
      t_data[t_data.length] = t_field + '=' + t_input.value;
    }
  }

  // Send user data
  if (t_data.length > 0) {
    var t_post_data = t_data.join('&');
    $.ajax({
      type: 'POST',
      cache: false,
      url: '/ajax/quickcomments/',
      data: t_post_data,
      dataType: 'xml',
      success: processResponse
    });
    t_div = t_form.parentNode;
    t_div.innerHTML = 'Добавляем комментарий...';
  }

  // Preserve browser submit
  return false;
}


// Remove quick comments form from the page
function clearForm() {
  $('div.node-form').each(function(j) {
    if ((typeof this['sending'] == 'undefined') || !this.sending) {
      this.parentNode.removeChild(this);
    }
  });
  drupal_form_token = '';
}


// Remove all children
function clearDiv(t_obj) {
  while (t_obj.childNodes.length > 0) {
    t_obj.removeChild(t_obj.firstChild);
  }
}


// Get content for XML-tag
function getXMLData(t_xml, t_tag) {
  if (t_tags = t_xml.getElementsByTagName(t_tag)) {
    return t_tags[0].firstChild ? t_tags[0].firstChild.data : null;
  }
  return null;
}


// Process server response and render
function processResponse(t_xml) {

  var t_nid     = getXMLData(t_xml, 'nid');
  var t_status  = getXMLData(t_xml, 'status');
  var t_message = getXMLData(t_xml, 'message');
  var t_comment = getXMLData(t_xml, 'comment');

  // Invalid response
  if (!t_nid || !t_status || !t_message) {
    return;
  }

  var t_div_id = 'quickcomments_' + t_nid;

  if (t_div = document.getElementById(t_div_id)) {
    // 'Nice' indication
    clearDiv(t_div);

    // Indicator block
    var t_indicator = document.createElement('div');
    t_indicator.id = 'quickcomments_indicator';
    t_indicator.className = 'quickcomments_' + t_status;
    t_indicator.innerHTML = t_message;
    t_div.appendChild(t_indicator);

    // Change link text (comments counter)
    if (t_comment) {
      $('a.comment_add, a.comment_comments').each(function(j) {
        var t_node_id = 'node-' + t_nid;
        if ((typeof this['node_id'] != 'undefined') && (this.node_id == t_node_id)) {
          this.innerHTML = t_comment;
        }
      });
    }

    // Animation...
    setTimeout(function() {
      if (t_div = document.getElementById(t_div_id)) {
        $(t_div).fadeOut('slow', function() {
          this.parentNode.removeChild(this);
        });
      }
    }, 5000);

  } else {
    // Default indication
    alert(t_status + ': ' + t_message);
  }
}


// Check User ID and set quick comments processor
function setClicker(msg) {
  if (!msg || (parseInt(msg) == 0)) {
    return;
  }

  $('a.comment_add, a.comment_comments').each(function(j) {
    if (t_id = getNodeId(this)) {
      this['node_id'] = t_id;
      $(this).click(function() {
        return quickCommentsForm(this);
      });
    }
  });

}


// Initialize script...
$(function() {

  $.ajax({
    type: 'GET',
    cache: false,
    url: '/ajax/checkuser/',
    success: setClicker
  });

});


//-->
