var Comments = new Object();

Comments.msg = new Array();

Comments.create = function (id,link)
  {
    Comments.new_id = id;
    Comments.link = link;
    Comments.page = 1;
    Comments.form_id = 0;
    Comments.edit = 0;
    Comments.php = '/comments/';
    Comments.form = jQuery('#comments_form').parent().html();
    jQuery('#comments_form').remove();

    httpRequest.go('POST',Comments.php,'action=is_captcha&link='+Comments.link,Comments,'Comments.set_captcha()');
    Comments.get_body();
  }

Comments.click_pager = function (id)
{
    document.getElementById("comments_top_loading").style.visibility = 'visible';
    document.getElementById("comments_bottom_loading").style.visibility = 'visible';
    Comments.page = id;
    Comments.get_body();
}

Comments.get_body = function ()
  {
    httpRequest.go('POST',Comments.php,'action=get_body&link='+Comments.link+'&new_id='+Comments.new_id+'&page='+Comments.page,Comments,'Comments.load_body()');
  }

Comments.load_body = function (){
	jQuery('#comments_body').html(Comments.responseText);
    if(Comments.responseText != ''){
    	jQuery('#add-coment-bottom').css('display','block');
    }
    Comments.hide_form();

    jQuery('#comments_top_loading').css('visibility','hidden');
    jQuery('#comments_bottom_loading').css('visibility','hidden');
  }

Comments.view_form = function (id){
    Comments.hide_form();
	Comments.form_id = id;
	Comments.edit = 0;

	jQuery('#'+Comments.form_id).html(Comments.form);
	jQuery('#comments_form').fadeIn('fast');
	jQuery('#comments_textarea').focus();
	if(Comments.captcha != "0"){
        jQuery('.captcha-td').css('display','table-cell');
	}
	/*
	if(navigator.appVersion.indexOf("MSIE 6.0") == -1)
	  {
	    document.getElementById(Comments.form_id).innerHTML = Comments.form;
        document.getElementById("comments_textarea").focus();
        if(Comments.captcha == 0) document.getElementById("comments_form_bottom").innerHTML = '<input id="comments_button" type="button" value="отправить" onclick="Comments.submit_form()" />';
	  }
	else  setTimeout("document.getElementById(Comments.form_id).innerHTML = Comments.form; document.getElementById(\"comments_textarea\").focus(); if(Comments.captcha == 0) document.getElementById(\"comments_form_bottom\").innerHTML = '<input id=\"comments_button\" type=\"button\" value=\"отправить\" onclick=\"Comments.submit_form()\" />';",0);
	/**/
  }

Comments.view_form_edit = function (id){
    Comments.view_form(id);
    Comments.edit = 1;
    httpRequest.go('POST',Comments.php,'action=get_text&link='+Comments.link+'&id='+id,Comments,'Comments.add_text()');
}

Comments.add_text = function (){
    document.getElementById('comments_textarea').value = Comments.responseText;
}

Comments.set_captcha = function (){
    Comments.captcha = Comments.responseText;
}

Comments.hide_form = function (){
    if(Comments.form_id != 0){
        jQuery('#'+Comments.form_id).html('');
        Comments.form_id = 0;
    }
}

Comments.submit_form = function ()
  {
    if(document.getElementById('comments_textarea').value == "") return;
    else
      {
        textarea_value = document.getElementById('comments_textarea').value;
        document.getElementById('comments_div_textarea').style.height = '45px';
        document.getElementById('comments_div_textarea').style.paddingTop = '40px';
        document.getElementById('comments_div_textarea').innerHTML = '<img src="/images/loading.gif" />';

        if(Comments.edit == 0)
          params = 'action=add&link='+Comments.link+'&body='+textarea_value+'&parent_id='+Comments.form_id+'&new_id='+Comments.new_id;
        else
          {
            Comments.edit = 0;
            params = 'action=edit&link='+Comments.link+'&body='+textarea_value+'&parent_id='+Comments.form_id;
          }

        if(Comments.captcha == 1)
          {
          	params += '&text_captcha='+document.getElementById('comments_captcha').value;
          }
        httpRequest.go('POST',Comments.php,params,Comments,'Comments.result_submit_form()');
      }
  }

Comments.result_submit_form = function ()
  {
    if(Comments.responseText == 1){
        document.getElementById('comments_div_textarea').innerHTML = Comments.msg[1];
        if((Comments.form_id == 'comments_top' || Comments.form_id == 'comments_bottom') && document.getElementById('total_page')){
          	Comments.page = document.getElementById('total_page').value+1;
        }
    }
	if(Comments.responseText == 2){
        document.getElementById('comments_div_textarea').innerHTML = Comments.msg[2];
    }
	if(Comments.responseText == 3){
        document.getElementById('comments_div_textarea').innerHTML = Comments.msg[3];
 	}

    setTimeout('Comments.get_body()',1200);
  }

Comments.add_smile = function (smile)
  {
    var comment = document.getElementById('comments_textarea');
    if(comment.value == "")
      {
        comment.value = smile;
        return;
      }
    comment.focus();
    if(comment.selectionStart) pos = comment.selectionStart;
    else {
           var sel = document.selection.createRange();
           var clone = sel.duplicate();
           sel.collapse(true);
           clone.moveToElementText(comment);
           clone.setEndPoint('EndToEnd', sel);
           pos = clone.text.length;
    	 }
    part1 = comment.value.substring(0,pos);
    part2 = comment.value.substring(pos,comment.value.length);
    comment.value = part1+smile+part2;
  }

Comments.drop = function (id)
  {
    httpRequest.go('POST',Comments.php,'action=drop&link='+Comments.link+'&id='+id,Comments,'Comments.get_body()');
  }