(function($){
  // очищаем select
  $.fn.clearSelect = function() {
	  return this.each(function(){
		  if(this.tagName=='SELECT') {
		      this.options.length = 0;
		      $(this).attr('disabled','disabled');
		  }
	  });
  }
  // заполняем select
  $.fn.fillSelect = function(dataArray) {
	  return this.clearSelect().each(function(){
		  if(this.tagName=='SELECT') {
			  var currentSelect = this;
			  $.each(dataArray,function(index,data){
				  var option = new Option(data.text, data.value, data.selected);
				  if($.support.cssFloat) {
					  currentSelect.add(option,null);
				  } else {
					  currentSelect.add(option);
				  }
			  });
		  }
	  });
  }
})(jQuery);

function CheckSearch()
{
	$.ajax({
	async:false,
	type:'GET',
	data:{action:'search',check:'true',
	act_type:$("input[name='act_type']:checked").val(),
	period_lease:$("select[name='period_lease']").val(),
	bild_type:$("input[name='bild_type']:checked").val(),
	bild_this:$("input[name='bild_this']:checked").val(),
	square_dom:$("input[name='square_dom']").val(),
	square_uch:$("input[name='square_uch']").val(),
	amount_room:$("input[name='amount_room']").val(),
	amount_floor:$("input[name='amount_floor']").val(),
	floor:$("input[name='floor']").val(),
	building_type:$("input[name='building_type']").val(),
	amount_bath:$("input[name='amount_bath']").val(),
	design:$("input[name='design']:checked").val(),
	building_ready:$("input[name='building_ready']:checked").val(),
	metro:$("input[name='metro'] option:selected").val(),
	country:$('#country').val(),
	region:$('#region').val(),
	city:$('#city').val(),
	price_start:$("input[name='price_start']").val(),
	price_end:$("input[name='price_end']").val()
	},
	url:'/ajax.php',
	dataType:'json',
	success:function(data){
				if(data[0].text != "")
				{
					alert(data[0].text);
					//alert(data[0].text);
					//$('#err_act_type').text(data[0].text);
					$('#err_act_type').addClass("unchecked");
					return false;
				}
				else
				{
					//$('#err_act_type').text("");
					$('#err_act_type').removeClass("unchecked");
					return true;
				}
			}
	});
	return true;
}	

$(document).ready(function(){

  $('#period_lease').hide();

  $('input[type=radio][name="act_type"]').click(
    function() {
      if ($('input[type=radio][name="act_type"][value="2"]').attr('checked')) {
        $('#period_lease').show();
        $('#hid9').hide();
        $('#hid12').hide();
      } else {
        $('#period_lease').hide();
        $('#hid9').show();
        $('#hid12').show();
      }
    }
  );

  function returnSelValue(data) {
    for (var el in data) if (data[el].selected === 'selected') return region_id = data[el].value;
    return false;
  }
  
  // выбор региона
  function adjustRegion(){
  	var countryValue = $('#country').val();
  	var tmpSelect = $('#region');
  	if(countryValue.length == 0) {
  		tmpSelect.attr('disabled','disabled');
  		tmpSelect.clearSelect();
  		adjustCity();
  	} else {
  		$.getJSON('/ajax.php',{action:'search',country:countryValue},function(data) { 
  		    tmpSelect.fillSelect(data).attr('disabled',''); 
  		    
  		    var region_id = returnSelValue(data);
  		    $('#region option[value=' + region_id + ']').attr('selected', 'selected');
  		  
  		    adjustCity(); 
  		});
  	}
  };
  // выбор горада
  function adjustCity(){
  	var countryValue = $('#country').val();
  	var regionValue = $('#region').val();
  	var tmpSelect = $('#city');
  	if(countryValue.length == 0||regionValue.length == 0) {
  		tmpSelect.attr('disabled','disabled');
  		tmpSelect.clearSelect();
  	} else {
  		$.getJSON('/ajax.php',{action:'search',country:countryValue,region:regionValue},function(data) { 
  		    tmpSelect.fillSelect(data).attr('disabled',''); 
  		    var city_id = returnSelValue(data);
  		    $('#city option[value=' + city_id + ']').attr('selected', 'selected');    
  		});
  	}
  };
	
  $('#country').change(function(){
  	adjustRegion();
  }).change();
  $('#region').change(adjustCity);
  
	//$('#hid4').hide();
	$('#hid5').hide();
	
	function TabAppartment()
	{
		$('#hid3').show();
		//$('#hid4').hide();
		$('#hid5').hide();
		$('#hid6').show();
		$('#hid7').show();
		$('#hid8').show();
		$('#hid9').show();
		$('#hid10').show();
		$('#hid11').show();
		$('#hid12').show();
		$('#hid13').show();
	}
	function TabHome()
	{
		$('#hid3').show();
		//$('#hid4').show();
		$('#hid5').show();
		$('#hid6').hide();
		$('#hid7').hide();
		$('#hid8').hide();
		$('#hid9').hide();
		$('#hid10').hide();
		$('#hid11').hide();
		$('#hid12').show();
		$('#hid13').hide();
	}
	function TabTerra()
	{
		$('#hid3').hide();
		//$('#hid4').hide();
		$('#hid5').show();
		$('#hid6').hide();
		$('#hid7').hide();
		$('#hid8').hide();
		$('#hid9').hide();
		$('#hid10').hide();
		$('#hid11').hide();
		$('#hid12').hide();
		$('#hid13').hide();
	}
	function TabOffice()
	{
		$('#hid3').show();
		//$('#hid4').hide();
		$('#hid5').hide();
		$('#hid6').hide();
		$('#hid7').hide();
		$('#hid8').hide();
		$('#hid9').hide();
		$('#hid10').hide();
		$('#hid11').hide();
		$('#hid12').hide();
		$('#hid13').hide();
	}
	
	$('#b3').click(function (){
		$('#hid1').show();
		$('#hid2').hide();
		$('#b5').attr('checked','checked');
		TabAppartment();
		}
	);
	$('#b4').click(function (){
		$('#hid2').show();
		$('#hid1').hide();
		$('#b10').attr('checked','checked');
		TabOffice();
		}
	);
	
	//квартира
	$('#b5').change(function (){
		TabAppartment();
		}
	);
	//комната
	$('#b6').click(function (){
		$('#hid3').show();
		//$('#hid4').hide();
		$('#hid5').hide();
		$('#hid6').hide();
		$('#hid7').show();
		$('#hid8').show();
		$('#hid9').show();
		$('#hid10').hide();
		$('#hid11').show();
		$('#hid12').show();
		$('#hid13').show();
		}
	);
	
	//дом
	$('#b7').click(function (){
		TabHome();
		}
	);
	//дом
	$('#b8').click(function (){
		TabHome();
		}
	);
	//земля
	$('#b9').click(function (){
		TabTerra();
		}
	);
	//офис магазин
	$('#b10').click(function (){
		TabOffice();
		}
	);
	//офис магазин
	$('#b11').click(function (){
		TabOffice();
		}
	);
	//земля
	$('#b12').click(function (){
		TabTerra();
		}
	);
	$('#search').submit(function() {
		if(CheckSearch()==true && $(".unchecked").length != 0)return false;
		else return true;
		}
	);
});