video_comment=
{
    commentLoading: false,
    commentPosting: false,
    commentPage: 0,
    firstTime: true,
	insertSymbol: function(symbol)
	{
		var obj=$('#comment_content').get(0);
		if (document.selection)
		{
			obj.focus();
			sel = document.selection.createRange();
			sel.text = symbol;
		}
		else if (obj.selectionStart || obj.selectionStart == 0)
		{
			var startPos = obj.selectionStart;
			var endPos = obj.selectionEnd;
			obj.value = obj.value.substring(0, startPos)
						+ symbol
						+ obj.value.substring(endPos, obj.value.length);
		} 
		else
		{
			obj.value += symbol;
		}
	},
	loadComment:  function(vid, p)
 	{
 	    if (this.commentLoading)
 	    {
 	        return false;
 	    }

 	    $.ajax({
			   type: "GET",
			   url: "/comment/view/" + vid + "?page=" + p,
			   beforeSend: function(req){
                   $("#cmt_loader").show();
				   video_comment.commentLoading=true;
			   },
		       complete: function(res, s){
		       	   
			       $("#comment_area").html(res.responseText);
				   $("#cmt_loader").hide();
				   
				   video_comment.commentLoading=false;
				   video_comment.commentPage=p;
				   if (!video_comment.firstTime)
				   {
                        location.href='#comment_section';
				   }
				   else
				   {
				        video_comment.firstTime=false;
				   }
				   
			    }	   
	        });
 		return false;
 	},
 	deleteComment: function(cid)
 	{
 		if (!confirm("Bạn chắc chắn xóa bình luận này"))
 			return false;
 		$.ajax({
			   type: "POST",
			   url: "/comment/delete/" + cid + "?page=" + this.commentPage,
			   error: function(req, status, info){
			   	   alert("Có lỗi khi xóa bình luận. Hãy thử lại lần khác");
			   },
			   success: function(comment, status){
			   	   $("#comment_area").html(comment);
			   	   location.href='#comment_section';
			   }
		});
		return false;
 	},
 	saveComment: function(vid, cid)
 	{
 		var saveCommentUrl = "/comment/save/" + vid;
 		if (cid == '') {
            textAreaID = "#comment_content";
        } else {            
            textAreaID = "#comment_content_" + cid;
            saveCommentUrl = saveCommentUrl+"/"+cid;
        }
        
 	    if ($(textAreaID).val()=="")
			return;
		if (video_comment.commentPosting)
			return;

		$.ajax({
			   type: "POST",
			   url: saveCommentUrl,
			   data: $(textAreaID).serialize(),
			   beforeSend: function(req){
                   $("#cmt_loader").show();
				   video_comment.commentPosting=true;
			   },
			   error: function(req, status, info){
			   	   alert("Có lỗi khi cập nhật bình luận của bạn");
			   },
			   success: function(comment, status){
			   	   $("#comment_area").html(comment);
			   	   $(textAreaID).val("");
			   },
			   complete: function(res, s){
				   $("#cmt_loader").hide();
				   video_comment.commentPosting=false;
				   video_comment.commentPage=0;
				   location.href='#comment_section';
			    }
		});
 	},
 	showReplyBox: function(cid)
    {
        $("#reply_hint_" + cid).hide();
        $("#reply_box_" + cid).show();
        return false;
    },
    showReply: function(cid)
    {
        $('.reply_comment_' + cid).show();
        $('#display_reply_' + cid).hide();
        return false;
    }
}