$(document).ready(function () {
    $('.addtocart').click (function() {
	$(this).children('span').html('В корзине');
	$(this).parent().addClass("gray");
        $.ajax({url: $(this).attr("href").replace('/addToCart/', '/ajaxAddToCart/'), cache: false, success: function(html){ $("#cartBlock").html(html); }});
        return false;
    });
    
    $('.addtocompare').click (function() {
	$(this).hide();
	$(this).parent().children('.delfromcompare').show();
        $.ajax({url: $(this).attr("href").replace('/addToCompare/', '/ajaxAddToCompare/'), cache: false, success: function(html){ $("#compareBlock").html(html); }});
        return false;
    });
    
    $("input.productCount").keyup( function() {
	var reg=/\D/;
    	var count = parseInt($(this).val());
    	count=reg.test(count)?0:count;
    	var product = $(this).attr('id').replace(/count_/, '');
    	if (count>0) {
	    $(this).parent().append('<img src="/images/arrows.gif" id="loading_'+product+'" style="margin: 2px;">');
	    $.ajax({url: '/orders/addAjax/'+product+'/'+count, cache: false, success: function(html){ $("#cartBlock").html(html); $("#loading_"+product).remove(); }});
	}
    });
    
    $(".countChange").click(function() {
	var reg=/\D/;
	var product = $(this).attr('id').replace(/count_/, '');
    	var count = parseInt($(this).parent().children("."+$(this).attr('id')).val());
    	count=reg.test(count)?0:count;
	if ($(this).hasClass('plus')) count++; else count--;
	$(this).parent().children("."+$(this).attr('id')).val(count);
	$(this).parent().children("."+$(this).attr('id')).keyup();
    	
    	return false;
    });
    
    $('.deletefromcart').click (function() {
        id = $(this).attr("href").replace('/orders/deleteProduct/', '');
        $("#productTr_"+id).hide();
        $.ajax({url: $(this).attr("href").replace('/deleteProduct/', '/ajaxDeleteProduct/'), cache: false, success: function(html){ $("#cartBlock").html(html); }});
    	return false;
    });
    
    $('.delfromcompare').click (function() {
    	$(this).hide();
	$(this).parent().children('.addtocompare').show();
    	$.ajax({url: $(this).attr("href").replace('/delFromCompare/', '/ajaxDelFromCompare/'), cache: false, success: function(html){ $("#compareBlock").html(html); }});
    	return false;
    });
    
    $('#log_in_form').click(function(){
    	$('#overlay_bg, #reg_form').fadeIn(700);
    	return false;
    });
    $('.close_reg, #overlay_bg').click(function(){
    	$('#overlay_bg, #reg_form').fadeOut(400);
    	return false;
    });
    
    $('.cart_out .cart.full').click(function(){
	if (typeof(sizeCart) !== 'undefined') { 
	    if (sizeCart>0) {
	        $('#overlay_bg2, #overlay_bg3').fadeIn(1000);
	        $('.cart_out .top_cart').slideDown(1000);
	    }
	}
    	return false;
    });
    $('.top_cart_close, #overlay_bg2').click(function(){
    	$('#overlay_bg2, #overlay_bg3').fadeOut(400);
    	$('.cart_out .top_cart').slideUp(600);
    	return false;
    });
});
