$(function() {
	var root = $('#QnA');
	var blockClass = 'hidden-answer';
	var isAnimate = false;

	root.click(function(e) {
		if (!isAnimate && $(e.target).hasClass('pseudo')) {
			var block = getBlock($(e.target));

			if (false !== block) {
				onClick(block);
			}
		}
	});

	function getBlock(el) {
		var curEl = el;

		while (!curEl.hasClass(blockClass)) {
			if ('BODY' == curEl.get(0).nodeName) {
				return false;
			}

			curEl = curEl.parent();
		}
		return curEl;
	}

	function onClick(block) {
		var animatingEl = block.find('div');

		if (animatingEl.hasClass('hidden')) {
			show(animatingEl);
		} else {
			hide(animatingEl);
		}
	}

	function show(el) {
		isAnimate = true;
		el.css({
			left: -10000,
			position: 'absolute',
			visibility: 'hidden',
			width: el.parent().width() -20,
			display: 'block'
		});
		var height = el.height();

		el.css({
			left: '',
			position: '',
			visibility: '',
			height: 0,			
			display: 'block',
			overflow: 'hidden'
		}).removeClass('hidden');

		jTweener.addTween(el, {
			height: height,
			time: 0.4,
			onComplete: function() {
				el.css({
					display: '',
					width: '',
					height: '',
					overflow: ''
				});
				isAnimate = false;
			}
		});
	}

	function hide(el) {
		isAnimate = true;

		var height = el.height(); 
		el.css ('overflow', 'hidden');

		jTweener.addTween(el, {
			height: 0,
			time: 0.3,
			onComplete: function() {
				el.addClass('hidden').css('height', '');
				el.css ('overflow', '');
				isAnimate = false;
			}
		});
	}
}); 