$(function() {

	var my_left=($('body').width()/2)-300;
	$('#callback').css('left', my_left);


	// ВЫБОР ПРОДУКЦИИ И ЗАПОЛНЕНИЕ КОЛЛЕКЦИЙ
	$('select[name=product]').change(function(){
		$('select[name=collection]').empty();
		$('select[name=material]').empty();
		var action = $(this).val();
		$('form[name=search_form]').attr('action', '/catalog/'+action+'/');
		var url='/';
		var data='doit=getJson&section='+action;

		jQuery.ajax({
				type: "POST",
				url: url,
				data: data,
				async: true,
				dataType: 'json',
				success: function(jsonDoc){
					if (jsonDoc.collect) {
						$('select[name=collection]').empty();
						$('select[name=collection]').append('<option value="">&nbsp;&nbsp;&nbsp;ВЫБОР</option>');
						$(jsonDoc.collect).each(function(i) {
							if (this.name)
								$('select[name=collection]').append('<option value="'+this.prefix+'">&nbsp;&nbsp;&nbsp;'+this.name+'</option>');
						});
					}

					if (jsonDoc.material) {
						$('select[name=material]').empty();
						$('select[name=material]').append('<option value="">&nbsp;&nbsp;&nbsp;ВЫБОР</option>');
						$(jsonDoc.material).each(function(i) {
							if (this.sostav)
								$('select[name=material]').append('<option value="'+this.sostav+'">&nbsp;&nbsp;&nbsp;'+this.sostav+'</option>');
						});
					}
				}
			});
	});

	// ВЫБОР КОЛЛЕКЦИИ И ЗАПОЛНЕНИЕ МАТЕРИАЛОВ (СОСТАВ)
	$('select[name=collection]').live("change", function(){		
		var collection = $(this).val();
		var url='/';
		var action = $('select[name=product] > option:selected').val();
		var data='doit=getJson&section='+action+'&collection='+collection;
		$('form[name=search_form]').attr('action', '/catalog/'+action+'/'+collection+'/');
		jQuery.ajax({
				type: "POST",
				url: url,
				data: data,
				async: true,
				dataType: 'json',
				success: function(jsonDoc){
					if (jsonDoc.material) {
						$('select[name=material]').empty();
						$('select[name=material]').append('<option value="">&nbsp;&nbsp;&nbsp;ВЫБОР</option>');
						$(jsonDoc.material).each(function(i) {
							if (this.sostav)
								$('select[name=material]').append('<option value="'+this.sostav+'">&nbsp;&nbsp;&nbsp;'+this.sostav+'</option>');
						});
					}
				}
			});
	});


	$('form[name=search_form]').submit(function(){
		var prod=$('select[name=product] option:selected').val();
		var coll=$('select[name=collection] option:selected').val();
		var matr=$('select[name=material] option:selected').val();
		if (!prod) {
			alert('Не выбран тип продукции');
			$('select[name=product]').focus();
			return false;
		}
	});
	
	// скачивание прайс-листа
	$(".down a").click(function(){
		$("#callback").animate({"width": "toggle", "opacity": "toggle"}, { duration: "fast" });		
		return false;
	});

	// закрытие окна рег.формы
	$('#close_call').click(function(){
		$("#callback").hide();
	});

	// обработка рег.формы
	$('form[name=callback]').submit(function(){
		var result=true;
		$('form[name=callback] input.nesc').each(function(i){
			if (!$(this).val()) {
				result=false;
				alert('Не заполнено поле "'+$(this).attr('title')+'"');
				$(this).focus();
				return false;
			}
		});
		if (result) {
			var data="doit=callme&"+$('form[name=callback]').serialize();
			var answer = jQuery.ajax({
				type: "POST",
				url: "/ajax.php",
				data: data,
				async: false
			}).responseText;

			alert('Спасибо '+$('#callback_user').val()+', мы обязательно свяжемся с Вами!')
			$("#callback").hide();
			window.location = $(".down a").attr('href');
		}
		return false;
	});


});
