var limit = 250;
var cnt = 0; 

function updateCnt(){
	var cnt = limit - ($("#commentText").val() != null ? $("#commentText").val().length : 0);	
	$("#charCount").html(cnt);
	if(cnt < 0 || cnt == limit){
		$("#charCount").css("color", "#FF0000");
		$("#commentSubmit").attr("disabled", "disabled");
	}else{
		$("#charCount").css("color", "#000000");
		$("#commentSubmit").removeAttr("disabled");
	}
}

function editComment(ele){	
	$("#commentText").val($("#comment_text_" + $(ele).attr("id").substring(5) + " p").text());
	$("#commentAction").val("edit");
	$("#commentForm").append("<input type=\"hidden\" name=\"cid\" id=\"cid\" value=\"" + $(ele).attr("id").substring(5)  + "\" />");
	$("#cancel_edit_comment").css("display", "inline");
	$("#commentSubmit").val("Edit Comment");
	$("#commentText").get(0).focus();
}

function cancelEdit(){
	$("#commentText").val("");
	$("#commentAction").val("post");
	$("#commentForm").remove("#cid");
	$("#cancel_edit_comment").css("display", "none");
	$("#commentSubmit").val("Post Comment");
}

$(document).ready(function(){	
	$("#commentText").keyup(function(e){updateCnt(e);});		
	$("#commentSubmit").click(function(e){if($.trim($("#commentText").val()).length == 0) e.preventDefault();});
	updateCnt();
	
	var url = window.location.href;
	var index = url.indexOf("?cs="); 
	if(index != -1 && url.indexOf("#") == -1){
		var commentId = window.location.href.substring(index + 4);
		$.scrollTo($("#comment-" + commentId), 800, {offset: 100});
	}
});
