// This is the page controller constructor. This controller handles
// the forum actions.
function Comments( strControllerURL ){
	this.ControllerURL = strControllerURL;
	// Initialize page.
	this.Init();
}

Comments.prototype.objPostProperties = function( objPostProperties ){
	this.PostProperties = objPostProperties;
}

// Extend the page controller class.
Comments.prototype = new PageControllerClass;
// When page loads, hook up events and properties.
Comments.prototype.Init = function(){
	var objSelf = this;
	var commentForm = $( "#commentForm" );
	// Flag as busy.
	this.SetBusy( true );
	// Hook up.
	$('#commentForm').submit(
		function( objEvent ){
			// Submit the form data.
			var $this = $(this);
			objSelf.SubmitComment( commentForm );
			// Stop default event.
			objEvent.preventDefault();
			return( false );
		}
	);
	$('a[title=deletelink]').click(
		function( objEvent ){
			// Submit the form data.
			var $this = $(this);
			objSelf.SubmitDeleteComment( $this );
			// Stop default event.
			objEvent.preventDefault();
			return( false );
		}
	);

	this.SetBusy( false );
}

Comments.prototype.SubmitComment = function( jForm ){
	var objSelf = this;
	// Check to see if page is currently busy.
	if (objSelf.GetBusy()){
	// Alert busyness.
		objSelf.AlertBusy();
	} else {
		// Flag page as busy.
		objSelf.SetBusy( true );
	}
	// Define the post properties.
	var objPostProperties = {
		"do": "api.comments.postComment",
		associatedid: jForm.find("input[name='associatedid']").val(),
		body: jForm.find("textarea[name='body']").val(),
		type: jForm.find("input[name='type']").val()
		};
	// Make API request with the given data.
	$.post(
		this.ControllerURL,
		objPostProperties,
		function( objResponse ){
			objSelf.SubmitCommentHandler( objResponse , jForm );
		},
		"json"
		);
}

Comments.prototype.SubmitCommentHandler = function( objResponse , jForm ){
	var newComment = $( "#newComment" );
	$("#post-comment-container" ).remove();
	// Check to see if there are errors. If there are, alert.
	if (objResponse.SUCCESS){
		// Setup & clear.
		newComment.html(objResponse.DATA);
		this.SetBusy( false );
	} else {
		// There were errors.
		this.ShowAPIErrors( objResponse.ERRORS );
	}
}

Comments.prototype.SubmitDeleteComment = function( jLink ){
	var objSelf = this;
	if (objSelf.GetBusy()){
		objSelf.AlertBusy();
	} else {
		objSelf.SetBusy( true );
	}
	// Define the post properties.
	var objPostProperties = {
		"do": "api.comments.deleteComment",
		"ObjectID": jLink.attr("id")
		};
	// Make API request with the given data.
	$.get(
		this.ControllerURL,
		objPostProperties,
		function( objResponse ){
			objSelf.SubmitDeleteCommentHandler( objResponse , jLink );
		},
		"json"
		);
}

Comments.prototype.SubmitDeleteCommentHandler = function( objResponse , jLink ){
	if (objResponse.SUCCESS){
		jLink.parent().remove();
		this.SetBusy( false );
	} else {
		this.ShowAPIErrors( objResponse.ERRORS );
	}
}
