$(document).ready(function () {
	$('.header-search-input').bind('keydown', function (e) {
		if (e.keyCode == 13) {
		}
	});

	$(document).mouseup(function (e) {
		var container = $(".header-search-options");
		if (container.has(e.target).length === 0) {
			container.removeClass('visible');
		}
	});

	$('.header-search-input').bind('keyup', function (e) {
		if ($(this).val().length > 2) {
			$.ajax({
				type: "GET",
				url: "/search/autocomplete",
				data: {q: $(this).val()},
				dataType: 'html',
				success: function (data) {
					if (data) {
						$('.header-search-options').addClass('visible');
					} else {
						$('.header-search-options').removeClass('visible');
					}

					$('.header-search-options').html(data);
				}
			});
		}
	});
});

$(document).ready(function () {
	$(window).resize(function () {
		var footerHeight = $('footer').outerHeight();
		var stickFooterPush = $('.footer-buffer').height(footerHeight);

		$('#page').css({'marginBottom': '-' + footerHeight + 'px'});
	});

	$(window).resize();
});

$(document).ready(function () {
	$('.header-dropdown-menu').on('click', '.nav-tabs a', function () {
		$('.header-dropdown-menu .dropdown-menu').addClass('dontClose');
	})

	$('.header-dropdown-menu').on('hide.bs.dropdown', function (e) {
		if ($('.header-dropdown-menu .dropdown-menu').hasClass('dontClose')) {
			e.preventDefault();
		}

		$('.header-dropdown-menu .dropdown-menu').removeClass('dontClose');
	});
});



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();
});


$(document).ready(function () {
	initListeners();

	$(".scrollto").on("click", function () {
		$(document).scrollTo($(this).data('target'), 300);
	});


	$('#create-branch-form').on('shown.bs.collapse', function () {
		$(document).scrollTo($("#create-branch-form"), 300);
	})

	$('.collapse').on('shown.bs.collapse  hidden.bs.collapse', function () {
		$("a.collapse-control-button[href='#" + $(this).attr('id') + "']").each(function () {
			if ($(this).hasClass('collapsed') && $(this).data('show-more')) {
				$(this).text($(this).data('show-more'));
			} else {
				if ($(this).data('show-less')) {
					$(this).text($(this).data('show-less'));
				}
			}
		});
	});
});


function initListeners() {
	$('.input-mask').on('change', function () {
		target_input = $(this).data('target-input');
		$(target_input).val($(this).val());
	});
}


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();
		}
	})
}


$(document).ready(function () {

	var maskList = $.masksSort($.masksLoad("/vendor/inputmask-multi/data/phone-codes-fixed.json"), ['#'], /[0-9]|#/, "mask");
	var maskOpts = {
		inputmask: {
			definitions: {
				'#': {
					validator: "[0-9]",
					cardinality: 1
				}
			},
			clearIncomplete: true,
			showMaskOnHover: false,
			//autoUnmask: true
		},
		match: /[0-9]/,
		replace: '#',
		list: maskList,
		listKey: "mask",
		// onMaskChange: function (maskObj, completed) {
		// 	$(this).attr("placeholder", $(this).inputmask("getemptymask"));
		// }
	};

	//$('#formcompany-phone').inputmasks(maskOpts);
	$('.js-phonemask').inputmasks(maskOpts);
});