/*
 *	Extended search panel
 *
 */

function autoSubmitSearchForm() {
	if ($("#search-filter-my-contacts").attr("checked"))
		$("#search-filter-my-contacts-value").val("0");
	else
		$("#search-filter-my-contacts-value").val("1");
	$("#ico-loading").show();
	var form = $("#extended-search-panel-form");
	$("#search-results-panel").load(
		form.attr("action") + "?recode=1&" + form.serialize() + " #search-results-panel",
		function() {
			initCustomInputs();
			$("#ico-loading").hide();
		}
	);
	$('.notice_blue').hide();
	contacts_selected = 0;
	contacts_price = 0;
	selected_count = 0;
	unselected_count = 0;
}

$(function() {
	// free text input
	$("#quick-search").each(function() {
		var search_field = $(this);
		search_field.attr("placeholder", $("#quick-search-placeholder").text())
			.placeholder();
		$("#extended-search-example").click(function() {
			search_field.val($(this).text());
			return false;
		});
	});
	// extended search slider
	$("#extended-search-button").click(function(){
		$(this).hide();
		$("#brief-search-button").show();
		$("#extended-search-panel").slideDown();
		$('.qtip').each(function(){
			   $(this).qtip('hide')
			});
		return false;
	});
	$("#brief-search-button").click(function(){
		$(this).hide();
		$("#extended-search-button").show();
		$("#extended-search-panel").slideUp();
		$('.qtip').each(function(){
			   $(this).qtip('hide')
			});
		return false;
	});
	// additional foi's
	$("#extended-search-fois-toggle").click(function() {
		$("#extended-search-fois-secondary").toggle(100);
		return false;
	});
	// group checkboxes
	$(".extended-search-check-all").click(function() {
		$(this).parents(".extended-search-selectable").find('input[type="checkbox"]').attr("checked", true).trigger("change");
		return false;
	});
	$(".extended-search-uncheck-all").click(function() {
		$(this).parents(".extended-search-selectable").find('input[type="checkbox"]').attr("checked", false).trigger("change");
		return false;
	});
	// autocomplete fields: geographic position, branches and positions
	function init_autocomplete_block(prefix, _name, _url, autoSubmit) {
		$("#extended-search-" + prefix + "-input").attr("autocomplete", "off").autocomplete({
			url: _url,
			sortResults: false,
			onItemSelect: function(item) {
				$("#extended-search-" + prefix + "-container").append('												\
					<div class="row margintop3 txt11 extended-search-' + prefix + '-item">							\
					<input type="hidden" name="' + _name + '[]" value="' + item.data[0] + '"/>						\
					<a class="darkblue_dottedb" href="#">' + item.value + '</a>										\
					<a class="extended-search-' + prefix + '-remove" href="#"><img style="margin-left: 5px;"		\
						src="templates/images/ico_delete.png" width="8" height="8" alt="" />						\
					</a><br/>																						\
					</div>																							\
				');
				$("#extended-search-" + prefix + "-input").val("");
				if (autoSubmit) autoSubmitSearchForm();
			}
		});
		$(".extended-search-" + prefix + "-remove").live("click", function() {
			$(this).parents(".extended-search-" + prefix + "-item").remove();
			if (autoSubmit) autoSubmitSearchForm();
			return false;
		});
		$("#extended-search-" + prefix + "-clear").click(function() {
			$("#extended-search-" + prefix + "-container").find(".extended-search-" + prefix + "-item").remove();
			if (autoSubmit) autoSubmitSearchForm();
			return false;
		});
	}
	init_autocomplete_block("geo", "city", "/index.php?mod=contacts&cmd=searchcities&with_regions=1&ajax=1");
	init_autocomplete_block("branch", "branch", "/index.php?mod=contacts&cmd=searchbranches&groups=1&ajax=1");
	init_autocomplete_block("position", "position", "/index.php?mod=contacts&cmd=searchpositions&ajax=1");
	init_autocomplete_block("company-groups", "company-groups", "/index.php?mod=contacts&cmd=searchcompanygroups&ajax=1");
	// expand all branches autocomplete field
	$("#extended-search-branch-button").click(function() {
		$("#extended-search-branch-input").data("autocompleter").fetchAll();
		return false;
	});
	// expand all positions autocomplete field
	$("#extended-search-position-button").click(function() {
		$("#extended-search-position-input").data("autocompleter").fetchAll();
		return false;
	});
	$("#extended-search-company-groups-button").click(function() {
		$("#extended-search-company-groups-input").data("autocompleter").fetchAll();
		return false;
	});
	//autocomplete company name field
	$("#_company_name").attr("autocomplete", "off").autocomplete({
		url: "/index.php?mod=contacts&cmd=searchcompanies&ajax=1",
		sortResults: false,
		onItemSelect: function(item) {
			// autocomplete company name
			$("#_company_name").val(item.data[0]);
			// foi
			$("select[name='_company_foi'] option[value='" + item.data[1] + "']")
				.attr("selected", "selected").trigger("change");
			// autocomplete company city
			$("#contact-city-input").val(item.data[2]);
			$("#contact-city-code").val(item.data[3]);
			// zip code
			$("#_zip").val(item.data[4]);
			// full address
			$("#_full_address").val(item.data[5]);
			// branch
			$("#add-contact-branch-container").html("");
			var branches = item.data[6].split("!");
			for (i in branches) {
				var branch_item = branches[i].split("=");
				$("#add-contact-branch-container").append('														\
					<div class="row margint5 txt11 add-contact-branch-item">									\
						<input name="_branch[]" value="' + branch_item[0] + '" type="hidden">					\
						<a class="darkblue_dottedb" href="#">' + branch_item[1] + '</a>							\
						<a class="add-contact-branch-remove" href="#">											\
							<img style="margin-left: 5px;" src="templates/images/ico_delete_red.png" alt="">	\
						</a>																					\
						<br/>																					\
					</div>																						\
				');
			}
			$("#_branch_value").val("").trigger("change");
			// employers
			$("#_cemployers" + item.data[7]).attr("checked", true).trigger("change");
			// phones
			var phones = item.data[8].split("!");
			$(".company-phone").val(phones.shift());
			for (i in phones) {
				var el = $(".company-phone").parents(".top-container").find(".contact-repeatable-element");
				el.parent().append(el.html());
				el.parent().find(".company-phone:last").attr("value", phones[i]);
			}
			// url
			$("#_company_www").val(item.data[9]);
			// email
			var emails = item.data[10].split("!");
			$(".company-email").val(emails.shift());
			for (i in emails) {
				var el = $(".company-email").parents(".top-container").find(".contact-repeatable-element");
				el.parent().append(el.html());
				el.parent().find(".company-email:last").attr("value", emails[i]);
			}
		}
	});
	// autocomplete branch field
	$("#_branch_value").attr("autocomplete", "off").autocomplete({
		url: "/index.php?mod=contacts&cmd=searchbranches&ajax=1",
		sortResults: false,
		onItemSelect: function(item) {
			$("#add-contact-branch-container").append('														\
				<div class="row margint5 txt11 add-contact-branch-item">									\
					<input name="_branch[]" value="' + item.data[0] + '" type="hidden">						\
					<a class="darkblue_dottedb" href="#">' + item.value + '</a>								\
					<a class="add-contact-branch-remove" href="#">											\
						<img style="margin-left: 5px;" src="templates/images/ico_delete_red.png" alt="">	\
					</a>																					\
					<br/>																					\
				</div>																						\
			');
			$("#_branch_value").val("");
		}
	});
	$(".add-contact-branch-remove").live("click", function() {
		$(this).parents(".add-contact-branch-item").remove();
		return false;
	});

	// expand all branches autocomplete field
	$("#branch-explore-button").click(function() {		
		if ($("#_branch_value").attr("disabled")!=true)		
			$("#_branch_value").data("autocompleter").fetchAll();
		return false;
	});
	// extended search form fixup before commiting
	$("#extended-search-form").submit(function() {
		var s = $("#quick-search");
		if (s.val() != s.attr("placeholder"))
			$(this).find('input[name="q"]').val(s.val());
		return true;
	});
	// auto group check boxes
	$(".extended-search-element").change(function() {
		var dest_id = $(this).attr("id").replace(/[0-9]/g, "");
		$("." + dest_id + "-checkbox").attr("checked", true).trigger("change");
	});
	// search right panel
	init_autocomplete_block("rgeo", "city", "/index.php?mod=contacts&cmd=searchcities&with_regions=1&ajax=1", true);
	init_autocomplete_block("rbranch", "branch", "/index.php?mod=contacts&cmd=searchbranches&groups=1&ajax=1", true);
	$("#search-filter-foi-toggle").click(function() {
		$("#search-filter-foi-other").toggle(100);
		return false;
	});
	$(".submit-button").click(function() {
		$(this).parents("form").submit();
		return false;
	});
	// auto submit form when any data changed
	$("#extended-search-panel-form input").change(autoSubmitSearchForm);
	// auto hide dropdown lists
	$('#body_page').mousedown(function(event) {
		$(".acResults").hide(); 			
	});
});

/*
 *	Search for 404 error page
 *
 */
$(function() {
	var path = location.pathname;
	var query = location.search;
	if (path == '/pages/contacts' || query == '?mod=page&id=contacts') {
		$("#extended-search-button").trigger("click");
	}	
});

