$(document).ready(function() {
//скрыть все формы ответов
	$('.line_block .newsList .answerBlock').hide();
	$('#ftagstext').hide();

//при нажатии на кнопку ответить, закрыть все формы ответов, и паказать текущую форму
	$('.hover.answer').click(function(){
		$('.hover').show();
		$('.line_block .newsList .answerBlock').slideUp("slow");
		//$(this).hide();
		var id = '#answerBlock_' + $(this).attr('id');
		$(id).slideToggle("slow");
	});

//подгрузка экспертов соответствующих теме
/*
$('#ftheme').change(function(){
		$.ajax({
			url: '/faq/ajax_get_experts/'+$('#ftheme option:selected').attr('value'),
			success: function(data){$('div#expert').html(data)},
			//error: function(html){alert(html.statusText)},
			type: 'post'
		})
	});
*/

//использовать/не использовать тэги
	$('#ftags').change(function(){
		$('#ftagstext').toggle();
		$('#ftagstext').attr('value', '');
	});

//отправка вопроса на модерацию
	$('#fqsubmit').click(function(){
		$.post(
			'/faq/ajax_question_save/',
			{
				theme: $('#ftheme option:selected').attr('value'),
				respondent: $('.fchoice option:selected').attr('value'),
				text: $('#ftext').attr('value'),
				new_tags: $('#ftagstext').attr('value')
			},
			function(data){
				if(data == "Ok")
					$('.askaquestion').html('Ваш вопрос поставлен в очередь на модерацию.');
				else
					$('#faq_errors').html(data);
				}

		)
	});

//подписаться не оставляя комментарий
	$('#subsnocom_question').click(function(){
		var id = $(this).attr('question_id');
		$.post(
			'/faq/ajax_add_subcriptioner/',
			{
				question: id
			}
			,
			function(data){
				$('#'+id+'text').text(data);
			}
		)
	});

//смена эксперта
	$('#change_expert_form').submit(function(ev){
			if(confirm('Сменить эксперта?'))
			{
				var prm = {};

				$(this).find(':input,:hidden, #frespondent').each( function(i){ prm[this.name] = this.value; });

				$.post(this.action, prm, function(){ window.location.reload(); });
			}
			ev.preventDefault();
	});


//отправка ответа на модкрацию
	$('.fanswer').click(function(){
		var id = $(this).attr('question_id');
		$.post(
			'/faq/ajax_answer_save/',
			{
				expert: $('#fexpert'+id).attr('checked'),
				subscription: $('#fsubscribe'+id).attr('value'),
				text: $('#fanswertxt'+id).attr('value'),
				question: $('#fquestion'+id).attr('value'),
				parent: $('#fparent'+id).attr('value')
			},
			function(data){
				if(data == "Ok")
				{
					$('#faq_errors'+id).html('Ваш ответ поставлен в очередь на модерацию.');
					$('#answerBlock_'+id).slideToggle('slow');
					$('#fanswertxt'+id).attr('value', '');
				}
				else if(data == 'Exp')
				{
					window.location.reload();
				}
				else
					$('#faq_errors'+id).html(data);
				}
		)
	});

//preview

	$('#question_preview').dialog({
		autoOpen: false
		, modal: true
		//, position: 'center'
		, width:'auto'
		//, height:'auto'
	});


	$('#show_question_preview').click( function(event){

		var $prvw = $('#question_preview');
		var $theme = $prvw.find('div.theme span #pre_ftheme');
		var $tags  = $prvw.find('div.theme span #pre_ftags');


		event.preventDefault();

		$('#pre_ftext').html( $('#ftext').val() );
		$theme.text( $('#ftheme option:selected').text() );
		$tags.text( $('#ftagstext').val() );

		$prvw.dialog('open');
	});

});
