var commentForm = new requiredForm("submit_button");
commentForm.setFields({
	"name": {"checkType":"non_empty"},
	"email": {"checkType":"email"},
	"message": {"checkType":"non_empty"}
});


/*
	Класс для форм комментирования
*/

function comments(){
	this.currentFormId='commentFormBlock_0';
}

comments.prototype.afterAnswerClick  = function(currentFormId){
		this.currentFormId='commentFormBlock_'+currentFormId;

		$('#name_form').val('');
		$('#email_form').val('');
		$("#message_form").val('');

		$('#parentId_form').val(currentFormId);
		commentForm.checkFields();
		$('#name_form').focus();		
}

comments.prototype.commentAnswer  = function(postNum){
   
		$("div.blogComments, #commentFormBlock_0").css({display:"none"}); 
		$("#commentBottom>span").addClass("active");

		$("div.blog_comment_answer>span").css({display:""});
		$("#commentBlock_"+postNum+">div.blog_comment_answer>span").css({display:"none"}); 
		$('#commentBlock_'+postNum+'>div.blog_comment_answer>div.blog_comment_write').css({display:""}); 

		if(this.currentFormId!='commentFormBlock_'+postNum){
			$('#commentFormBlock_'+postNum).html($('#'+this.currentFormId).html());
			$('#'+this.currentFormId).empty();
		}

		this.afterAnswerClick(postNum);

		var formTopY=$("#commentBlock_"+postNum).offset().top;
		var formBottomY=$("#commentBlock_"+postNum).offset().bottom;
		var formHeight=formBottomY-formTopY;
		var scroll=formBottomY-getClientBottomY();

		if(scroll>0){
			scroll+=15;
			$.scrollTo('+='+scroll, 450)
		}
}

comments.prototype.commentPost  = function(){
	if(this.currentFormId!='commentFormBlock_0'){
		$('#commentFormBlock_0').html($('#'+this.currentFormId).html());
		$('#'+this.currentFormId).empty();
	}
	$("div.blog_comment_answer>span").css({display:""});
	$("#commentBottom>span.active").removeClass("active");
	$("div.blogComments").css({display:"none"}); 
	$("#commentFormBlock_0").css({display:""}); 

	this.afterAnswerClick('0');
}

comments.prototype.activate = function(){
	var self = this;

	$("div.blog_comment_answer>span").live("click", function(){		
		var blockId=$(this).parent().parent().attr("id");
		var post_id=blockId.split('_');
		self.commentAnswer(post_id['1']);
	});

	$("#commentBottom>span").click(function(){
		self.commentPost();
	});


	$("#submit_button").live("click", function(){
		$("#commentForm").ajaxSubmit(function(data) {
			if(self.currentFormId=='commentFormBlock_0'){
				$('ul.blog_comment_area').append(data);
				self.afterAnswerClick(0);
			}else{
				var div_id=$('#'+self.currentFormId).parent().parent().parent().attr('id');
				var level=($('#'+div_id+'>div.blog_comment_content>div.blog_comment_answer_arrow').attr('id')).split('_');

				if (level['1']<10){
					$('#'+self.currentFormId).parent().parent().parent().parent().append("<ul>"+data+"</ul>");
				}else{
					$('#'+self.currentFormId).parent().parent().parent().parent().parent().append(data);
				}

				$("div.blogComments, #commentFormBlock_0").css({display:"none"});
			}
			$("div.blog_comment_answer>span").css({display:""});
		});
	});


	$("#toComments, #commentsHeader>span.pseudo_link").click(function(){
		window.scrollTo(0, $('#commentsHeader').offset().top);
		//$.scrollTo($('#commentsHeader').offset().top, 400);
	});

	$("#commentsHeader>span.pseudo_link").click(function(){
		window.scrollTo(0, $('#commentBottom').offset().top);
		self.commentPost();
	});
}



var comments = new comments();

$(document).ready(function(){

comments.activate();

commentForm.activate();


var currentUrl = new  URL(window.location);
var hash=str_replace('#', '', currentUrl.hash);

if (hash){
	if (hash=='comments'){
		window.scrollTo(0, $('#commentsHeader').offset().top-8);
	}else if(hash=='writeComment'){
		window.scrollTo(0, $('#commentBottom').offset().top-8);
	}else{
		window.scrollTo(0, $('#commentBlock_'+currentUrl.hash).offset().top-8);
	}
}

});