var comments_count = 0;

function send_comment(id, text){
	
	if(!id)
		return;
	
	$("#err_div").text('');
	$('#send_button').attr('disabled',"disabled");
	
	if(text == ""){
		$("#err_div").append(lang_comm_empty + "<br/>");
		$('#send_button').attr('disabled',"");
		return;
	}
	
	if( text.length>1000 ){
		$("#err_div").append(lang_comm_size + "<br/>");
		$('#send_button').attr('disabled',"");
		return;
	}
	
	$("#err_div").html('<img src="/img/loading.gif">');
	
	$.ajax({
		type: "POST",
		url: "/index.php?fuseaction=files.comment",
		dataType: 'json',
		data: {
			'file_id'				 : id,
			'comment_text' :text
		},
		success: function (msg){
			var data = msg;
			if (msg.message == "success"){
				var new_comment_html = '<div id="cmnt_' + msg.comment_id + '" class="item">'
													   + 	'<div class="header"> '
													   + '<div class="commdel" onclick="drop_comment(' +  msg.comment_id +  ')">x</div>'
													   + msg.date + ' ' + msg.username + ':</div>'
													   +	'<div class="text">' + msg.text + '</div>'
													   + '</div>';
				if(comments_count > 0){
					$("#canvas").append( new_comment_html );
				} else {
					$("#canvas").html( new_comment_html );
				}
				
				
				$("#err_div").text("");
				$("#cmnt_"+ msg.comment_id).highlightFade({color:'rgb(255,255,0)', interval: 2, iterator:'sinusoidal'});
				$("#file_comment_text").val("");
				comments_count = comments_count*1 + 1;
				$('#send_button').attr('disabled',"");
				
			} else if (msg.message == "error") {
				$("#err_div").text("");
				$("#err_div").append(msg.text + "<br/>");
			} else {
				$("#err_div").text("");
				$("#err_div").append(lang_fail + "<br/>");
			}
		},
		error: function (){
			$('#send_button').attr('disabled',"");
			$("#err_div").text("");
			$("#err_div").append(lang_fail + "<br/>");
		}
	});
}

function drop_comment(id){
	
	if(!confirm(lang_sure_drop))
		return false;
	$.ajax({
		type: "POST",
		url: "/index.php?fuseaction=files.comment_drop",
		dataType: 'json',
		data: {
			'comment_id'				 : id
		},
		success: function (msg){
			var data = msg;
			if (msg.message == "success"){
				$('#cmnt_' + id).slideUp(500);
			}
			comments_count = comments_count*1 + 1;
		
		},
		error: function (){
			alert(lang_fail );
		}
	});
}
