$(document).ready(function () {
	//$(".fancybox").fancybox();

	updateRatingStars();



	$('.header-search-input').bind('keydown', function (e) {
		if (e.keyCode == 13) {
		}
	});
	$('.header-search-input').autocomplete({
		delay: 100,
		minLength: 3,
		source: function (request, response) {
			$.ajax({
				url: '/search/autocomplete?q=' + encodeURIComponent(request.term),
				dataType: 'json',
				success: function (json) {
					response($.map(json, function (item) {
						return {
//							label: item.title_formated,
//							label_raw: item.title,
							label: item.title,
							href: item.href
						}
					}));
				}
			});
		},
		select: function (event, ui) {
			$('.header-search-input').val(ui.item.label_raw)
			location = ui.item.href;
			return false;
		},
		focus: function (event, ui) {
			return false;
		},
	}).autocomplete("instance")._renderItem = function (ul, item) {
		var re = new RegExp("(" + this.term + ")", "gi"),
			template = "<span class='ui-state-highlight'>$1</span>",
			label = item.label.replace(re, template),
			$li = $("<li/>").appendTo(ul);


		$("<span/>").html(label).appendTo($li);

		return $li;
	};
});
$(document).ready(function () {
	$(".collapsible .collapsible-control a.collapse-button").on("click", function () {
		collapse_block = $(this).parent().parent();
		collapse_block.toggleClass('collapsed');
		$(this).hide();
		collapse_block.find('.fog').hide();
		return false;
	});
});


function initInputRating() {
	$(".review-rating-stars").each(function () {
		$(this).raty({
			targetScore: $(this).data('target'),
			number: 5,
			halfShow: false,
			score: $($(this).data('target')).val(),
			dependenceOnScoreFolder: true,
			path: '/vendor/raty/lib/images'
		});
	});
}


function updateRatingStars() {
	$(".rating-autostars").each(function () {
		$(this).raty({
			readOnly: true,
			number: 5,
			score: $(this).data('rating'),
			path: '/vendor/raty/lib/images'
		});
	});
}


function initListeners() {

}

function reviewSetStatus(review_id, status, reload_pjax) {
	$.post("/ajax-company/review-set-status", {review_id: review_id, status: status}, function () {
		if (reload_pjax) {
			$.pjax.reload(reload_pjax);
			initListeners();
		}
	})
}

function reviewAnswerSetStatus(review_id, status, reload_pjax) {
	$.post("/ajax-company/review-answer-set-status", {review_id: review_id, status: status}, function () {
		if (reload_pjax) {
			$.pjax.reload(reload_pjax);
			initListeners();
		}
	})
}


ajax_action_listener_start = function () {
	$(this).children(".fog").show();
};

ajax_action_listener_end = function () {
	$(this).children(".fog").hide();
	//initAjaxActionListeners();
};

function initAjaxActionListeners(container) {
	if (!container)
		container = "body";


	$(container).find('.ajax-fog').css("position", "relative");
	if ($(container).find('.ajax-fog>.fog').length == 0) {
		$(container).find('.ajax-fog').append("<div class='fog' style='z-index: 100; display: none; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: url(\"/img/loading.gif\") center center no-repeat rgba(0,0,0,0.05); background-size: 100px 100px;'></div>");
	}

	$(container).find('.ajax-fog')
		.off('pjax:start', ajax_action_listener_start).on('pjax:start', ajax_action_listener_start)
		.off('pjax:end', ajax_action_listener_end).on('pjax:end', ajax_action_listener_end);
}


$(document).ready(function () {
	initAjaxActionListeners();
});