(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($.browser.msie) {
					currentSelect.add(option);
				  } else {
				  	currentSelect.add(option,null);
				  }

        });
      }
    });
  }

})(jQuery);


$(document).ready(function(){
	// выбор автомобиля
	function adjustAuto(parent,child,init){
		if(init == 1)	var Value = 0;
		else 	var Value = $('#'+parent).val();
		var tmpSelect = $('#'+child);
		if(Value.length == 0) {
			tmpSelect.attr('disabled','disabled');
			tmpSelect.clearSelect();
		} else {
		var hr = $("#url_prefXX").val();
		$.getJSON(
		  hr+'/ajax.php',
		  {marka:Value},
		  function(data) {
			tmpSelect.fillSelect(data).attr('disabled','');
			//alert(data);
		});
	  }

	};

	$('#marka').change(function(){ adjustAuto('marka','model'); });
	$('#marka_z').change(function(){ adjustAuto('marka_z','model_z'); });
	
	$('#hdrive1').click(function(){
		$('.post_need').html('VIN(Frame):');	
	});
	$('#hdrive2').click(function(){
		$('.post_need').html('<span class="need">VIN *:<br />(№ кузова):</span>');	
	});
	if($('#hdrive2').is(':checked')){
		$('.post_need').html('<span class="need">VIN *:<br />(№ кузова):</span>');	
	}
	
	$('.help').hover(
		function(){
			$(this).prev().show().css('font-weight','bold');
		},
		function(){
			$(this).prev().css('display','none');
		}
	);



	$(".dopHref").each(function(){
		this.href = 'javascript:void(false);';
		id=$(this).attr("id");
		$("#"+id+"hint").hide();
	});

	$(".dopHref").click(function(){
		id=$(this).attr("id");
			$("#"+id+"hint")
				.show()
				.mouseleave(function(){
					$("#"+id+"hint").hide();
					});
	});

	$(".dopHref").click(function(){
		$("#"+id+"inp").css("display","inline");
	});
});


