// JavaScript Document

var TEMPLATEPATH = "";

function toggleCommentForm(){
	$("#comments_form").slideToggle();
}

function viewComments(page){
	var postid = $("#post_id").val();
	var author_email = $("#post_author_email").text();
	var pg = page || 1;
	$('#comments').fadeOut('fast', function(){
		$('#comments').load(TEMPLATEPATH+"/includes/ajax/view_comments.php", {'post_id':postid, 'author_email':author_email, 'page':pg}, function(response){
			if(response != ""){
				var count = $("#commentlist li").length;
				var commentsNum = (count > 1) ? count + ' Responses' : count + ' Response';
				var commentsText = $('.comment_count').html().replace(/([0-9])*( Response)(s)?/, commentsNum);
				$('.comment_count').html(commentsText);
				$('.postcomments a').text(count);
				$("#comments").fadeIn();
			}
		});
	});
}

function submitComment(){
	var name = $("#author").val().trim();
	var email = $("#author_email").val().trim();
	var comment = $("#comment").val().trim();
	var require_name_email = $("#commentform").attr("require_name_email") || 0;
	var validated = true;
	
	if(require_name_email == 1 && (name == "" || name == $("#author").attr("defaultText"))) validated = false;
	if(require_name_email == 1 && (email == "" || email == $("#author_email").attr("defaultText"))) validated = false;
	if(comment == "" || comment == $("#comment").attr("defaultText")) validated = false;
	if(!validated){
		if(require_name_email == 1)
			alert("Please fill in all fields before pressing SUBMIT");
		else
			alert("Please enter your comment in the field provided before pressing SUBMIT");
	}else{
		$("#comments_loader").fadeIn();
		var options = {
			target : "#response",
			success : commentSuccess
		};
		$("#commentform").ajaxForm();
		$("#commentform").ajaxSubmit(options);
	}
}

function validateComment(){
	var name = $("#author").val().trim();
	var email = $("#author_email").val().trim();
	var comment = $("#comment").val().trim();
	var require_name_email = $("#commentform").attr("require_name_email") || 0;
	var validated = true;
	
	if(require_name_email == 1 && (name == "" || name == $("#author").attr("defaultText"))) validated = false;
	if(require_name_email == 1 && (email == "" || email == $("#author_email").attr("defaultText"))) validated = false;
	if(comment == "" || comment == $("#comment").attr("defaultText")) validated = false;
	if(!validated){
		if(require_name_email == 1)
			alert("Please fill in all fields before pressing SUBMIT");
		else
			alert("Please enter your comment in the field provided before pressing SUBMIT");
		return false;
	}else{
		return true;
	}
}

function commentSuccess(responseText, statusText){
	if(console != null) console.log(responseText);
	var response = responseText.split("||");
	if(response[1] && intval(response[1]) == 1){
		$("#response").html("<span class='success'>" + response[0] + "</span>");
		viewComments();
	}else{
		$("#response").html("<span class='error'>" + response[0] + "</span>");
		$("#comments_loader").fadeOut();
	}
}

function cancelComment(){
	$(".clearClick").each(function(i,elem){
		$(elem).val($(elem).attr("defaultText"));
	});
	$("#comments_form").slideUp();
}


$(document).ready(function(){
	var options = {
		target : "#response",
		beforeSubmit : validateComment,
		success : commentSuccess
	};
	$("#commentform").ajaxForm(options);
});
