/*
	comment.js
*/


// --------------------------------------------------------------------------------
// Constants
// --------------------------------------------------------------------------------

/*
	We use 
		 "document.domain" and 
		 "window.location.port" 
	to make this script as domain.port independent as possible.
	To put it simple, no configuration, this should work on every client/server!
*/
var COMMENTS_DOMAIN			= document.domain;
var COMMENTS_PORT			= window.location.port;
var COMMENTS_SVC_ADD		= "/club/modules/comments/svcAddComment.php";
var COMMENTS_SVC_SHOW		= "/club/modules/comments/svcShowComments.php";

// --------------------------------------------------------------------------------
// Class LESLink
// ----------------------------------------

// ----------------------------------------
// Private section
// ----------------------------------------

Comments.prototype.__buildLinkToSvc = function( svc, qrystr )
{	
	//alert( "Comments.__buildLinkToSvc: function( " + svc + ", " + qrystr + " )" );
	
	var reqURI = "http://" + COMMENTS_DOMAIN + ":" + COMMENTS_PORT + svc;
	
	if ( qrystr != "" )
	{
		reqURI += ( "?" + qrystr );
	}
	
	//alert( reqURI );
	return reqURI;		
}
	
Comments.prototype.__showBusyAddComment = function( enabled )
{
	//alert( "Comments:: __showBusyAddComment( " + enabled + " )" );
	
	var busy = $("#addingComment");	
	//alert( "busy:" + busy );
	
	if ( busy != null )
	{
		if ( enabled )
		{
			busy.show();
		}
		else
		{
			busy.hide();
		}
	}
}

// ----------------------------------------
// Constructor
// ----------------------------------------

function Comments()
{
	//alert( "Comment:: Comments()" );
		
	this.name = "Comments";			
	this.__showBusyAddComment( false );
}

// ----------------------------------------
// Public section
// ----------------------------------------

Comments.prototype.add = function( boxId, serializedForm )
{
	//alert( "Comment.add( " + boxId + ", " + serializedForm + ", " + " )" );

	this.__showBusyAddComment( true );

	var boxId = "#" + boxId;
	var svcUri = this.__buildLinkToSvc( COMMENTS_SVC_ADD, "" );
	$(boxId).load(
				svcUri, 
				serializedForm,
				function() 
				{
					var comments = new Comments();
				}
	);
}
	
Comments.prototype.showAll =  function( boxId )
{
	//alert( "Comments.showAll( " + boxId + " )" );

	var boxId = "#" + boxId;
	var svcUri = this.__buildLinkToSvc( COMMENTS_SVC_SHOW, "" );
	$(boxId).load(svcUri);
}
	
Comments.prototype.show = function( boxId, sourceId )
{
	//alert( "Comments.show( " + boxId + ", " + sourceId + "  )" );

	var boxId = "#" + boxId;
	var qrystr = "sourceId=" + sourceId;
	var svcUri = this.__buildLinkToSvc( COMMENTS_SVC_SHOW, qrystr );
	$(boxId).load(svcUri);
}

// ----------------------------------------
// End of Class
// --------------------------------------------------------------------------------





/*	----------------------------------------
	Helper/support methods
	----------------------------------------
*/

// 
// Show ALL comments in the boxId
//
function showAllComments( boxId )
{
	//alert( "function showAllComments( " + boxId + " )" );
	
	var comments = new Comments();
	comments.showAll( boxId );
}

// 
// Show ALL comments for a sourceId in the boxId
//
function showComments( boxId, sourceId )
{
	//alert( "function showComments( " + boxId + ", " + sourceId + " )" );
	
	var comments = new Comments();
	comments.show( boxId, sourceId );
}

//
// Add a comment (serializedForm) and show ALL comments in the boxId
//

// User clicked the comment submit button.
// POST the entire form (AJAX call) to the PHP "service".
// Validating, sanitizing, etc. is done on the server.
function submitCommentHandler()
{
	//alert( "submitCommentHandler()" );
	
	// Serialize the entire form and post it.
	var comments = new Comments();
	comments.add( 
				"comments", 
				$("#frmNewComment").serialize() 
	);
}

$(document).ready(
			function ()
			{
				//alert("document ready - begin");

				var comments = new Comments(); 

				$('#btnSubmitComment').click( function() 
					{
						submitCommentHandler();
					}
				);

				//alert("document ready - end");
			}
		);


/**/
