(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);
				  if($.support.cssFloat) {
					  currentSelect.add(option,null);
				  } else {
					  currentSelect.add(option);
				  }
			  });
		  }
	  });
  }
})(jQuery);

// Вытаскиваем AJAXом SELECTы которые выбирает пользователь
$(document).ready(function(){

  // выбор модели авто
  function adjustAutoModel(){
  	var autoproducerValue = $('#producer').val();
  	var tmpSelect = $('#model');
  	if(autoproducerValue.length == 0) {
  		tmpSelect.attr('disabled','disabled');
  		tmpSelect.clearSelect();
  		adjustAutoYears();
  	} else {
  		$.getJSON('',{autoproducer:autoproducerValue},function(data) { 
  	  		tmpSelect.fillSelect(data.content).attr('disabled',''); 
			//$('#count').text("Найдено " + data.count + " товаров");
  	  		adjustAutoYears();
  	  	});
  	}
  };
  // end выбор модели авто
  // выбор года авто
  function adjustAutoYears(){
  	var autoproducerValue = $('#producer').val();
  	var automodelValue = $('#model').val();
  	var tmpSelect = $('#years');
  	if(autoproducerValue.length == 0||automodelValue.length == 0) {
  		tmpSelect.attr('disabled','disabled');
  		tmpSelect.clearSelect();
  		adjustAutoModifity();
  	} else {
  		$.getJSON('',{autoproducer:autoproducerValue,automodel:automodelValue},function(data) { 
  	  		tmpSelect.fillSelect(data.content).attr('disabled','');
  	  		//$('#count').text("Найдено " + data.count + " товаров"); 
  	  		adjustAutoModifity(); 
  	  	});
  	}
  };
  // end выбор года авто
  
  // выбор модификации автомобиля
  function adjustAutoModifity(){
  	var autoproducerValue = $('#producer').val();
  	var automodelValue = $('#model').val();
	var autoyearsValue = $('#years').val();
  	var tmpSelect = $('#modifity');
  	if(autoproducerValue.length == 0||automodelValue.length == 0||autoyearsValue.length == 0) {
  		tmpSelect.attr('disabled','disabled');
  		tmpSelect.clearSelect();
  	} else {
  		$.getJSON('',{autoproducer:autoproducerValue,automodel:automodelValue,autoyears:autoyearsValue},function(data) { 
  	  		tmpSelect.fillSelect(data.content).attr('disabled','');
  	  		//$('#count').text("Найдено " + data.count + " товаров");
			//adjustDiskET(); 
  	  	});
  	}
  };
  // end выбор модификации автомобиля
  
	$('#producer').change(function(){
		adjustAutoModel();
	}).change();
	$('#model').change(adjustAutoYears);
	$('#years').change(adjustAutoModifity);
	
	$('#modifity').change(function(){
  	if($(this).val().length != 0) { 
  	  	var autoproducerValue = $('#producer').val();
  	  	var automodelValue = $('#model').val();
  	  	var autoyearsValue = $('#years').val();
		var automodifityValue = $('#modifity').val();
  	  	document.location.href = "?autoproducer=" + autoproducerValue + "&automodel=" + automodelValue + "&autoyears=" + autoyearsValue + "&automodifity=" + automodifityValue; 
  	}
  });
  

});



//Если пользователь выбрал уже параметры, отображаем их
var arrGET = get_request();
if(arrGET != undefined && arrGET["autoproducer"] != undefined) {
	$(document).ready(function(){

		$('#producer option[value="' + arrGET["autoproducer"] + '"]').attr('selected', 'selected');

		$.getJSON('',{autoproducer:arrGET["autoproducer"]},function(data) { 
			$('#model').fillSelect(data.content).attr('disabled','');
			$('#model option[value="' + arrGET["automodel"] + '"]').attr('selected', 'selected');
		});
		$.getJSON('',{autoproducer:arrGET["autoproducer"],automodel:arrGET["automodel"]},function(data) { 
			$('#years').fillSelect(data.content).attr('disabled','');
			$('#years option[value="' + arrGET["autoyears"] + '"]').attr('selected', 'selected');
		});
		$.getJSON('',{autoproducer:arrGET["autoproducer"],automodel:arrGET["automodel"],autoyears:arrGET["autoyears"]},function(data) { 
			$('#modifity').fillSelect(data.content).attr('disabled','');
			$('#modifity option[value="' + arrGET["automodifity"] + '"]').attr('selected', 'selected');
		});
	});
}


