// корзина
$('form.variants').live('submit', function(e) { 
	e.preventDefault();// return;
	button = $(this).find('input[type="submit"]');
	button2 = $(this).find('button[type="submit"]');
	if($(this).find('input[name=variant]:checked').size()>0)
		variant = $(this).find('input[name=variant]:checked').val();
	if($(this).find('select[name=variant]').size()>0)
		variant = $(this).find('select').val();
	var fav = '';
	var favs = $(this).find('#params input, #params select');
	if($(favs).length > 0){
		fav = $(favs).serialize();
	}
	yandexReachGoal('korzina');
	$.ajax({
		url: "ajax/cart.php",
		/*data: {variant: variant},*/
		data: {variant: variant,amount: $(this).find('select[name="amount"]').val(),feature: fav},
		dataType: 'json',
		success: function(data){ console.log(button2);
			$('#cart_informer').html(data);                                                
            $('#top-shopcart').addClass('notempty');                        
			if(button.length && button.attr('data-result-text')) button.val(button.attr('data-result-text'));
			if(button2.length && button2.attr('data-result-text')) button2.text(button2.attr('data-result-text')).attr('disabled', true);
			 
                       
            $(document).trigger( "update.shopcart" ); //событие добавления в корзину
            
		}
	});
	var o1 = $(this).offset();
	var o2 = $('#cart_informer').offset();
	var dx = o1.left - o2.left;
	var dy = o1.top - o2.top;
	var distance = Math.sqrt(dx * dx + dy * dy);
	$(this).closest('.product').find('.image img').effect("transfer", { to: $("#cart_informer"), className: "transfer_class" }, distance);
	$('.transfer_class').html($(this).closest('.product').find('.image').html());
	$('.transfer_class').find('img').css('height', '100%');
	return false;
});


$(document).ready(function() {
	$('.keypress').on('keypress', function(event){
		var key, keyChar;
			if(!event) var event = window.event;
			if (event.keyCode) key = event.keyCode;
			else if(event.which) key = event.which;
			if(key==null || key==0 || key==8 || key==13 || key==9 || key==46 || key==37 || key==39 ) return true;
			keyChar=String.fromCharCode(key);
			if(!/\d/.test(keyChar))
				return false;
	});
	$('.filter .block.range_slider').each(function(){
		var th = this;

		var min = parseFloat($(th).find('input.min').val());
		var max = parseFloat($(th).find('input.max').val());
		var current_min = parseFloat($(th).find('input.current_min').val());
		var current_max = parseFloat($(th).find('input.current_max').val());
		$(th).find("div.slider").slider({
			range: true,
			min: min,
			max: max,
			values:[current_min,current_max],
			range:true,
			slide:function(event, ui){
				$(th).find("input.imin").val($(th).find("div.slider").slider("values",0));
				$(th).find("input.imax").val($(th).find("div.slider").slider("values",1));
			},
			stop: function(event, ui) {
				$(th).find("input.imin").val($(th).find("div.slider").slider("values",0));
				$(th).find("input.imax").val($(th).find("div.slider").slider("values",1));
			}
		});
		$(th).find("input.imin").change(function(){
			var value1=$(th).find("input.imin").val();
			var value2=$(th).find("input.imax").val();
				if(parseInt(value1) > parseInt(value2)){
					value1 = value2;
					$(th).find("input.min").val(value1);
				}
			$(th).find("div.slider").slider("values",0,value1);
		});
		$(th).find("input.imax").change(function(){
			var value1=$(th).find("input.imin").val();
			var value2=$(th).find("input.imax").val();
				if(parseInt(value1) > parseInt(value2)){
					value2 = value1;
					$(th).find("input.max").val(value2);
				}
			$(th).find("div.slider").slider("values",1,value2);
		});


	});

function ajaxFormRequest(form, callback, callbackbf) {
	jQuery.ajax({
		url: form.action,
		type: form.method,
		dataType: "html",
		data: jQuery(form).serialize(),
		success: callback,
		beforeSend: callbackbf
	});
}
jQuery('form.ajaxform').submit(function(e){
	e.preventDefault();
	var form = this; 
	var p = $(form).find('input[name=phone]');
	if(p.val().length < 5){
		p.after('<p class="hel-block">Введите телефон</p>');
		return false;
	}
	ajaxFormRequest(form, function(dat){
		jQuery(form)[0].reset();
	//	jQuery.fancybox.hideActivity();
		jQuery.fancybox(dat);
	}, function(){
		//$.fancybox.showActivity();
	}
	);
	
	return false;
});

	jQuery('input.preorder').click(function (e) {
		var th = this;
		var varik = $(th).attr('data-id');
		jQuery.fancybox({
			href: '#amount'+varik,
			onComplete: function(){ 
			 jQuery('#fancybox-content').css('border-width',0); 
             jQuery('#fancybox-outer').css('background','none'); 
            }
		});
	    e.preventDefault();

	})

});
