function deletePost(id){

	var isBlog = $('#isBlog-' + id).val();
	var belongsToGroup = $('#belongsToGroup-' + id).val();
	
	if(isBlog != 0){
		if (confirm('Are you sure you want to delete this post?')){
			$.get('/blog/ajaxDeleteMiniBlogEntry/' + id,
				{},
				function(data,textState) {
					$('#entryAndToolContainer-' + id).fadeOut('fast').remove();
					showHideNoActivity();
				}
			);
		}
	}else if(belongsToGroup != 0){
		if (confirm('Are you sure you want to delete this post?')){
			$.get('/groups/ajaxDeleteMiniBlogEntry/' + id,
				{},
				function(data,textState) {
					$('#entryAndToolContainer-' + id).fadeOut('fast').remove();
					showHideNoActivity();
				}
			);
		}
	}else{
		if (confirm('Are you sure you want to delete this post?')){
			$.get('/me/ajaxDeleteMiniBlogEntry/' + id,
				{},
				function(data,textState) {
					$('#entryAndToolContainer-' + id).fadeOut('fast').remove();
					showHideNoActivity();
				}
			);
		}
	}
}

function postComment(id){

	var isBlog = $('#isBlog-' + id).val();
	var belongsToGroup = $('#belongsToGroup-' + id).val();

	var val= $('#miniBlogEntryCommentBody-' + id).val();
	
	if (val != '') {
	
		if(isBlog != 0){
			$.post('/blog/ajaxPostMiniBlogEntryComment',
				{ "postId" : id,
				  "body"   : val
				},
				function(data,textState) {
				
					$('#commentBin-' + id).append(data);
					$('#miniBlogEntryCommentBody-'+id).val('');
					//$('div.miniBlogEntryLeaveComment.comment-' + id).hide();
					
					//update links to show/hide comments and comment counts:
					var commentCount = $('#commentBin-'+id).children('.miniBlogEntryComment:visible').length;
					
					//hide dialog
					$('#commentDialog-' + id).hide();
					$('#leaveComment-' + id).fadeIn('fast');
	
					//update comment tallies
					updateCommentCountAndLinks(commentCount, $('#commentTally-'+id));
					
				}
			);
		}else if(belongsToGroup != 0){
			$.post('/groups/ajaxPostMiniBlogEntryComment',
				{ "postId" : id,
				  "body"   : val
				},
				function(data,textState) {
				
					$('#commentBin-' + id).append(data);
					$('#miniBlogEntryCommentBody-'+id).val('');
					//$('div.miniBlogEntryLeaveComment.comment-' + id).hide();
					
					//update links to show/hide comments and comment counts:
					var commentCount = $('#commentBin-'+id).children('.miniBlogEntryComment:visible').length;
					
					//hide dialog
					$('#commentDialog-' + id).hide();
					$('#leaveComment-' + id).fadeIn('fast');
	
					//update comment tallies
					updateCommentCountAndLinks(commentCount, $('#commentTally-'+id));
					
				}
			);
		}else{
			$.post('/me/ajaxPostMiniBlogEntryComment',
				{ "postId" : id,
				  "body"   : val
				},
				function(data,textState) {
				
					$('#commentBin-' + id).append(data);
					$('#miniBlogEntryCommentBody-'+id).val('');
					//$('div.miniBlogEntryLeaveComment.comment-' + id).hide();
					
					//update links to show/hide comments and comment counts:
					var commentCount = $('#commentBin-'+id).children('.miniBlogEntryComment:visible').length;
					
					//hide dialog
					$('#commentDialog-' + id).hide();
					$('#leaveComment-' + id).fadeIn('fast');
	
					//update comment tallies
					updateCommentCountAndLinks(commentCount, $('#commentTally-'+id));
				}
			);
		}
	} else {
	
		alert('Type something first...');
		
	}
}

function deleteComment(id){
	
	var isBlog = $('#isBlog-' + id).val();
	var belongsToGroup = $('#belongsToGroup-' + id).val();
	
	if(isBlog != 0){
		if (confirm('Are you sure you want to delete this comment?')) {
			$.get('/blog/ajaxDeleteMiniBlogEntryComment/'+id,
				{},
				function(data,textState) {
					if (data == 'success') {
						var $commentBin = $('#commentDeleteLink-' + id).parent().parent().parent().parent();
						$('#commentDeleteLink-' + id).parent().parent().parent().hide();
						updateCommentCountAndLinks(
							$commentBin.children('.miniBlogEntryComment:visible').length,
							$commentBin.parent().children('.commentTally')
						);
					}
				}
			);
		}
	}else if(belongsToGroup != 0){
		if (confirm('Are you sure you want to delete this comment?')) {
			$.get('/groups/ajaxDeleteMiniBlogEntryComment/'+id,
				{},
				function(data,textState) {
					if (data == 'success') {
						var $commentBin = $('#commentDeleteLink-' + id).parent().parent().parent().parent();
						$('#commentDeleteLink-' + id).parent().parent().parent().hide();
						updateCommentCountAndLinks(
							$commentBin.children('.miniBlogEntryComment:visible').length,
							$commentBin.parent().children('.commentTally')
						);
					}
				}
			);
		}
	}else{
		if (confirm('Are you sure you want to delete this comment?')) {
			$.get('/me/ajaxDeleteMiniBlogEntryComment/'+id,
				{},
				function(data,textState) {
					if (data == 'success') {
						var $commentBin = $('#commentDeleteLink-' + id).parent().parent().parent().parent();
						$('#commentDeleteLink-' + id).parent().parent().parent().hide();
						updateCommentCountAndLinks(
							$commentBin.children('.miniBlogEntryComment:visible').length,
							$commentBin.parent().children('.commentTally'),
							id
						);
					}
				}
			);
		}
	}
}

function updateCommentCountAndLinks(commentCount, $commentTallyDiv, id){

	$commentTallyDiv.children('span.numberOfComments').html(commentCount);
	
	if(commentCount == 0){
		$commentTallyDiv.children('span.showHide').hide();
		$commentTallyDiv.children('span.commentOrComments').html('Comments');
	}else if(commentCount == 1){
		$commentTallyDiv.children('span.showHide').show();
		$commentTallyDiv.children('span.commentOrComments').html('Comment');
	}else{
		$commentTallyDiv.children('span.showHide').show();
		$commentTallyDiv.children('span.commentOrComments').html('Comments');
	}
	
}


function showCommentDialog(ID){

	//close other comment dialogs
	$('.miniBlogEntryLeaveComment').fadeOut('fast');
	
	//show previous comments if not already shown
	showComments(ID);
	
	//hide 'leave a comment' link
	$('#leaveComment-' + ID).hide();
	
	//show comment dialog
	$('#commentDialog-' + ID).fadeIn('fast');
	
	//focus in text area
	$('#miniBlogEntryCommentBody-' + ID).focus();

} 

function cancelComment(ID){
	
	//clear text area
	$('#miniBlogEntryCommentBody-' + ID).val('');
	
	//hide comment dialog
	$('#commentDialog-' + ID).hide();
	
	//show second 'leave a comment' link if it exists
	$('#leaveComment-' + ID).fadeIn('fast');

}

function hideComments(ID){
	$('#commentBin-'+ID).fadeOut('fast');
	$('#commentDialog-'+ID).fadeOut('fast');
	$('#showHideComments-'+ID).attr('href', 'javascript:showComments('+ID+');');
	$('#showHideComments-'+ID).attr('title', 'Click to Show Comments');
	$('#showHideComments-'+ID).html('show');
}

function showComments(ID){
	$('#commentBin-'+ID).fadeIn('fast');
	$('#showHideComments-'+ID).attr('href', 'javascript:hideComments('+ID+');');
	$('#showHideComments-'+ID).attr('title', 'Click to Hide Comments');
	$('#showHideComments-'+ID).html('hide');
}

function showHideNoActivity(){
	if($('#miniBlogEntriesContainer').children('.entryAndToolContainer:visible').size()>0){
		$('#miniBlogEntriesContainer').show();
		$('#noActivity').hide();
	}else if($('#miniBlogEntriesContainer').children('.entryAndToolContainer:visible').size() == 0 && $('#firstPost').children('.entryAndToolContainer:visible').size() > 0){
		$('#miniBlogEntriesContainer').hide();
		$('#noActivity').hide();
	}else{
		$('#miniBlogEntriesContainer').hide();
		$('#noActivity').show();		
	}		
}
