
// This is the page controller constructor. This controller handles
// the View Photo page.
function PhotoViewPage( strControllerURL, objPostProperties ){
	this.ControllerURL = strControllerURL;
	this.PostProperties = objPostProperties;
	
	// Initialize page.
	this.Init();
}

// Extend the page controller class.
PhotoViewPage.prototype = new PageControllerClass;


// When page loads, hook up events and properties.
PhotoViewPage.prototype.Init = function(){
	var objSelf = this;
	var jForm = $( "#add-comment" );
	
	// Flag as busy.
	this.SetBusy( true );
	
	// Hook up form submission.
	jForm.submit(
		function( objEvent ){
			// Submit the form data.
			objSelf.SubmitCommentForm();
			
			// Stop default event.
			objEvent.preventDefault();
			return( false );
		}
		);
	
	// Load the comment data.
	this.GetCommentMarkup();
	
	// Leave init method in busy state since the get markup will end it.
}


// This submits a comment.
PhotoViewPage.prototype.SubmitCommentForm = function( jForm ){
	var objSelf = this;
	var jForm = $( "#add-comment" );
	var jLoaderImage = jForm.find( "img.ajax-loader" );
	
	// 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.comment.add",
		comment: jForm.find( "input[ name = 'comment' ]" ).val(),
		photo_id: this.PostProperties.id
		};
		
	// Show loader.
	jLoaderImage.show();
		
	// Make API request with the given data.
	$.post(
		this.ControllerURL,
		objPostProperties,
		function( objResponse ){
			objSelf.SubmitCommentFormHandler( objResponse );
		},
		"json"
		);
}


// This handles the submit form handler.
PhotoViewPage.prototype.SubmitCommentFormHandler = function( objResponse ){
	var jForm = $( "#add-comment" );
	var jLoaderImage = jForm.find( "img.ajax-loader" );
	
	// Hide loader.
	jLoaderImage.hide();
	
	// Check to see if there are errors. If there are, alert.
	if (objResponse.SUCCESS){
		
		// Clear the comment field.
		jForm.find( "input[ name = 'comment' ]" ).val( "" );
		
		// Load the comment data.
		this.GetCommentMarkup();
		
	} else {
		
		// There were errors.
		this.ShowAPIErrors( objResponse.ERRORS );
			
	}
}


// This gets the comment markup. We are going to assume that the 
// page is already busy at this point.
PhotoViewPage.prototype.GetCommentMarkup = function(){
	var objSelf = this;
	
	// Define the post properties.
	var objPostProperties = {
		"do": "api.photo.get-comment-list-markup",
		photo_id: this.PostProperties.id
		};
		
	// Make API request with the given data.
	$.post(
		this.ControllerURL,
		objPostProperties,
		function( objResponse ){
			objSelf.GetCommentMarkupHandler( objResponse );
		},
		"json"
		);
}


// This handles the comment API response.
PhotoViewPage.prototype.GetCommentMarkupHandler = function( objResponse ){
	var jCommentsContainer = $( "#comment-list-container" );
	
	// Check to see if there are errors.
	if (objResponse.SUCCESS){
		
		// The API call was successful. Clear out the current container 
		// and then add the comment markup to the dispaly.
		jCommentsContainer
			.empty()
			.append( objResponse.DATA )
		;
		
	} else {
		
		// There were errors.
		this.ShowAPIErrors( objResponse.ERRORS );
			
	}
	
	// Set the page as no longer busy.
	this.SetBusy( false );
}


