/*
	comment.js
	
	dependencies
		prototype.js
		
	resources
		http://www.prototypejs.org/api/ajax/options
*/


var Comments = Class.create();

/*
	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!
*/
Comments.DOMAIN			= document.domain;
Comments.PORT			= window.location.port;
Comments.SVC_ADD		= "/club/modules/comments/svcAddComment.php";
Comments.SVC_SHOW		= "/club/modules/comments/svcShowComments.php";

Comments.prototype =  {
	
	// --------------------------------------------------------------------------------
	// Private section
	// --------------------------------------------------------------------------------

	// --------------------------------------------------------------------------------
	// --------------------------------------------------------------------------------

	__showXmlHttpRequest: function( /* string */ eventName, /* object */ xmlHttpRequest )
	{
		/*
		alert(	eventName + ": "			+ xmlHttpRequest							+ " \n\n " +
				"All response headers: \n"	+ xmlHttpRequest.getAllResponseHeaders()	+ " \n\n " +
				"readyState: "				+ xmlHttpRequest.readyState					+ " \n\n " +
				"responseText: "			+ xmlHttpRequest.responseText				+ " \n\n " +
				"responseXML: " 			+ xmlHttpRequest.responseXML				+ " \n\n " +
				"status: " 					+ xmlHttpRequest.status						+ " \n\n " +
				"statusText: "				+ xmlHttpRequest.statusText					+ " \n\n " 
		);
		*/
	},
	
	//	object xmlHttpRequest
	doLoaded: function( xmlHttpRequest )
	{
		this.__showXmlHttpRequest( "onLoaded", xmlHttpRequest );
	},
	doComplete: function( xmlHttpRequest )
	{
		this.__showBusyAddComment( false );
		this.__showXmlHttpRequest( "onComplete", xmlHttpRequest );
	},
	doSuccess: function( xmlHttpRequest )
	{
		this.__showXmlHttpRequest( "onSuccess", xmlHttpRequest );
	},
	doFailure: function( xmlHttpRequest )
	{
		this.__showXmlHttpRequest( "onFailure", xmlHttpRequest );
	},

	__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;		
	},
	
	__showBusyAddComment: function( enabled )
	{
		var busy = $("addingComment");
		
		//alert( "busy:" + busy );
		if ( busy != null )
		{
			if ( enabled )
			{
				busy.show();
			}
			else
			{
				busy.hide();
			}
		}
	},

	// --------------------------------------------------------------------------------
	// public section
	// --------------------------------------------------------------------------------
	
	// ----------------------------------------
	// ----------------------------------------
	
	initialize: function()
	{
		//alert( "Comment:: initialize()" );
		
		this.__showBusyAddComment( false );
		
		this.name = "Comments";
		this.ajaxOptions = 
		{
			asynchronous: true, 
		    method: 'get',
			evalScripts: true,
			onLoaded: this.doLoaded.bind( this ),
			onComplete: this.doComplete.bind( this ),
			onSuccess: this.doSuccess.bind( this ),
			onFailure: this.doFailure.bind( this )
		};		
	}, // initialize
	
	// ----------------------------------------
	// ----------------------------------------
	
	// ----------
	//	string boxId
	add: function( boxId, serializedForm )
	{
		//alert( "Comment.add( " + boxId + ", " + serializedForm + ", " + " )" );
		this.__showBusyAddComment( true );
		
		var svcUri = this.__buildLinkToSvc( Comments.SVC_ADD, "" );
		
		var ajaxOptions = 
		{
			asynchronous: true, 
		    method: 'post',
			parameters: serializedForm,
			evalScripts: true,
			onLoaded: this.doLoaded.bind( this ),
			onComplete: this.doComplete.bind( this ),
			onSuccess: this.doSuccess.bind( this ),
			onFailure: this.doFailure.bind( this )
		};		

		ajax = new Ajax.Updater( boxId, svcUri, ajaxOptions );		
	},
	
	showAll: function( boxId )
	{
		//alert( "Comments.showAll( " + boxId + " )" );

		var svcUri = this.__buildLinkToSvc( Comments.SVC_SHOW, "" );

		ajax = new Ajax.Updater( boxId, svcUri, this.ajaxOptions );		
	},
	
	show: function( boxId, sourceId )
	{
		//alert( "Comments.show( " + boxId + ", " + sourceId + "  )" );

		var qrystr = "sourceId=" + sourceId;
		var svcUri = this.__buildLinkToSvc( Comments.SVC_SHOW, qrystr );

		ajax = new Ajax.Updater( boxId, svcUri, this.ajaxOptions );		
	}

} // 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( event )
{
	//alert( "submitCommentHandler( " + event + " )" );
	//alert( "this = " + this );
	
	// Serialize the entire form (this is a prototype helper) and post it.
	var comments = new Comments();
	comments.add( 
				"comments", 
				$("frmNewComment").serialize( /* getHash */ true ) 
	);
}

// Watch for the PrototypeJS dom:loaded event.
// This will fire right after the DOM is loaded.
// Resources (i.e. images) are still being loaded.
document.observe(
	"dom:loaded", 
	function() 
	{
		//alert( "DOM:Loaded" );		
		// This will init a Comments object and 
		// hide the "working" image.
		var comments = new Comments(); 
				
		// Connect mouse event handlers.
		var btn = $("btnSubmitComment");
		if ( btn != null )
		{
			$("btnSubmitComment").observe( "click", submitCommentHandler.bind( /* the element being observed */ this ) );
		}
	}
);


/**/