var comments_current_node = 0;



function ShowCommentsForm (id)
{
    newobj = document.getElementById ("comments_answer_"+id);
    oldobj = document.getElementById ("comments_answer_"+comments_current_node);
    if (oldobj == null)   {
        return;
    };
    if (id == comments_current_node || newobj == null) {
        if (oldobj.style.display == 'none')
        oldobj.style.display = 'block';
        else
        oldobj.style.display = 'none';
        return;
    };
    form = oldobj.innerHTML;
    newobj.innerHTML = form;
    oldobj.innerHTML = "";
    newobj.style.display = 'block';
    comments_current_node = id;
    document.getElementById ("parent_id").value = id;
    return;
}

function EditCommentData(id , url)
{
  var form = document.getElementById ("comments_form_"+id);
  var li   = document.getElementById ("comments_textdata_"+id);
  var valid_form   = document.getElementById ("name_form_"+id);
  var obj = document.getElementById ("comments_edit_"+id);

  if (validateForm( valid_form , 0, 0, 0, 1, 8)){
    var post_obj = {
        'comment_id' : id,
        'comment_text'  : form.value,
        'ajax'  : 1
    };
    post_obj.url = url;
    
    post_obj.onSuccess = function(req){
        try {
            li.innerHTML = '';
            debugResult('Пришел ответ от сервера, задержка '+ $time(reqTime) + 'мс.');
            PuskFramework.srvapi.parseResponse(req, url);
            if (obj.style.display == 'none')
             obj.style.display = 'block';
            else
             obj.style.display = 'none';
        } catch(err) {
            debugError('Не получается выполнить коллбэк!');
            serverSetStatus('error');
        };
    };
    post_obj.onError =  function(req) {
        debugError('Ошибка запроса!\nСтатус=' + req.statusText);
        serverSetStatus('error');
    };

    var reqTime = $time();
    PuskFramework.ajax.post(post_obj);
  }
}

function SubmitComment(div_id, url){
   var msid = document.getElementById("msid");
   var data_action = document.getElementById("data_action");
   var data_comment_id = document.getElementById("data_comment_id");
   var data_text = document.getElementById("data_text");
   var div = document.getElementById(div_id);
   
   val_msid = (msid == null?'':msid.value);
   val_data_action = (data_action == null?'':data_action.value);
   val_data_comment_id = (data_comment_id == null?'':data_comment_id.value);
   val_data_text = (data_text == null?'':data_text.value);
   
   PuskFramework.ajax.post({
    'url': url,
    'msid' : val_msid,
    'data[action]' : val_data_action,
    'data[comment_id]' : val_data_comment_id,
    'data[text]'  : val_data_text,
    'onSuccess': function(req){
        div.innerHTML = req.responseText;
    },
    'onError': function(req) {
      debugError('Ошибка запроса!\nСтат. =' + req.statusText);
      serverSetStatus('error');
      if (onerror) onError(req);
    }
  });

}

function ShowEditForm (id , el_this)
{

//    var text = document.getElementById ("comments_textdata_"+id);
//    var form = document.getElementById ("comments_form_"+id);
    var form = document.getElementById ("comments_textdata_"+id);
    var obj = document.getElementById ("comments_edit_"+id);
    if (obj == null)
    {
        return;
    }

   PuskFramework.ajax.get({
    'url': el_this.href,
    'onSuccess': function (req){
        form.innerHTML = req.responseText;
    },
    'onError': function(req) {
      debugError('Ошибка запроса!\nСтат. =' + req.statusText);
      serverSetStatus('error');
      if (onerror) onError(req);
    }
  });

    if (obj.style.display == 'none')
     obj.style.display = 'block';
    else
     obj.style.display = 'none';
    return;
}

function ShowComment (id)
{
    if (document.getElementById('comment_'+id).style.display == 'none') {
        document.getElementById('comment_'+id).style.display='block';
        document.getElementById('comment2_'+id).style.display='block';
    } else {
        document.getElementById('comment_'+id).style.display='none';
        document.getElementById('comment2_'+id).style.display='none';
    }
}

function CommentsStatusRating(id)
{

  var _string = '';
  var _debug = '';
  PuskFramework.ajax.get({
    'url': id.href,
    'onSuccess': function (req){
        id.parentNode.innerHTML = req.responseText;
    },
    'onError': function(req) {
      debugError('Ошибка запроса!\nСтат. =' + req.statusText);
      serverSetStatus('error');
      if (onerror) onError(req);
    }
  });

  return false;
}

var StarsCurrent = 0;
var StarsCurrentLast = 0;
var StarsNameArray = ['', 'лучше и не вспоминать', 'очень плохо', 'могло бы быть и лучше', 'мне понравилось', 'превзошло все ожидания'];
var StarsNameElem  = null;

function CommentsCompaniesMove(id, html, el){
    var el_class = el.parentNode;
    StarsCurrent = id;
    el_class.className = el_class.className.substr(0,el_class.className.length-1) + id;
    if (!StarsNameElem) StarsNameElem = document.getElementById('starsname');
    StarsNameElem.innerHTML = StarsNameArray[StarsCurrent];
    return false;
}

function CommentsCompaniesClick(id, html, el){
    StarsCurrent = id;
    StarsCurrentLast = id;
    var rating   = document.getElementById('rating_hidden_input');
    rating.value = StarsCurrentLast;
    if (!StarsNameElem) StarsNameElem = document.getElementById('starsname');
    StarsNameElem.innerHTML = StarsNameArray[StarsCurrentLast];
}

function CommentsCompaniesOut() {
    if (StarsCurrentLast != StarsCurrent) {
        var el_class = document.getElementById('comments_rating_stars');
        el_class.className = el_class.className.substr(0,el_class.className.length-1) + StarsCurrentLast;
        if (!StarsNameElem) StarsNameElem = document.getElementById('starsname');
        StarsNameElem.innerHTML = StarsNameArray[StarsCurrentLast];
    }
}

var comments_current_node = 0;
function ShowBlogCommentsForm(id, position_on_obj){
    newobj = document.getElementById("comments_answer");
    if(newobj == null){
        return;
    };
    if(comments_current_node == id || comments_current_node == 0){
        if(newobj.style.display == 'none'){
            newobj.style.display = 'block';
        } else {
            newobj.style.display = 'none';
            comments_current_node = 0;
            return;
        };
    };
    comments_current_node = id;
    document.getElementById("answer_parent_id").value = id;
    newobj.style.top = (position_on_obj.offsetTop+10) + 'px';
    newobj.style.left = (position_on_obj.offsetLeft-320) + 'px';
    return;
}