function checkAll(className, flag) {	//zaskrtne / odskrtne vsechny mestske casti
	$('.'+className).attr('checked',flag);
	$.uniform.update('.'+className);
}

function isCheckedAll(className) {
	if (!$('input.'+className+'[type=checkbox]:not(:checked)').length) return true;
	return false;
}



$(document).ready(function(){
	$("#mcast_all").click(function() {
		checkAll('mcast_checkbox',this.checked);
	});
	$("#locality_okres_all").click(function() {
		checkAll('locality_okres_checkbox',this.checked);
	});
	$("#advert_subtype_all").click(function() {
		checkAll('advert_subtype_checkbox',this.checked);
	});
	$("#building_type_all").click(function() {
		checkAll('building_type_checkbox',this.checked);
	});
	$("#ownership_all").click(function() {
		checkAll('ownership_checkbox',this.checked);
	});
	$("#beds_all").click(function() {
		checkAll('beds_checkbox',this.checked);
	});
	$("#usable_area_all").click(function() {
		checkAll('usable_area_checkbox',this.checked);
	});
	$("#estate_area_all").click(function() {
		checkAll('usable_area_checkbox',this.checked);
	});
	$("#floor_area_all").click(function() {
		checkAll('floor_area_checkbox',this.checked);
	});

	$(".mcast_checkbox").click(function() {
		if (isCheckedAll('.mcast_checkbox')) {
			$("#mcast_all").attr('checked',true);
			$.uniform.update('#mcast_all');
		} else {
			$("#mcast_all").attr('checked',false);
			$.uniform.update('#mcast_all');
		}
	});

	$(".locality_okres_checkbox").click(function() {
		if (isCheckedAll('.locality_okres_checkbox')) {
			$("#locality_okres_all").attr('checked',true);
			$.uniform.update('#locality_okres_all');
			alert('all');
		} else {
			$("#locality_okres_all").attr('checked',false);
			$.uniform.update('#locality_okres_all');
		}
	});
	$(".advert_subtype_checkbox").click(function() {
		if (isCheckedAll('.advert_subtype_checkbox')) {
			$("#advert_subtype_all").attr('checked',true);
			$.uniform.update('#advert_subtype_all');
		} else {
			$("#advert_subtype_all").attr('checked',false);
			$.uniform.update('#advert_subtype_all');
		}
	});
	$(".building_type_checkbox").click(function() {
		if (isCheckedAll('.building_type_checkbox')) {
			$("#building_type_all").attr('checked',true);
			$.uniform.update('#building_type_all');
		} else {
			$("#building_type_all").attr('checked',false);
			$.uniform.update('#building_type_all');
		}
	});
	$(".ownership_checkbox").click(function() {
		if (isCheckedAll('.ownership_checkbox')) {
			$("#ownership_all").attr('checked',true);
			$.uniform.update('#ownership_all');
		} else {
			$("#ownership_all").attr('checked',false);
			$.uniform.update('#ownership_all');
		}
	});
	$(".beds_checkbox").click(function() {
		if (isCheckedAll('beds_checkbox')) {
			$("#beds_all").attr('checked',true);
			$.uniform.update('#beds_all');
		} else {
			$("#beds_all").attr('checked',false);
			$.uniform.update('#beds_all');
		}
	});
	$(".usable_area_checkbox").click(function() {
		if (isCheckedAll('.usable_area_checkbox')) {
			$("#usable_area_all").attr('checked',true);
			$.uniform.update('#usable_area_all');
		} else {
			$("#usable_area_all").attr('checked',false);
			$.uniform.update('#usable_area_all');
		}
	});
	$(".estate_area_checkbox").click(function() {
		if (isCheckedAll('.estate_area_checkbox')) {
			$("#estate_area_all").attr('checked',true);
			$.uniform.update('#estate_area_all');
		} else {
			$("#estate_area_all").attr('checked',false);
			$.uniform.update('#estate_area_all');
		}
	});
	$(".floor_area_checkbox").click(function() {
		if (isCheckedAll('.floor_area_checkbox')) {
			$("#floor_area_all").attr('checked',true);
			$.uniform.update('#floor_area_all');
		} else {
			$("#floor_area_all").attr('checked',false);
			$.uniform.update('#floor_area_all');
		}
	});



	$("select#id_object_type").change(function(){
		clear_form_elements('.druh_nemovitosti_dependent');
		set_form_elements(".id_object_type_"+$("#id_object_type").val());
		$.uniform.update(".id_object_type_"+$("#id_object_type").val());
		var id_object_type = $("select#id_object_type").val();
		if(id_object_type != 0) {
			if ($("select#id_object_type").val() == 1) {
				$('#loading').show();
				$.getJSON("/res/ajax_results.php",{ id_object_type: $(this).val(), mode: 'get_nabidka_properties', prop_name: 'advert_subtype' }, function(j){
					$(".druh_nemovitosti_dependent").hide();
//					$.uniform.update('.ownership_checkbox');
					$(".id_object_type_"+$("#id_object_type").val()).show();
					var options = '';

					for (var i = 0; i < j.length; i++) {
						if (id_object_type == 1 && i == 1) {
							options += '<span><label></label></span>';
						}
						if(i % 2 == 0) {
							options += '<span style="width:100px;"><label><input type="checkbox" checked="checked" class="advert_subtype_checkbox" value="' + j[i].id_enumeration + '" name="advert_subtype[]"/>' + j[i].display_value + '</label></span>';
						} else {
							options += '<span style="width:120px;"><label><input type="checkbox" checked="checked" class="advert_subtype_checkbox" value="' + j[i].id_enumeration + '" name="advert_subtype[]"/>' + j[i].display_value + '</label></span>';
						}
					}
					options += '<span style="width: 120px;">&nbsp;</span><span style="width: 90px;"><label><input type="checkbox" checked="checked" id="advert_subtype_all" onclick="checkAll(\'advert_subtype_checkbox\',this.checked)" value="vse" name="advert_subtype[]"/>Vybrat vše</label></span>';
					$("#advert_sub").html(options);
					$("#advert_sub input:checkbox").uniform();
					$('#advert_sub').show();
					$('#loading').hide();
					$(".advert_subtype_checkbox").click(function() {
						if (isCheckedAll('.advert_subtype_checkbox')) {
							$("#advert_subtype_all").attr('checked',true);
							$.uniform.update('#advert_subtype_all');
						} else {
							$("#advert_subtype_all").attr('checked',false);
							$.uniform.update('#advert_subtype_all');
						}
					});
				})
			} else if($("select#id_object_type").val() == 4) {
				$('#loading').show();
				$.getJSON("/res/ajax_results.php",{ id_object_type: $(this).val(), mode: 'get_nabidka_properties', prop_name: 'advert_subtype' }, function(j){
					$(".druh_nemovitosti_dependent").hide();
					$(".id_object_type_"+$("#id_object_type").val()).show();
					var options = '<h3>Podtyp nemovitosti</h3><select name="advert_subtype"><option value="">vše</option>';
					for (var i = 0; i < j.length; i++) {
						options += '<option value="' + j[i].id_enumeration + '">' + j[i].display_value + '</option>';
					}
					options += '</select>';
					$("#advert_sub").html(options);
					$("#advert_sub select").uniform();
					$('#advert_sub').show();
					$('#loading').hide();
					$("#advert_sub select").change(function() {
						$('#beds_block').hide();
						if($(this).val() == 316) {
							$('#beds_block').show();
						} else {
							clear_form_elements('#beds_block');
						}
					});
				})
			} else {
				$('#loading').show();
				$.getJSON("/res/ajax_results.php",{ id_object_type: $(this).val(), mode: 'get_nabidka_properties', prop_name: 'advert_subtype' }, function(j){
					$(".druh_nemovitosti_dependent").hide();
					$(".id_object_type_"+$("#id_object_type").val()).show();
					var options = '';

					for (var i = 0; i < j.length; i++) {
						if (i == j.length - 1) {
							options += '<span style="width: 120px"><label><input type="checkbox" checked="checked" class="advert_subtype_checkbox" id="advert_subtype" value="' + j[i].id_enumeration + '" name="advert_subtype[]"/>' + j[i].display_value + '</label></span>';
						} else {
							options += '<span><label><input type="checkbox" checked="checked" class="advert_subtype_checkbox" id="advert_subtype" value="' + j[i].id_enumeration + '" name="advert_subtype[]"/>' + j[i].display_value + '</label></span>';
						}

					}
					options += '<span style="width: 90px;"><label><input type="checkbox" checked="checked" id="advert_subtype_all" onclick="checkAll(\'advert_subtype_checkbox\',this.checked)" value="vse" name="advert_subtype[]"/>Vybrat vše</label></span>';
					$("#advert_sub").html(options);
					$("#advert_sub input:checkbox").uniform();
					$('#advert_sub').show();
					$('#loading').hide();
					$(".advert_subtype_checkbox").click(function() {
						if (isCheckedAll('.advert_subtype_checkbox')) {
							$("#advert_subtype_all").attr('checked',true);
							$.uniform.update('#advert_subtype_all');
						} else {
							$("#advert_subtype_all").attr('checked',false);
							$.uniform.update('#advert_subtype_all');
						}
					});
				})
			}
		} else {
			$('#advert_subtype').hide();
		}
	});


	$("#advert_sub select").change(function() {
		$('#beds_block').hide();
		if($(this).val() == 316) {
			$('#beds_block').show();
		} else {
			clear_form_elements('#beds_block');
		}
	});

	$("select#kraj").change(function(){
		$('#loading').show();
		$('#locality_city').val('');
		$('#locality_city_uir').val('');
		$('#locality_street').val('');
		$('#locality_street').attr('disabled','disabled');
		if ($("select#kraj").val() == 19) {	// praha - misto okresu mcasti
			$.getJSON("/res/ajax_results.php",{
				kraj_kod: $(this).val(),
				mode: 'uir_select',
				sub_mode: 'get_mcast_by_obec_kod'
			}, function(j){
				var options = '';
				for (var i = 0; i < j.length; i++) {
					if(i % 2 == 0) {
						options += '<span style="width:120px;"><label><input class="mcast_checkbox" type="checkbox" checked="checked" id="okres_kod" value="' + j[i].id_enumeration + '" name="locality_mcast[]"/>' + j[i].id_enumeration + '</label></span>';
					} else {
						options += '<span style="width:90px;"><label><input class="mcast_checkbox" type="checkbox" checked="checked" id="okres_kod" value="' + j[i].id_enumeration + '" name="locality_mcast[]"/>' + j[i].id_enumeration + '</label></span>';
					}
				}
				options += '<span style="width: 120px;">&nbsp;</span><span style="width: 90px;"><label><input type="checkbox" checked="checked" id="mcast_all" onclick="checkAll(\'mcast_checkbox\',this.checked)" value="vse" name="locality_mcast[]"/>Vybrat vše</label></span>';
				$("#okres_checkboxes").html(options);
				$("#okres_checkboxes input:checkbox").uniform();
				$('#loading').hide();
				$('#okres_mcast_label').text('Vyberte městskou část');
				$('#okres_mcast_label').show();
				$('#filter_mcast_block').show();
				$('#filter_okres_block').show();
				$('#filter_street_block').show();
				$(".mcast_checkbox").click(function() {
					if (isCheckedAll('.mcast_checkbox')) {
						$("#mcast_all").attr('checked',true);
						$.uniform.update('#mcast_all');
					} else {
						$("#mcast_all").attr('checked',false);
						$.uniform.update('#mcast_all');
					}
				});
			});
			$('.locality_city_block').hide();
			$('#locality_city_uir').val('554782');
		} else if ($("select#kraj").val() == 0) {	// vse
			$('#filter_okres_block').hide();
			$('#filter_city_block').hide();
			$('#filter_mcast_block').hide();
			$('#filter_street_block').hide();
		} else {
			$.getJSON("/res/ajax_results.php",{
				kraj_kod: $(this).val(),
				mode: 'uir_select',
				sub_mode: 'okresy_by_kraj'
			}, function(j){
				var options = '';
				for (var i = 0; i < j.length; i++) {
					$('.locality_city_block').show();
					if(i == j.length - 1) {
						options += '<span style="width: 120px;"><label><input class="mcast_checkbox" type="checkbox" checked="checked" id="okres_kod" value="' + j[i].display_value + '" name="locality_okres[]"/>' + j[i].display_value + '</label></span>';
					} else {
						options += '<span><label><input class="mcast_checkbox" type="checkbox" checked="checked" id="okres_kod" value="' + j[i].display_value + '" name="locality_okres[]"/>' + j[i].display_value + '</label></span>';
					}
				}
				options += '<span style="width: 90px;"><label><input type="checkbox" checked="checked" id="mcast_all" onclick="checkAll(\'mcast_checkbox\',this.checked)" value="vse" name="locality_mcast[]"/>Vybrat vše</label></span>';
				$("#okres_checkboxes").html(options);
				$("#okres_checkboxes input:checkbox").uniform();
				$('#loading').hide();
				$('#okres_mcast_label').text('Vyberte okres');
				$('#okres_mcast_label').show();
				$('#filter_okres_block').show();
				$('#filter_city_block').show();
				$('#filter_street_block').show();
				$(".mcast_checkbox").click(function() {
					if (isCheckedAll('.mcast_checkbox')) {
						$("#mcast_all").attr('checked',true);
						$.uniform.update('#mcast_all');
					} else {
						$("#mcast_all").attr('checked',false);
						$.uniform.update('#mcast_all');
					}
				});
			});
		}
		$('#locality_street').removeAttr('disabled');
	})

	var id_object_type = $("#id_object_type").val();
	$(".druh_nemovitosti_dependent").hide();
	$(".id_object_type_"+id_object_type).show();

	$("input#locality_city").autocomplete({
		source: function(request, response) {
			$('#loading').show();
			$.ajax({
				url: "/res/ajax_results.php?mode=hp_autocomplete",
				dataType: "json",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 12,
					name_startsWith: request.term,
					radio: "mesto"
				},
				success: function(data) {
					$('#loading').hide();
					response($.map(data, function(item) {
						return {
							label: item.nazev,
							value: item.obec,
							kod: item.obec_kod
						}
					}))
				}
			})
		},
		minLength: 2,
		select: function(event, ui) {
			$('#locality_street').removeAttr('disabled');
			$("#locality_city_uir").val(ui.item.obec_kod);
			$("#locality_street").val('');
		},
		open: function() {
			$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
		},
		close: function() {
			$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
		}
	});

	$("input#locality_street").autocomplete({
		source: function(request, response) {
			$('#loading').show();
			$.ajax({
				url: "/res/ajax_results.php?mode=uir_select",
				dataType: "json",
				data: {
					sub_mode: "ulice_by_obec_kod",
					featureClass: "P",
					style: "full",
					maxRows: 12,
					name_startsWith: request.term,
					obec_kod: $("#locality_city_uir").val()
				},
				success: function(data) {
					$('#loading').hide();
					response($.map(data, function(item) {
						return {
							label: item.ulice_nazev,
							value: item.ulice_nazev,
							kod: item.obec_kod
						}
					}))
				}
			})
		},
		minLength: 2,
		select: function(event, ui) {
		},
		open: function() {
			$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
		},
		close: function() {
			$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
		}
	});



});

