/**
 * Class for incrementally loading page comments using XHR.
 * @author Stefan T.
 * @copyright Godengo Inc., 2008
 *
 * DEPENDENCIES:
 *  prototype.js must be loaded prior to this
 **/

/* @global */
UserCommentsLoader = Class.create();

UserCommentsLoader.prototype = {

	initialize: function (commentsContainer, config) {
		this.commentsContainer = $(commentsContainer);
		this.loader_img = config.loader_img || '/core/media/images/ajax-loader.gif';
		this.backendurl = config.backendurl || '/core/ajaxserver.php';
		this.max = config.max || 10;
		this.publicationid = config.publicationid || 0;
	},

	//load a paginated commments list with pager via AJAX-calls as HTML
	//and update the given container element with it.
	get: function(pageid, offset, cp, order) {
		var ajaxRequest = new Ajax.Updater(this.commentsContainer, this.backendurl, {
    		method:       'get',
    		parameters:   {	'req':'getUserComments',
							'pageid' : pageid,
							'offset' : offset || 0,
							'max' : this.max,
							'cp_comments_pager' : cp || 1,
							'sortorder': order || 'asc',
							'publicationid' : this.publicationid
						  },
    		asynchronous: true,
    		onLoading : this.onLoading(),
    		onFailure: this.onFailure(),
    		onComplete : this.onComplete()
  		});
	},

	//replace comments list with a 'loading' text and progress image
	onLoading : function(transport) {
		if(this.commentsContainer)
			this.commentsContainer.innerHTML = '<p style="padding-top:50px;color:#ccc;text-align:center;">Loading comments ...</p><p style="padding-bottom:50px;text-align:center;"><img style="display:inline;" src="' + this.loader_img + '" border="0"></p>';
	},

	//if the call fails, show nothing for now
	onFailure : function(transport) {
		if(this.commentsContainer)
			this.commentsContainer.innerHTML = "";

	},

	//once we're done, set the page focus on top of the comments section
	onComplete : function(transport) {
		var commentsAnchor = $('comments');
		if(commentsAnchor)
			commentsAnchor.scrollTo();
	}

}