$(document).ready(
function(){

	var current_expert_question = 'unansw';
	
	$('div#expert_questions').jScrollPane();
	
	$('a.show_answ').click(
		function()
		{
			$(this).blur().parent().siblings('div.expert_new_text').children('div.question_block').children('div.answer').slideToggle(function(){$('div#expert_questions').jScrollPane();});
			
			if($(this).text() == 'ответ')
				$(this).text('скрыть');
			else
				$(this).text('ответ');
				
			$(this).siblings('a.update_answ').toggle();
		
			return false;
		});
		
	$('div.question_block').click(
		function()
		{
			if($(this).hasClass('noclick')) return false;

			$(this).children('div.answer').slideToggle(function(){$('div#expert_questions').jScrollPane();});
			
			var link_show_answ = $(this).parent().siblings('div.question_actions').children('a.show_answ');

			if(link_show_answ.text() == 'ответ')
				link_show_answ.text('скрыть');
			else
				link_show_answ.text('ответ');
				
			$(link_show_answ).siblings('a.update_answ').toggle();
		});
	
	$('div#expert_question_title_unansw a').click(
		function()
		{
			$(this).blur();
			
			if(current_expert_question == 'unansw') return false;
			
			$('div.qstn_answ').hide();
			$('div.qstn_unansw').show();
			
			$(this).add('div#expert_question_title_answ a').toggleClass('nolink');
			
			$('div#expert_questions').jScrollPane();
			
			current_expert_question = 'unansw';

			return false;
		});

	$('div#expert_question_title_answ a').click(
		function()
		{
			$(this).blur();
			
			if(current_expert_question == 'answ') return false;
			
			$('div.qstn_unansw').hide();
			$('div.qstn_answ').show();
			
			$(this).add('div#expert_question_title_unansw a').toggleClass('nolink');
			
			$('div#expert_questions').jScrollPane();
			
			current_expert_question = 'answ';
			
			return false;
		});
	
	$('a.reply_qstn').click(
		function()
		{
			$(this).parent().siblings('div.expert_new_text').children('div.qstn_reply').slideDown(function(){$('div#expert_questions').jScrollPane();});
			$(this)	.siblings('a').andSelf().hide()
					.siblings('a.send_answ, a.cancel_reply_answ').show();
			
			
			return false;
		});
		
	$('a.cancel_reply_answ').click(
		function()
		{
			$(this).parent().siblings('div.expert_new_text').children('div.qstn_reply').slideUp(function(){$('div#expert_questions').jScrollPane();});
			$(this)	.siblings('a').andSelf().hide()
					.siblings('a.del_qstn, a.reply_qstn').show();
		
			return false;
		});
		
	$('a.send_answ').click(
		function()
		{
			var textarea = $(this).parent().siblings('div.expert_new_text').children('div.qstn_reply').children('textarea');
			var text = textarea.val();
			var qid = $(this).siblings('input:hidden').val();
			var that = this;
			
			$.ajax(
					{
						type: 'POST',
						url: '/request/replyexpertquestion',
						data: {id: qid, text: text},
						success:
							function(response)
							{
								var resp = $.toJSON(response);
								if(!resp.result)
									alert(resp.msg);
								else
								{
									textarea.val('')
											.parent().slideUp(function(){$('div#expert_questions').jScrollPane();})
											.siblings('div.question_block').removeClass('noclick')
											.children('div.answer').children('p').children('span.answer_text').text(text);
									
									$(that)	.siblings('a').andSelf().hide()
											.siblings('a.del_qstn, a.show_answ').show();
											
									var question = $(that).parents('div.qstn_unansw').removeClass('qstn_unansw').addClass('qstn_answ').fadeOut();
									if(question.prevAll('div').length != 0)
										question.parent().prepend(question);
								}
							}
					});
						
			return false;
		});
		
	$('a.del_qstn').click(
		function()
		{
			$(this)	.siblings('a').andSelf().hide()
					.siblings('a.cancel_del_qstn').show()
					.parent().siblings().animate({opacity: 0.3});
		
			$(this).everyTime(3000,
				function()
				{
					var qid = $(this).siblings('input:hidden').val();
					var that = this;			
								
					$.ajax(
					{
						type: 'POST',
						url: '/request/deleteexpertquestion',
						data: {id: qid},
						success:
							function(response)
							{
								var resp = $.toJSON(response);
								if(!resp.result)
								{
									alert(resp.msg);
									
									if(
										$(that)	.stopTime().show()
												.siblings('a.cancel_del_qstn').hide()
												.parent().siblings('div').animate({opacity: 1})
												.parent().hasClass('qstn_answ'))
										$(that).siblings('a.show_answ, a.update_answ').show()
									else
										$(that).siblings('a.reply_qstn').show();
								}
								else
								{
									$(that).parents('div:eq(1)').slideUp('300', function(){$(this).remove(); $('div#questions').jScrollPane();});
								}
							}
					});
								
					$(this).stopTime();
					
				});
		
			return false;
		});
		
	
	$('a.cancel_del_qstn').click(
		function()
		{
			if(
				$(this)	.hide()
						.siblings('a.del_qstn').stopTime().show()
						.parent().siblings('div').animate({opacity: 1})
						.parent().hasClass('qstn_answ'))
				$(this).siblings('a.show_answ, a.update_answ').show()
			else
				$(this).siblings('a.reply_qstn').show();
			
			return false;
		});
		
	$('a.update_answ').click(
		function()
		{
			$(this)	.siblings('a').andSelf().hide()
					.siblings('a.send_update_answ, a.cancel_send_answ').show()
					.parent().siblings('div.expert_new_text').children('div.question_block').toggleClass('noclick').children('div.answer').slideUp()
							.parent().siblings('div.qstn_reply').slideDown(function(){$('div#expert_questions').jScrollPane();});
							
			return false;
		});
		
	$('a.cancel_send_answ').click(
		function()
		{
			$(this)	.siblings('a').andSelf().hide()
					.siblings('a.show_answ, a.update_answ, a.del_qstn').show()
					.parent().siblings('div.expert_new_text').children('div.question_block').toggleClass('noclick').children('div.answer').slideDown()
							.parent().siblings('div.qstn_reply').slideUp(function(){$('div#expert_questions').jScrollPane();});
							
			return false;
		});
		
	$('a.send_update_answ').click(
		function()
		{
			var text = $(this).parent().siblings('div.expert_new_text').children('div.qstn_reply').children('textarea').val();
			var qid = $(this).siblings('input:hidden').val();
			var that = this;
			
			$.ajax(
					{
						type: 'POST',
						url: '/request/updateexpertquestion',
						data: {id: qid, text: text},
						success:
							function(response)
							{
								var resp = $.toJSON(response);
								if(!resp.result)
									alert(resp.msg);
								else
								{
									
									$(that)	.siblings('a').andSelf().hide()
											.siblings('a.show_answ, a.update_answ, a.del_qstn').show()
											.parent().siblings('div.expert_new_text').children('div.qstn_reply').slideUp()
													.siblings('div.question_block').toggleClass('noclick').children('div.answer').slideDown(function(){$('div#expert_questions').jScrollPane();}).children('p').children('span.answer_text').text(text);
								}
							}
					});
					
			return false;
		});
		
		
	
	$('a#make_expert_qstn').click(
		function()
		{
			if($.browser.msie)
			{
				$('div#expert_questions').parent().animate({height: 120}, function(){$('div#expert_qstn_text').fadeIn();$('div#expert_questions').jScrollPane();});
			}
			else
			{
				$('div#expert_qstn_text').slideDown();
				$('div#expert_questions').parent().animate({height: 120}, function(){$('div#expert_questions').jScrollPane();});
			}
			
			$(this).hide().siblings().show();
			
			return false;
		});

	$('a#cancel_expert_qstn').click(
		function()
		{
			$('div#expert_qstn_text').slideUp();
			$('div#expert_questions').parent().animate({height: 195}, function(){$('div#expert_questions').jScrollPane();});
			$(this).siblings('a#send_expert_qstn').andSelf().hide();
			$('div#expert_qstn_text textarea').val('');
			$('a#make_expert_qstn').show();
			
			return false;
		});
		
	$('a#send_expert_qstn').click(
		function()
		{
			var text = $('div#expert_qstn_text textarea').val();
			var uid = $('div#expert_qstn_text input:hidden').val();
			var that = $(this);

			$.ajax(
			{
				type: 'POST',
				url: '/request/sendexpertquestion',
				data: {text: text, uid: uid},
				success:
					function(response)
					{
						var resp = $.toJSON(response);
						if(!resp.result)
						{
							alert(resp.msg);
						}
						else
						{
							alert(resp.data);
							$('div#expert_qstn_text').slideUp();
							$('div#expert_questions').parent().animate({height: 195}, function(){$('div#expert_questions').jScrollPane();});
							$(that).siblings('a#cancel_expert_qstn').andSelf().hide();
							$('a#make_expert_qstn').show();
							$('div#expert_qstn_text textarea').val('');
						}
					}
			});
			
			return false;
		});
		
		
	//// MC
	
	////////
	
	/////////
	
	var current_mc_question = 'unansw';
	
	$('div#mc_questions').jScrollPane();
	
	$('a.mc_show_answ').click(
		function()
		{
			$(this).blur().parent().siblings('div.expert_new_text').children('div.mc_question_block').children('div.answer').slideToggle(function(){$('div#mc_questions').jScrollPane();});
			
			if($(this).text() == 'ответ')
				$(this).text('скрыть');
			else
				$(this).text('ответ');
				
			$(this).siblings('a.mc_update_answ').toggle();
		
			return false;
		});
		
	$('div.mc_question_block').click(
		function()
		{
			if($(this).hasClass('noclick')) return false;

			$(this).children('div.answer').slideToggle(function(){$('div#mc_questions').jScrollPane();});
			
			var link_show_answ = $(this).parent().siblings('div.question_actions').children('a.mc_show_answ');

			if(link_show_answ.text() == 'ответ')
				link_show_answ.text('скрыть');
			else
				link_show_answ.text('ответ');
				
			$(link_show_answ).siblings('a.mc_update_answ').toggle();
		});
	
	$('div#mc_question_title_unansw a').click(
		function()
		{
			$(this).blur();
			
			if(current_mc_question == 'unansw') return false;
			
			$('div.mc_qstn_answ').hide();
			$('div.mc_qstn_unansw').show();
			
			$(this).add('div#mc_question_title_answ a').toggleClass('nolink');
			
			$('div#mc_questions').jScrollPane();
			
			current_mc_question = 'unansw';

			return false;
		});

	$('div#mc_question_title_answ a').click(
		function()
		{
			$(this).blur();
			
			if(current_mc_question == 'answ') return false;
			
			$('div.mc_qstn_unansw').hide();
			$('div.mc_qstn_answ').show();
			
			$(this).add('div#mc_question_title_unansw a').toggleClass('nolink');
			
			$('div#mc_questions').jScrollPane();
			
			current_mc_question = 'answ';
			
			return false;
		});
	
	$('a.mc_reply_qstn').click(
		function()
		{
			$(this).parent().siblings('div.expert_new_text').children('div.qstn_reply').slideDown(function(){$('div#mc_questions').jScrollPane();});
			$(this)	.siblings('a').andSelf().hide()
					.siblings('a.mc_send_answ, a.mc_cancel_reply_answ').show();
			
			
			return false;
		});
		
	$('a.mc_cancel_reply_answ').click(
		function()
		{
			$(this).parent().siblings('div.expert_new_text').children('div.qstn_reply').slideUp(function(){$('div#mc_questions').jScrollPane();});
			$(this)	.siblings('a').andSelf().hide()
					.siblings('a.mc_del_qstn, a.mc_reply_qstn').show();
		
			return false;
		});
		
	$('a.mc_send_answ').click(
		function()
		{
			var textarea = $(this).parent().siblings('div.expert_new_text').children('div.qstn_reply').children('textarea');
			var text = textarea.val();
			var qid = $(this).siblings('input:hidden').val();
			var that = this;
			
			$.ajax(
					{
						type: 'POST',
						url: '/request/replymasterclassquestion',
						data: {id: qid, text: text},
						success:
							function(response)
							{
								var resp = $.toJSON(response);
								if(!resp.result)
									alert(resp.msg);
								else
								{
									textarea.parent().slideUp(function(){$('div#mc_questions').jScrollPane();})
											.siblings('div.mc_question_block').removeClass('noclick')
											.children('div.answer').children('span.answer_text').text(text);
									
									$(that)	.siblings('a').andSelf().hide()
											.siblings('a.mc_del_qstn, a.mc_show_answ').show();
											
									var question = $(that).parents('div.mc_qstn_unansw').removeClass('mc_qstn_unansw').addClass('mc_qstn_answ').fadeOut();
									if(question.prevAll('div').length != 0)
										question.parent().prepend(question);
								}
							}
					});
						
			return false;
		});
		
	$('a.mc_del_qstn').click(
		function()
		{
			$(this)	.siblings('a').andSelf().hide()
					.siblings('a.mc_cancel_del_qstn').show()
					.parent().siblings().animate({opacity: 0.3});
		
			$(this).everyTime(3000,
				function()
				{
					var qid = $(this).siblings('input:hidden').val();
					var that = this;
							
					$.ajax({
							type: 'POST',
							url: '/request/deletemasterclassquestion',
							data: {id: qid},
							success:
								function(response)
								{
									var resp = $.toJSON(response);
									
									if(!resp.result)
									{
										alert(resp.msg);
										
										if(
											$(that)	.stopTime().show()
													.siblings('a.mc_cancel_del_qstn').hide()
													.parent().siblings('div').animate({opacity: 1})
													.parent().hasClass('mc_qstn_answ'))
											$(that).siblings('a.mc_show_answ, a.mc_update_answ').show();
										else
											$(that).siblings('a.mc_reply_qstn').show();
									}
									else
									{
										$(that).parents('div:eq(1)').slideUp('300', function(){$(this).remove(); $('div#mc_questions').jScrollPane();});
									}
								}
						});		
							
					$(this).stopTime();
					
				});
		
			return false;
		});
		
	
	$('a.mc_cancel_del_qstn').click(
		function()
		{
			if(
				$(this)	.hide()
						.siblings('a.mc_del_qstn').stopTime().show()
						.parent().siblings('div').animate({opacity: 1})
						.parent().hasClass('mc_qstn_answ'))
				$(this).siblings('a.mc_show_answ, a.mc_update_answ').show()
			else
				$(this).siblings('a.mc_reply_qstn').show();
			
			return false;
		});
		
	$('a.mc_update_answ').click(
		function()
		{
			$(this)	.siblings('a').andSelf().hide()
					.siblings('a.mc_send_update_answ, a.mc_cancel_send_answ').show()
					.parent().siblings('div.expert_new_text').children('div.mc_question_block').toggleClass('noclick').children('div.answer').slideUp()
							.parent().siblings('div.qstn_reply').slideDown(function(){$('div#mc_questions').jScrollPane();});
							
			return false;
		});
		
	$('a.mc_cancel_send_answ').click(
		function()
		{
			$(this)	.siblings('a').andSelf().hide()
					.siblings('a.mc_show_answ, a.mc_update_answ, a.mc_del_qstn').show()
					.parent().siblings('div.expert_new_text').children('div.mc_question_block').toggleClass('noclick').children('div.answer').slideDown()
							.parent().siblings('div.qstn_reply').slideUp(function(){$('div#mc_questions').jScrollPane();});
							
			return false;
		});
		
	$('a.mc_send_update_answ').click(
		function()
		{
			var text = $(this).parent().siblings('div.expert_new_text').children('div.qstn_reply').children('textarea').val();
			var qid = $(this).siblings('input:hidden').val();
			var that = this;
			
			$.ajax(
					{
						type: 'POST',
						url: '/request/updatemasterclassquestion',
						data: {id: qid, text: text},
						success:
							function(response)
							{
								var resp = $.toJSON(response);
								if(!resp.result)
									alert(resp.msg);
								else
								{
									
									$(that)	.siblings('a').andSelf().hide()
											.siblings('a.mc_show_answ, a.mc_update_answ, a.mc_del_qstn').show()
											.parent().siblings('div.expert_new_text').children('div.qstn_reply').slideUp()
													.siblings('div.mc_question_block').toggleClass('noclick').children('div.answer').slideDown(function(){$('div#mc_questions').jScrollPane();}).children('span.answer_text').text(text);
								}
							}
					});
					
			return false;
		});
		
		
		
	
});