/* Initializes the ajax paging of the RateCommentDisplay content group. */
function initRateCommentDisplayAjax(siteId, locale, pageId, sec, pos, numpp) {
	// init global vars
	window.siteId= siteId;
	window.locale= locale;
	window.pageId= pageId;
	window.commentsSec= sec;
	window.commentsPos= pos;

	if (numpp != null) {
		window.commentsNumPerPage= numpp;
		Event.observe(window, 'load', function() {
			addPagingLinkEvents() ;
		});
	}
}

/* Adds the onclick events for the paging links. */
function addPagingLinkEvents() {
	$$('.commentsPaging .pagingLink').each(function(lnkItem) {
		var pageNum = 0;
		Element.classNames(lnkItem).each(function(className) {
			if (className.indexOf('pagingLinkNum_')==0) {
				pageNum = parseInt(className.substring('pagingLinkNum_'.length, className.length));
			}
		});
		Event.observe(lnkItem, 'click', function(event) {
			event.preventDefault();
			updateCommentsListByAjax(pageNum);
		});
	});
}

/*
 * Updates the comments list of the RateCommentDisplay content group.
 * PRECON:
 * The global variables siteId, locale, pageId, commentsSec and commentsPos must be set
 * (e.g. by the initCommentsAjaxPaging function).
 * If you want to use paging the global variable commentsNumPerPage must be set. If it is not,
 * the pageNum will be ignored and all comments are returned.
 * 
 * If you want to update with all comments (no paging), set pageNum=-1.
 */
function updateCommentsListByAjax(pageNum) {
	var url = 'servlet/AjaxActionServlet?';
	url += 'siteid='+window.siteId;
	url += '&locale='+window.locale;
	url += '&action=commentAndRate';
	url += '&func=ratingsCommentsDisplay';
	url += '&id='+window.pageId;
	url += '&sec='+window.commentsSec;
	url += '&pos='+window.commentsPos;
	url += '&pageId='+window.pageId;
	if (window.commentsNumPerPage && pageNum > -1) {
		url += '&npp='+window.commentsNumPerPage;
		url += '&page='+pageNum;
	}

	showAjaxWaitingScreen('ratings_comments_waiting');
	new Ajax.Request(url, {
		method: 'post',
		onComplete: function() {
			hideAjaxWaitingScreen('ratings_comments_waiting');
		},
		onSuccess: function(response) {
			var xmlDoc = response.responseXML;
			if(xmlDoc) {
				var elem = xmlDoc.firstChild;
				if(elem) {
					var code = elem.getAttribute("code");
					if (code == '0') {
						if($('displayedEntries')) { $('displayedEntries').update(elem.firstChild.data); }
						addPagingLinkEvents();
					} else {
						if (code != '-1') {
							alert('Error [' + code + ']');
						}
					}
				}
			}
		}
	});	
}

