var CComments = Class.create();
CComments.prototype = {
	initialize: function() {
		var obj = this;
		// Comments list
		this.cmLayer = $('commentLayer');
		if(!this.cmLayer) return;
		this.cmConn = new CSrvConnect('commentLayer', '/comment/', '?id=' + $('content_id').value);
		this.cmConn.onLoad = function(t){
			obj.cmLayer.update(t.responseText);
			obj.applyNavigation();
		};
		this.applyNavigation();

		// Adding new comment
		if($('commentAddLayer')) {
			this.cmAddConn = new CSrvAddEdt('comment', 'post/', '', 'commentLayer');
			this.cmAddConn.root = '/comment/';
	
			this.cmAddConn.onDataApplySuccess = function(){
				// clear comment text field
				$('cm_text').value = '';
				// refresh comments
				obj.cmConn.load();
			}
			this.cmAddConn.setEvents();
		}
	},//---------------------------------------------------------------------
	
	applyNavigation: function(){
		var obj = this;
		$$('#commentLayer .page-navigation a').each( function(elem){
			Event.observe( elem, 'click', function(event){
				var page = this.id.split('-')[1];
				if(page=='x') return false;
				obj.cmConn.load( {'page':page} );
				return false;
			} );
		} );
	},//---------------------------------------------------------------------
	
	cmLayer: null,		// comments list layer
	cmConn: null,		// comments connector
	cmAddConn: null		// add new comment connector
};
/////////////////////////////////////////////////////////////////////////////

Event.onReady( function(){
	new CComments();
} );
//---------------------------------------------------------------------------
