function commentOnResource(resourceid, comment) {
	rpc.invoke('addResourceComment', {
		resource_id: resourceid,
		comment: comment
	}, function(response) {
		if(response.canComment == false) {
			alert('You have been logged out');
			//option to do this nicer
		} else if(response.success == false) {
			alert('There has been an error saving');
		} else {
			//SUCCESS!
			//response.comment contains the comment for injecting into the page.
			var newComment=document.createElement('div');
			newComment.className="comment";
			newComment.id="comment_" + response.id;
			newComment.innerHTML='<div class="head"></div>' +
				'<div class="content clearfix">' +
				'<img src="'+response.avatar+'" alt="your profile picture" height="70" width="70" />' +
				'<div class="info">Comment by you just now</div>' +
				'<p>' + response.comment + '</p><br style="clear:both;" />' +
				'</div>' +
				'<div class="options"><ul class="arrow"><li><a href="#" onclick="return deleteMyComment(\'resource\','+response.id+')">Delete this comment</a></li></ul></div>';
			$(newComment).inject($('addComment2').parentNode,'after');
			location.hash=newComment.id;
			
			$$('.add-comment','.no').setStyle('display','none');
			var ccspan=$$('.commentCount')[0];
			var ccount=parseInt(ccspan.innerHTML);
			ccspan.innerHTML=(ccount+1);
		}
		
		$$('.submitting').setStyle('display','none');
		if($$('.comments .btnAddComment')[0])
			$$('.comments .btnAddComment')[0].style.display='block';
		if($('yourcomment'))$('yourcomment').value='';
		if($('yourcomment2'))$('yourcomment2').value='';
	});
}

function commentOnCollection(collectionid, comment) {
	rpc.invoke('addCollectionComment', {
		collection_id: collectionid,
		comment: comment
	}, function(response) {
		if(response.canComment == false) {
			alert('You have been logged out');
			//option to do this nicer
		} else if(response.success == false) {
			alert('There has been an error saving');
		} else {
			//SUCCESS!
			//response.comment contains the comment for injecting into the page.
			var newComment=document.createElement('div');
			newComment.className="comment";
			newComment.id="comment_" + response.id;
			newComment.innerHTML='<div class="head"></div>' +
				'<div class="content clearfix">' +
				'<img src="'+response.avatar+'" alt="your profile picture" height="70" width="70" />' +
				'<div class="info">Comment by you just now</div>' +
				'<p>' + response.comment + '</p>' +
				'</div>' +
				'<div class="options"><ul class="arrow"><li><a href="#" onclick="return deleteMyComment(\'collection\','+response.id+')">Delete this comment</a></li></ul></div>';
			$(newComment).inject($('addComment2').parentNode,'after');
			//location.hash=newComment.id;
			
			$$(".add-comment",'.no').setStyle('display','none');
			if($$('.comments .btnAddComment')[0])
				$$('.comments .btnAddComment')[0].style.display='block';
			if($('yourcomment'))$('yourcomment').value='';
			if($('yourcomment2'))$('yourcomment2').value='';
		}
		
		$$('.submitting').setStyle('display','none');
		var ccspan=$$('.commentCount')[0];
		var ccount=parseInt(ccspan.innerHTML);
		ccspan.innerHTML=(ccount+1);
	});
}

function deleteMyComment(type, id) {
	if(confirm('Are you sure you want to delete this comment?')) {
		rpc.invoke('deleteMyComment', {
			type: type,
			id: id
		}, function(response) {
			if(response) {
				$('comment_'+id).dispose();
				var ccspan=$$('.commentCount')[0];
				var ccount=parseInt(ccspan.innerHTML);
				ccount--;
				ccspan.innerHTML=(ccount);
				
				if(ccount==0){
					if($$('.no')){
						if($$('.no').length<1){
							$$('.comments')[0].innerHTML+="<p class='no'>No comments yet</p>";
						}
						$$('.no').setStyle('display','block');
					}
				}
			} else {
				alert('Your comment could not be deleted.');
			}
		});
	}
	return false;
}

function reportComment(type, id) {
	if(confirm('Are you sure you want to report this comment as inappropriate?')) {
		rpc.invoke('reportComment', {
			type: type,
			id: id
		}, function(response) {
			alert('Thank you for reporting this comment.');
			//if(response) {
				//$('comment_'+id).dispose();
			//} else {
				//alert('Your comment could not be deleted.');
			//}
		});
	}
	return false;
}
