$(function(){
	$('.js-structure-item[href="#"]').click(function() {
		var container = $(this).find('.js-structure-item-name'),
			csid = container.val();

		$.ajax({
			type: 'get',
			url: '/sites/all/modules/court/court_structure/ajax/ajax_get_descr.php',
			data: {
				csid: csid
			},
			dataType: 'json',
			success: function(data) {
				if (data) {
					$('.js-structure, .js-structure-description').toggleClass('g-hidden');
					$('.js-structure-description-title').text(data.title || '');
					
					var structureDescLink = $('.js-structure-description-link'),
						structureDescText = $('.js-structure-description-text'),
						structureDescTextDefault = structureDescText.attr('rel') || '',
						structureDescHead = $('.js-structure-description-list_head_title, .js-structure-description-list_head'),
						structureDescSubhead = $('.js-structure-description-list_subhead_title, .js-structure-description-list_subhead'),
						structureDescStaff = $('.js-structure-description-list_staff_title, .js-structure-description-list_staff');
					
					if (data.contact) {
						structureDescLink.attr('href', data.contact);
					}
					structureDescLink.toggleClass('g-hidden', !data.contact);
					data.descr = data.descr || structureDescTextDefault;
					structureDescText.html(data.descr);
					structureDescText.toggleClass('g-hidden', !data.descr);
					if (data.staff) {
						structureDescHead.toggleClass('g-hidden', !data.staff.head);
						if (data.staff.head) {
							$('.js-structure-description-list_head_title').text(data.staff.head.title);
							if (data.staff.head.list) {
								var headList = $('.js-structure-description-list_head');
								headList.html('');
								for (index in data.staff.head.list) {
									headList.append('<li><a target="_blank" href="/' + data.staff.head.list[index].link + '">'
													+ data.staff.head.list[index].fio + '</a></li>');
								}
								if (!data.staff.head.list.length) {
									structureDescHead.addClass('g-hidden');
								}
							} else {
								structureDescHead.addClass('g-hidden');
							}
						}
						structureDescSubhead.toggleClass('g-hidden', !data.staff.subhead);
						if (data.staff.subhead) {
							$('.js-structure-description-list_subhead_title').text(data.staff.subhead.title);
							if (data.staff.head.list) {
								var subheadList = $('.js-structure-description-list_subhead');
								subheadList.html('');
								for (index in data.staff.subhead.list) {
									subheadList.append('<li><a target="_blank" href="/' + data.staff.subhead.list[index].link + '">'
													+ data.staff.subhead.list[index].fio + '</a></li>');
								}
								if (!data.staff.subhead.list) {
									structureDescSubhead.addClass('g-hidden');
								}
							} else {
								structureDescSubhead.addClass('g-hidden');
							}
						}
						structureDescStaff.toggleClass('g-hidden', !data.staff.staff);
						if (data.staff.staff) {
							$('.js-structure-description-list_staff_title').text(data.staff.staff.title);
							if (data.staff.staff.list) {
								var staffList = $('.js-structure-description-list_staff');
								staffList.html('');
								for (index in data.staff.staff.list) {
									staffList.append('<li><a target="_blank" href="/' + data.staff.staff.list[index].link + '">'
													+ data.staff.staff.list[index].fio + '</a></li>');
								}
								if (!data.staff.staff.list.length) {
									structureDescStaff.addClass('g-hidden');
								}
							} else {
								structureDescStaff.addClass('g-hidden');
							}
						}
					}
					// Прокручиваем страницу в самый верх
					window.scroll(0, 0);
				} else {
					$('.js-structure, .js-structure-description').toggleClass('g-hidden');
					$('.js-structure-description-conten').html('Данные отсутствуют');
				}
			},
			error: function() {
				$('.js-structure, .js-structure-description').toggleClass('g-hidden');
				$('.js-structure-description-conten').html('Произошла ошибка. Попробуйте повторить свои действия чуть позже.');
			}
		});
	});
	
	$('.js-structure-link--back').click(function() {
		$('.js-structure, .js-structure-description').toggleClass('g-hidden');
		return false;
	});
});