Ext.namespace('Altius.Comments');






Altius.Comments.Init = function() {

	items=Ext.select('#articleComments');
	items.each(function(div) {
		new Altius.Comments.Component({renderTo:Ext.id(div)});
	});

};

Altius.Comments.Component= Ext.extend(Ext.Component, {
	onRender: function() {
		this.table=  this.container.getAttributeNS('','altiustable');
		this.record= this.container.getAttributeNS('','altiusrecord');
		console.log('new init',this.table,this.record);
		var dh = Ext.DomHelper;

		this.header = this.container.first('.commentsHeader');
		if(this.header ===null) {
			this.header= dh.append(this.container,{cls:'commentsHeader'});			// to contain comment status information
		}

		this.descLink=Ext.get('commentsSortDesc');

		this.ascLink=Ext.get('commentsSortAsc');
		this.descLink.on('click',this.doSort,this);
		this.ascLink.on('click',this.doSort,this);


		this.listing = this.container.first('.commentListing');
		if(this.listing ===null) {
			this.listing= dh.append(this.container,{cls:'commentListing'});			// to contain comment listing information
			this.getComments();
		}
		this.formPanel = this.container.first('.commentForm');
		if(this.formPanel ===null) {
			this.formPanel= dh.append(this.container,{cls:'commentForm'});		// to contain comment form
			this.getForm();
		}


	},
	getForm: function() {
		TJ.CallbackManager.Direct({ cls: 'Comments',
									method: 'GetCommentForm',
									params:	{
											  recordid: this.record,
											  table:  this.table
										},
									callback: this.getFormCallback,
									scope: this });

	},
	getFormCallback: function(val,res,opt) {
//		console.log(val);
		if(! val) 
			return;
		this.form = new TJ.Admin.Form ( 
		{
			title: val.title,
			autoScroll: true,
			labelWidth: 150,
			focus: false,
			baseParams: { table: this.table, record: this.record},
			defaults: {anchor: '-20',selectOnFocus: true, msgTarget: 'side' },
			url:   TJ.LANGUAGESS + '_comments:submit',
			buttons:  val.buttons,
			items:  val.items,
			renderTo: this.formPanel,
			callback: this.submitSuccess,
			scope: this
		});
	},

	doSort: function(e,el) {
        e.preventDefault();
		Ext.fly(el).radioClass('active');
		this.getComments();


	},

	getComments: function(params) {

		TJ.CallbackManager.Direct({ cls: 'Comments',
									method: 'GetComments',
									params:	{
											  recordid: this.record,
											  table:  this.table,
											  highlight: params && params.highlight,
											  sort: this.ascLink.hasClass('active') ? 'asc' : 'desc'
										},
									updateEl: this.listing,
									callback: this.getCommentsCallback,
									scope: this });
	},
	getCommentsCallback: function(val,res,opt) {
//		console.log(val,res,opt);
		TJ.ExpandColumns();
		if(opt.params.highlight){
			var el = Ext.get('comment-' +opt.params.highlight);
			if(el)
				el.highlight();

		}
		var mods=Ext.fly(this.listing).select('.commentModerate');
//		console.log(mods);
		mods.on('click',this.moderate,this);

	},
	moderate: function(e,el) {
        e.preventDefault();
		rec=Ext.get(el).findParent('.commentRecord',10,true);
		TJ.CallbackManager.Direct( {	cls:		'Comments',
										method :	'Moderate',
										params:	{
													commentid: Ext.fly(rec).getAttributeNS('','altiuscommentid'),
													status: el.getAttribute('class')},
										callback: this.moderateCallback,
										scope:    this,
										record:   rec});


	},
	moderateCallback: function(val,res,opt) {
		if(val.status) {
			opt.record.removeClass('status_hidden');
			opt.record.removeClass('status_live');
			opt.record.removeClass('status_censored');
			opt.record.removeClass('status_pending');
			opt.record.addClass('status_' + val.status);


			if(val.count) {
				Ext.get('commentCount').update(String.format('Live {0}, Pending {1}, Other {2}',val.count.comments_live+'',val.count.comments_pending+'',val.count.comments_other+'')).stopFx().highlight();
			}
		}
	},
	submitSuccess : function(val,action) {
		this.form.form.reset();
		this.getComments({highlight: val.data});

	}

});




