/*
 ls.question
*/
ls.question = {
	add: function(b) {
        if (!this.element.hasClass('max')) {
	        var v = $(b).getParent();
    	    v.clone().injectAfter(v).getElements('.act').makeHover();
        	this.update();
        }
	},
	del: function(b) {
		if (!this.element.hasClass('min')) {
			$(b).getParent().dispose();
			this.update();
		}
	},
	up: function(b) {
		if (!$(b).getParent().hasClass('first')) {
			var v = $(b).getParent(), p = v.getPrevious();
			v.dispose().inject(p, 'before');
			this.update();
		}
	},
	down: function(b) {
		if (!$(b).getParent().hasClass('last')) {
			var v = $(b).getParent(), p = v.getNext();
			v.dispose().inject(p, 'after');
			this.update();
		}
	},
	update: function() {
		var el = this.element, vars = el.getChildren();
		vars.removeClass('first').removeClass('last').each(function(v, i) {
			v.getChildren('.num')[0].set('text', (i+1) + '.');
		});
		el.getFirst().addClass('first');
		el.getLast().addClass('last');
		el.removeClass('min').removeClass('max');
		if (vars.length == 2) el.addClass('min');
		if (vars.length == 20) el.addClass('max');
	}
}

ls.ready(function() {
	function act(type) {
		return new Element('div', {
			'class': 'act ' + type,
			onclick: 'ls.question.' + type + '(this)'
		}).makeHover();
	}
	ls.question.element = $$('.ls-question')[0];
	try {
		ls.question.element.getChildren().each(function(el) {
			el.wrapInner(new Element('div', {'class': 'box'})).adopt(new Element('div', {'class': 'num'}));
			if (!el.getElement('input').disabled) el.adopt(act('add'), act('del'), act('up'), act('down'));
		});
		ls.question.update();
	} catch (e) {}
	$$('[data-question]').each(function(el) {
		var tid = el.parseData('question').target;
		el.getElements('button.vote').addEvent('click', function() {
			var aid = this.hasClass('abstain') ? -1 : (el.toHash()['topic_answer_' + tid] || false);
			if (aid !== false)
				ls.json(aRouter.ajax + 'vote/question', {idTopic: tid, idAnswer: aid}, function(result) {
					ls.alert(result.sMsgTitle, result.sMsg);
					el.getParent().set('html', result.sText);
				});
		});
	});
});
