$(document).ready(function() {
	// Схлопывающиеся блоки
	$('a.toggle-link').click(function() {		$("#" + $(this).attr('rel') ).slideToggle();
		return false;	});


	///////////////////////

	function hidemap() {
		$("#first, #second, #third").css("left",0);

		$("#tabs").css("visibility","visible");

		$("#flashouter").fadeOut();

		$("ul a.selected").click();
	};

	$("a.onmap").click(function(e) {
				$("#first, #second, #third").css("left",'1000px');

				$("#tabs").css("visibility","hidden");

				$("#flashouter").fadeIn();
			});

	$("#layer, div.close").click(function() {
				hidemap();
			});
	$("#locations").attr("disabled", "true");

	$("input.submit_button").click(function() {
				var attrrel = $(this).attr("rel");

				var whereCities = '#test1';

				if (attrrel == 'filtericities2') {
					whereCities = '#test2';
				}
				if (attrrel == 'filtericities3') {
					whereCities = '#test3';
				}
				var citiesstr = $(whereCities).val();
				var idsarray = citiesstr.split(',')
				var idsarraytrim = $.map(idsarray, function(value) {
							return $.trim(value);
						});

				compareIds(attrrel, idsarraytrim);
			});
	if ($("#locations1").size() == 1) {
		$("body")
				.append("<select style=\"display:none\" id='regionslocations'></select>");
		$("#regionslocations").html($("#locations1 option"));
	}

	if ($("#locations10").size() == 1) {
		$("body")
				.append("<select style=\"display:none\" id='regionslocations'></select>");
		$("#regionslocations").html($("#locations10 option"));
	}

	if ($("#locations10").size() == 1) {
		$("body")
				.append("<select style=\"display:none\" id='locationscities'></select>");
		$("#locationscities").html($("#cities10 option"));
	}

	function makeSublist1(isSubselectOptional) {
		$("#first #locations1").attr("disabled", "true");
		$("#first #locations1").change(function() {
					var globallocation = $(this).val();
					$("#posted_location").val(globallocation);
					$("#first #test1").val(" ").flushCache();
				});
		$("#first #regions1").change(function() {
			var globalregion = $(this).val();
			$("#posted_region").val(globalregion);
			$("#test1").val(" ").flushCache();
			var parentValue = $("#first #regions1").attr("value");

			if (parentValue == 0)
				$("#first #locations1").attr("disabled", "true");
			$("#first #locations1 option[value='0']").attr("selected", "true");
			if (parentValue != 0)
				$("#first #locations1").attr("disabled", 0)
						.html($("#regionslocations .sub_" + parentValue)
								.clone());
			if (isSubselectOptional)
				$("#first #locations1")
						.prepend("<option value=\"0\" selected=\"true\">...</option>");
		});
	};

	function makeSublist2(isSubselectOptional) {
		$("#second #locations2").attr("disabled", "true");
		$("#second #locations2").change(function() {
					var globallocation = $(this).val();
					$("#posted_location").val(globallocation);
					$("#second #test2").val(" ").flushCache();
				});
		$("#second #regions2").change(function() {
			var globalregion = $(this).val();
			$("#posted_region").val(globalregion);
			$("#test2").val(" ").flushCache();
			var parentValue = $("#second #regions2").attr("value");

			if (parentValue == 0)
				$("#second #locations2").attr("disabled", "true");
			$("#second #locations2 option[value='0']")
					.attr("selected", "true");
			if (parentValue != 0)
				$("#second #locations2").attr("disabled", 0)
						.html($("#regionslocations .sub_" + parentValue)
								.clone());
			if (isSubselectOptional)
				$("#second #locations2")
						.prepend("<option value=\"0\" selected=\"true\">...</option>");
		});
	};


	function makeSublist3(isSubselectOptional) {
		$("#third #locations3").attr("disabled", "true");
		$("#third #locations3").change(function() {
					var globallocation = $(this).val();
					$("#posted_location").val(globallocation);
					$("#test3").val(" ").flushCache();
				});
		$("#third #regions3").change(function() {
			var globalregion = $(this).val();
			$("#posted_region").val(globalregion);
			$("#third #test3").val(" ").flushCache();
			var parentValue = $("#third #regions3").attr("value");

			if (parentValue == 0)
				$("#third #locations3").attr("disabled", "true");
			$("#third #locations3 option[value='0']").attr("selected", "true");
			if (parentValue != 0)
				$("#third #locations3").attr("disabled", 0)
						.html($("#regionslocations .sub_" + parentValue)
								.clone());
			if (isSubselectOptional)
				$("#third #locations3")
						.prepend("<option value=\"0\" selected=\"true\">...</option>");
		});
	};

	function makeSublistObject(isSubselectOptional) {
		$("#locations10").attr("disabled", "true");
		$("#regions10").change(function() {
			var parentValue = $("#regions10").attr("value");

			$("#locations10 option[value='0']").attr("selected", "true");

			if (parentValue == 0) {
				$("#locations10").attr("disabled", "true");
				$("#cities10 option[value='0']").attr("selected", "true");
				$("#cities10").attr("disabled", "true");
			}

			if (parentValue != 0) {
				$("#locations10").attr("disabled", 0)
						.html($("#regionslocations .sub_" + parentValue)
								.clone());
			}

			if (isSubselectOptional) {
				$("#locations10")
						.prepend("<option value=\"0\" selected=\"true\">...</option>");
			}

		});
	};

	function makeSublistCities(isSubselectOptional) {
		$("#cities10").attr("disabled", "true");
		$("#locations10").change(function() {
			var parentValue = $("#locations10").attr("value");

			if (parentValue == 0) {
				$("#cities10").attr("disabled", "true");
			}
			$("#cities10 option[value='0']").attr("selected", "true");
			if (parentValue != 0)
				$("#cities10")
						.attr("disabled", 0)
						.html($("#locationscities .sub_" + parentValue).clone());
			if (isSubselectOptional)
				$("#cities10")
						.prepend("<option value=\"0\" selected=\"true\">...</option>");
		});
	};

	makeSublist1(true);
	makeSublist2(true);
	makeSublist3(true);
	makeSublistObject(true);
	makeSublistCities(true);

	function savedObject() {
		var cityVal = $("#object_name").attr("city");
		var locationVal = $("#object_name").attr("location");
		var regionVal = $("#object_name").attr("region");

		if (regionVal != 0) {
			$("#regions10 option[value ='" + regionVal + "']").attr("selected",
					"true");
		}
		if (locationVal != 0) {
			$("#locations10").removeAttr("disabled")
					.html($("#regionslocations .sub_" + regionVal).clone());
			$("#locations10 option[value ='" + locationVal + "']")
					.removeAttr("disabled").attr("selected", "true");
		}
		if (cityVal != 0) {
			$("#cities10").removeAttr("disabled")
					.html($("#locationscities .sub_" + locationVal).clone());
			$("#cities10 option[value ='" + cityVal + "']").attr("selected",
					"true");
		}
	};

	savedObject();

	function savedFilter() {
		var locationVal = $("#posted_location").val();
		var regionVal = $("#posted_region").val();

		if (regionVal != 0) {
			$("#regions1 option[value ='" + regionVal + "']").attr("selected",
					"true");
		}
		if (locationVal != 0) {
			$("#locations1").removeAttr("disabled")
					.html($("#regionslocations .sub_" + regionVal).clone());
			$("#locations1 option[value ='" + locationVal + "']")
					.removeAttr("disabled").attr("selected", "true");
		}
	};
	savedFilter();
	function compareIds(buttonrel, iitems) {

		var hidSel = document.getElementById('selectedcities');
		var hidStr = document.getElementById(buttonrel);
		var itemsCnt = iitems.length;
		var selCnt = hidSel.options.length;

		var i = 0;
		var j = 0;

		for (i = 0; i < itemsCnt; i++) {
			for (j = 0; j < selCnt; j++) {
				if (iitems[i] == hidSel.options[j].text) {
					hidStr.value += (hidSel.options[j].value + ';');
				}
			}
		}

		return true;
	};

	function findValueCallback(event, data, formatted) {
		$("#selectedcities").append("<option value=\"" + data[1] + "\">"
				+ data[0] + "</option>");
	}

	function formatItem(row) {
		return "<span style=\"color: #000000;\">" + row[0] + "</span>"
				+ "&nbsp;&nbsp;&nbsp;<small>" + row[2] + "</small>";
	}

	$("#test1").autocomplete('/autotest', {
				width : 300,
				multiple : true,
				matchContains : false,
				mustMatch : true,
				max : 100,
				cacheLength : 100,
				delay : 100,
				formatItem : formatItem,
				extraParams : {
					reg : function() {
						return $("#regions1").val();
					},
					loc : function() {
						return $("#locations1").val();
					}
				}
			});
	$("#test2").autocomplete('/autotest', {
				width : 300,
				multiple : true,
				matchContains : false,
				mustMatch : true,
				max : 100,
				cacheLength : 100,
				delay : 100,
				formatItem : formatItem,
				extraParams : {
					reg : function() {
						return $("#regions2").val();
					},
					loc : function() {
						return $("#locations2").val();
					}
				}
			});
	$("#test3").autocomplete('/autotest', {
				width : 300,
				multiple : true,
				matchContains : false,
				mustMatch : true,
				max : 100,
				cacheLength : 100,
				delay : 100,
				formatItem : formatItem,
				extraParams : {
					reg : function() {
						return $("#regions3").val();
					},
					loc : function() {
						return $("#locations3").val();
					}
				}
			});

	$("#test1").result(findValueCallback);
	$("#test2").result(findValueCallback);
	$("#test3").result(findValueCallback);

});
