$(document).ready(function() {
	
	
	$('#blogComments form').each(function() {
		initBlogButtons( $(this) );
	});

});

function initBlogButtons(form) {

	var commentID = form.attr('id').substring(8);
	var targetURL = '/index.php?component=blog';
	
	form.find('.buttonDelete').click( function() {
	
		if (confirm("Are you sure you wish to delete this comment?\n\nThis cannot be undone")) {
		
			var deleteUrl = targetURL + '&action=commentDelete&commentID=' + commentID;
			commentHeight = ( document.getElementById( form.attr('id') ).offsetHeight + 'px' );
			form.html('&nbsp;');
			form.css('height', commentHeight);
			form.css('line-height', commentHeight);
			form.css('text-align', 'center');
			
			form.load(deleteUrl, function() {
				form.slideUp('slow', function() {
					form.remove();
				})
			});
		}

	});


	form.find('.buttonCancel, .buttonAccept').click( function() {
			target = $(this);
			var statusUrl = targetURL + '&action=commentStatus&commentID=' + commentID;
			target.load(statusUrl, function() {
				if (target.html() == 'Hide this Comment') target.attr('class', 'buttonCancel');
				else  target.attr('class', 'buttonAccept');
			
			});

	});


	form.find('.buttonEdit').click( function() {
	
			form.find('.buttonEdit').unbind('click');
			// store the current contents
			var cancelContents = form.parent().html();
	
			var formID = form.attr('id');
			var editUrl = targetURL + '&action=commentEdit&commentID=' + commentID;
			form.parent().load(editUrl, function() {
				initialiseForm('#' + formID);
				$('#' + formID).find('.cancelReturn').unbind('click');
				$('#' + formID).find('.cancelReturn').click( function() {
					$('#' + formID).parent().html(cancelContents);
					initBlogButtons ( $('#' + formID).parent().children() );
				});
			});
			
	});
}
