$(document).ready(function () {
	$(".info-block").infoBlock();

	setTextShort();

	$(document).on("click", ".js-query-text-block--short", function () {
		$(this).removeClass("query-item__text-block--short");
		$(this).find(".js-query-item__text").removeClass("query-item__text--short");
		$(this).find(".js-query-item__more").hide();
	});

	$(document).on("click", ".js-query-item__delete", function () {
		delete_offer_confirm($(this).data("id"));
	});

	$(document).on("click", ".js-want-toogle-desc", function () {
		$(this).closest(".card__content").find(".js-want-block-desc-full").toggleClass("hidden");
		$(this).closest(".card__content").find(".js-want-block-desc-short").toggleClass("hidden");
	});

	$(document).on("change", ".childs", function () {
		var params = {};

		if ($(this).val() === "subAll") {
			params["c"] = $(this).attr("data-catId");
		}
		else {
			params["c"] = $(this).val();
		}
		loadProjects(params);
	});

	$(document).on("change", "#parents", function () {
		var params = {};

		selectsState($(this).val());

		if ($(this).val() === "all") {
      params["a"] = 2;
			loadProjects(params);
			return true;
		}
		if ($(this).val() === "mine") {
			params["a"] = 1;
			loadProjects(params);
			return true;
		}

		params["c"] = $(this).val();

		loadProjects(params);
	});
});
window.onpopstate = function (event) {
	if (event.state) {
		showProjects(event.state.selector, event.state.content);
		selectsState(event.state.parentVal, event.state.childVal);
	}
};

function setTextShort() {
	$(".js-query-item__text").each(function () {

		var $el = $(this).clone(false);
		$el.css({visibility: "hidden", position: "absolute", "width": "auto"});
		$el.appendTo("body");
		var width = $el.width();
		$el.remove();

		if (width < 315) {
			$(this).closest(".js-query-text-block--short").removeClass("query-item__text-block--short")
			.find(".js-query-item__more").hide();
		}
	});
}

function selectsState(parentVal, childVal) {

	$("#parents").val(parentVal);
	var $activeTooltip = $(".js-is-active-tooltip");
	var $subCategoryContainer = $(".js-sub-cat-container");
	var $children = $(".childs");

	$(".sub_category").addClass("hidden");
	$('.cat_select').removeClass('has-tooltip');
	$activeTooltip.addClass("hidden");
	$subCategoryContainer.show();

	if (parentVal === "all") {
		$(".sub_empty").removeClass("hidden");
		return true;
	}
	if (parentVal === "mine") {
		$subCategoryContainer.hide();
		$('.cat_select').addClass('has-tooltip');
		$activeTooltip.removeClass("hidden");
		return true;
	}
	$children.addClass("hidden").next(".chosen-container").hide();
	$children.prop("selectedIndex", 0);
	$children.attr("name", "").removeAttr("required");
	var catSubSelector = ".sub-" + parentVal;
	if ($(catSubSelector).length > 0) {
		$(catSubSelector).removeClass("hidden");
	}

	if (childVal) {
		$(".childs:visible").val(childVal);
	}

}

function loadProjects(params) {
	var $container = $(".js-project-list");
	$container.preloader("show");
  params['ajax'] = 1;
	$.get("/projects", params, function (response) {
		var url = Utils.replaceQueryParam("c", params["c"], window.location.search);
		url = Utils.replaceQueryParam("a", params["a"], url);
		url = Utils.replaceQueryParam("page", "", url);
    url = window.location.origin + window.location.pathname + url;
		window.history.pushState({
			selector: ".js-project-list",
			content: response,
			parentVal: $("#parents").val(),
			childVal: $(".childs:visible").val()
		}, "", url);
		showProjects(".js-project-list", response);
	});
}

function showProjects(selector, html) {
	var $container = $(selector);

	if (typeof html == "undefined") {
		location.reload();
		return;
	}

	$container.html(html);
	setTextShort();
	initShortenedText();
}