/*
ls.comments
*/
ls.comments = {	url: {		topic: {			add: aRouter.blog + 'ajaxaddcomment/',
			response: aRouter.blog + 'ajaxresponsecomment/',
			toggle: aRouter.ajax + 'comment/delete/'		},
		talk: {			add: aRouter.talk + 'ajaxaddcomment/',
			response: aRouter.talk + 'ajaxresponsecomment/'
		}	},
	options: {	},
	newComments: []
}

ls.CmtTreeClass = new Class({

	form		: 'form_comment',
	text		: 'comment_text',

	initialize: function() {		$extend(this, ls.comments.options);
		this.url = ls.comments.url[this.target];
		this.scroller = new Fx.Scroll(document.getDocument(), {duration: 500, offset: {x: 0, y: 0}});
		this.toggleForm(this.cmtId = 0);
		this.newComments = ls.comments.newComments;
		$$('.comment').each(function(cmt) {			if (cmt.getElements('.comment-children .comment').length)				cmt.getElement('.folding').show().addClass('expanded');		});
		this.updateNewCount();
	},
	toggleNode: function(id) {		$('comment-children-' + id).toggle();
		$('comment-id-' + id).getElement('.folding').toggleClass('expanded').toggleClass('collapsed');
	},
	expandAll: function() {		$$('.folding.collapsed').each(function(folding) {			folding.swapClass('collapsed', 'expanded').getNext('.comment-children').show();		});	},
	collapseAll: function() {		$$('.folding.expanded').each(function(folding) {
			folding.swapClass('expanded', 'collapsed').getNext('.comment-children').hide();
		});
	},
	add: function() {
		ls.editor.flush();
		var self = this, data = $(this.form).toHash();
		ls.json(this.url.add, data, function(result) {			self.response(data.cmt_target_id, result.sCommentId);
			$(self.text).set('text', '');
		});
	},
	response: function(tid, cid) {
		if (!cid) $$('.comment .content').removeClass('new').removeClass('view');
		$('update-comments').setProperty('src', DIR_STATIC_SKIN + '/images/update_act.gif');
        ls.json(this.url.response, {idCommentLast: this.lastId, idTarget: tid, typeTarget: this.target}, (
        	function(result) {	        	var aCmts = result.aComments;
    	   		if (aCmts.length > 0 && result.iMaxIdComment) {
        			this.lastId = result.iMaxIdComment;
       				var cnt = $('count-comments');
        			cnt.set('text', parseInt(cnt.get('text')) + aCmts.length);
	        		$$('[data-block=stream_comment]').fireEvent('click');
    	   			if (cid) this.toggleForm(this.cmtId, true); else this.newComments = [];
        			$each(aCmts, (function(cmt) {       					if (!(cid && cid == cmt.id)) this.newComments.push(cmt.id);
						var div = new Element('div', {
							'class': 'comment',
							id: 'comment-id-' + cmt.id,
							html: cmt.html
						});
						if (cmt.idParent) {							$('comment-children-' + cmt.idParent).appendChild(div);
							$('comment-id-' + cmt.idParent).getElement('.folding').show().addClass('expanded');
						}
						else div.inject($('comment-children-0'), 'before');
        			}).bind(this));
    	   			if (cid && $('comment-id-' + cid)) this.to.delay(1000, this, cid);
        		}
        		$('update-comments').setProperty('src', DIR_STATIC_SKIN + '/images/update.gif');
        	}).bind(this)
        );
	},
	toggleForm: function(id, reset) {
		var curr = $('reply-' + this.cmtId), next = $('reply-' + id);
		if (curr && next) {
			if (this.cmtId != id) {
				if(next.hasClass('hidden')) next.slide('hide').removeClass('hidden');
				curr.slide('out');
				tinyMCE.execCommand('mceRemoveControl', true, this.text);
				next.grab($(this.form)).slide(); $(this.form).reset();
				if (!$(this.text).hasClass('html'))
					tinyMCE.execCommand('mceAddControl', true, this.text);
				$('form-comment-reply').value = this.cmtId = id;
			} else {
				curr.slide();
				if (reset) $(this.form).reset();
			}
			$$('.comment.preview').hide();
		}
	},
    to: function(id) {		this.scroller.start(0, $('comment-id-' + id).getPosition().y);
		if (this.lastViewId) $('comment-content-id-' + this.lastViewId).removeClass('view');
		$('comment-content-id-' + id).addClass('view');
		this.lastViewId = id;
    },
	toggle: function(obj, id) {		var content = $('comment-content-id-' + id);
		ls.json(this.url.toggle, {idComment: id}, function(result) {			ls.alert(result.sMsgTitle, result.sMsg);
   			content.removeClass('old').removeClass('self').removeClass('new').removeClass('del');
   			obj.removeClass('delete').removeClass('repair');
   			if (result.bState) {
   				content.addClass('del');
   				obj.addClass('repair');
   			} else obj.addClass('delete');
			obj.set('text', result.sTextToggle);
		});
	},
	toParent: function(obj, id) {
		var pid = obj.href.substr(obj.href.indexOf('#') + 8);
        var cmt = $('comment-id-' + pid);
		cmt.getElement('.to-child').removeClass('hidden').getFirst().href = '#comment' + id;
		this.to(pid);
	},
	toChild: function(obj) {
		obj.getParent().addClass('hidden');
        this.to(obj.href.substr(obj.href.indexOf('#') + 8));
	},
	next: function() {
		var id = this.newComments.shift();
		if (id) {			this.to(id);
			this.updateNewCount();		}
	},
	updateNewCount: function() {		var div = $('new-comments'), count = this.newComments.length;
        try {        	if (count) div.set('text', count).show(); else div.hide();
        } catch(e) {}
	}
});

ls.ready(function() {	ls.CmtTree = new ls.CmtTreeClass();});

window.addEvent('keyup', function(e) {
	if(e.control && e.key == 'enter') {
		$('form_comment').getElement('button[name=submit_comment]').click();
		return!1;
	}
});
