

	page = 2; // начальная страница подгружаемых комментов

$(document).ready(function(){ 
	 
  	$("#comment").keyup(function(){ limitChars("comment", 1000, "commentLimit");}) 
    $("#comment").blur(function(){ $("#commentLimit").empty();})
    
    var options = {  
  		dataType:"json",
  		timeout: 3000, // тайм-аут
  		beforeSubmit: showRequest,
  		success: show_answer
  	};
  	 
  var optionsSpam = {  
  	dataType:"json",
    timeout: 3000, // тайм-аут
    beforeSubmit: showRequestSpam,
    success: show_answerSpam
  };
  
  $("#formComment").validate({
	  submitHandler: function(form) { 
		  $(form).ajaxSubmit(options);
		  return false;
	  },
	  focusInvalid: false,
	  focusCleanup: true,
	  rules: {
		  "comment[name]": {
		  required: true,
		  minlength: 3,
		  maxlength: 25
	  } ,
	  "comment[email]": {
		  required: true,
		  email: true
	  },
	  "comment[comment]": {
		  required: true,
		  minlength: 10,
		  maxlength: 1000
	  } 
	  },
	  messages: {
		  "comment[name]": {
		  required: "Укажите свое имя, пожалуйста!",
		  minlength: "Не менее 3 символов",
		  maxlength: "Не более 25 символов"
	  } ,
	  "comment[email]": {
		  required: "Нужно указать email адрес",
		  email: "Email адрес должен быть корректным"
	  },
	  "comment[comment]": {
		  required: "Напишите сообщение, пожалуйста!",
		  minlength: "Не менее 10 символов",
		  maxlength: "Не более 1000 символов"
	  } 
	  },
	  errorPlacement: function(error, element) {
		  var er = element.attr("id");
		  error.appendTo( element.parent().find("label[for='" + er + "']") ).css("color", "red");
	  }
  });
// ---- Форма -----
  
  $("#formCommentSpam").validate({
	  	submitHandler: function(form) { 
	  	  $(form).ajaxSubmit(optionsSpam);
	  	  return false;
	    },
	    focusInvalid: false,
	    focusCleanup: true,
	    rules: {
			  "comment[name]": {
				required: true,
				minlength: 3,
				maxlength: 25
			  } ,
			  "comment[email]": {
				required: true,
				email: true
			},
			"comment[comment]": {
				required: true,
				minlength: 10,
				maxlength: 1000
			} 
	    },
	    messages: {
	      "comment[name]": {
	        required: "Укажите свое имя, пожалуйста!",
	        minlength: "Не менее 3 символов",
	        maxlength: "Не более 25 символов"
	      } ,
	    "comment[email]": {
	        required: "Нужно указать email адрес",
	        email: "Email адрес должен быть корректным"
	      },
	      "comment[comment]": {
	      	required: "Напишите сообщение, пожалуйста!",
	      	minlength: "Не менее 10 символов",
	        maxlength: "Не более 1000 символов"
	      } 
	    },
	   errorPlacement: function(error, element) {
	   var er = element.attr("id");
	    error.appendTo( element.parent().find("label[for='" + er + "']") ).css("color", "red");
	    }
  });
// ---- Форма -----
});


function showRequest(formData, jqForm, options) {  
	return true; 
} 

function show_answer(responseText, statusText)  {  

	if(responseText.error==0)
	{ 
		$('#formComment').animate({height: "hide"}, 1000);	 
    	$('#infoSpam').empty(); 
		$('#info').text(responseText.txt).animate({height: "show"}, 1000).css("color", "green");	   
	}
	
	if(responseText.error==1){ 
		$('#info').text(responseText.txt).animate({height: "show"}, 1000).css("color", "red");		 
	}
} 


function showRequestSpam(formData, jqForm, options) {  
    return true; 
} 
  
function show_answerSpam(responseText, statusText)  {  
	 
	
	if(responseText.error==0){ 
    	$('#block_spam_form').animate({height: "hide"}, 1000);	  
    	$('#block_comment_form').animate({height: "show"}, 1000);	  
    	$('#c_nameS').val('');
    	$('#emailS').val('');
    	$('#commentSpam').val('');
    	$('#info').empty();
		$('#infoSpam').text(responseText.txt).animate({height: "show"}, 1000).css("color", "green");	  
	}
	
	if(responseText.error==1){ 
		$('#infoSpam').text(responseText.txt).animate({height: "show"}, 1000).css("color", "red");		 
	}
	
	 
} 
 
function goSpamForm(id){
	$('#block_comment_form').animate({height: "hide"}, 1000);	  
	$('#block_spam_form').animate({height: "show"}, 1000);	  
	$('#comment_id').val(id);
	
}

function moreComment(id,all){

	  $("#loading_comments").show();   
	  $.post("/news/getComment", {page:page,id:id},
			  function(data){
 			  	$("#block_comments").append(data.div).animate({height: "show"}, 1000);
			    $("#loading_comments").hide();     	
			    page++;					    

			    var rest = parseInt($("#comments_count").text())-3;     
			    $("#comments_count").text(rest);     	

			    	if(page>all){
			    		$('.more-comment').hide();
			    	}
			  }, "json"); 		     	
}

function limitChars(textid, limit, infodiv)
{
	var text = $('#'+textid).val();	
	var textlength = text.length;
	if(textlength > limit)
	{
		$('#' + infodiv).html('Вам нельзя написать более чем '+limit+' символов!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		$('#' + infodiv).html( (limit - textlength) +' осталось.');
		return true;
	}
}
