$(document).ready(function () {
    $('.addtocart').click (function() {
	var id = $(this).attr('href').replace(/[\D]*/, '')
	$(this).parent().append('<img src="/images/arrows.gif" id="loading_'+id+'" style="margin: 4px 2px 2px -'+($(this).width()+20)+'px; position: absolute;">');
	if ($("#sameFields_"+id).length) {
	    $("#sameFields_"+id).dialog({resizable: false, width: parseInt($("#sameFields_"+id).find(".name").css('width'))+28, modal: true});
	    $('#loading_'+id).remove();
	} else {
	    $.ajax({url: $(this).attr("href"), cache: false, success: function(html){
		$('body').append('<div id="same'+id+'">'+html+'</div>');
		if (!$("#sameFields_"+id).length) {
		    $("#newBasket").html(html);
		    $('#same'+id).remove();
		} else {
		    var elementWidth = parseInt($("#sameFields_"+id).find(".name").css('width'));
		    if (elementWidth<300) elementWidth = 300;
		    $("#sameFields_"+id).find(".name").width(elementWidth);
		    $("#sameFields_"+id).find(".fl").width(elementWidth);
		    $("#sameFields_"+id).find("p").css({width: Math.floor((elementWidth-50)/($("#sameFields_"+id).find(".name p").length)), textAlign: 'right'});
		    $("#sameFields_"+id).find("p.tb7").css({width: 50, textAlign: 'right'});
		    $("#sameFields_"+id).dialog({resizable: false, width: elementWidth+28, modal: true});
		    $("#sameFields_"+id+" .tocart").click(function() {
			var obj = this;
			$(this).parent().append('<img src="/images/arrows.gif" id="loading_'+($(obj).attr('href').replace(/[\D]*/, ''))+'" style="margin: 4px 2px 2px -'+($(this).width()+20)+'px; position: absolute;">');
			$.ajax({url: $(this).attr("href").replace('/addToCart/', '/ajaxAddToCart/'), cache: false, success: function(html){
			    $("#newBasket").html(html);
			    $('#loading_'+$(obj).attr('href').replace(/[\D]*/, '')).remove();
			}});
		    return false;
		    });
		}
		$('#loading_'+id).remove();
	    }});
	}
        return false;
    });
    
    $(".tocart").click(function() {
	var obj = this;
	$(this).parent().append('<img src="/images/arrows.gif" id="loading_'+($(obj).attr('href').replace(/[\D]*/, ''))+'" style="margin: 4px 2px 2px -'+($(this).width()+20)+'px; position: absolute;">');
	$.ajax({url: $(this).attr("href").replace('/addToCart/', '/ajaxAddToCart/'), cache: false, success: function(html){
	    $("#newBasket").html(html);
	    $('#loading_'+$(obj).attr('href').replace(/[\D]*/, '')).remove();
	}});
	return false;
    });
    
    $('.addtocompare').click (function() {
        $.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(/product_/, '');
    	if (count>0) {
	    $(this).parent().append('<img src="/images/arrows.gif" id="loading_'+product+'" style="margin: 2px; position: absolute;">');
	    $.ajax({url: '/orders/addAjax/'+product+'/'+count, cache: false, success: function(html){ $("#newBasket").html(html); $("#loading_"+product).remove(); }});
	}
    });
    
    $('.deletefromcart').click (function() {
	count = parseInt($("#cart_count").html())-1;
	if (count>0) {
	    id = $(this).attr("href").replace('/orders/deleteProduct/', '');
	    $("#productTr_"+id).hide();
	    $("#cart_count").html(count);
	}
	else {
	    $('#cartListLeft').html('<p>Ваша корзина пуста.</p>');
	    $('.cart_panel').hide();
	}
	$.ajax({url: $(this).attr("href").replace('/deleteProduct/', '/ajaxDeleteProduct/'), cache: false, success: function(html){ $("#newBasket").html(html); }});
    	return false;
    });
        
    $('.delfromcompare').click (function() {
    	$(this).parent().hide();
	count = parseInt($("#compare_count").html())-1;
	if (count!=0) $("#compare_count").html(count); else $("#compareBlock").html('');
    	$.ajax({url: $(this).attr("href").replace('/delFromCompare/', '/ajaxDelFromCompare/'), cache: false, success: function(html){ if (count!=0) $("#compareBlock").html(html); }});
    	return false;
    });
    
    $("input[type=checkbox].compareProduct").click(function () {
	id = $(this).attr('id').replace('compare_', '');
	if ($(this).is(':checked')) {
	    $.ajax({url: '/products/ajaxAddToCompare/'+id, cache: false, success: function(html) {}});
	} else {
	    $.ajax({url: '/products/ajaxDelFromCompare/'+id, cache: false, success: function(html) {}});
	}
    }); 
    
    $('#unique_active').click (function() {
	$(this).addClass('active').removeClass('none');
	$('#unique_all').removeClass('active').addClass('none');
	$('.uniqueTr').hide();
	return false;
    });
    
    $('#unique_all').click (function() {
	$(this).addClass('active').removeClass('none');
	$('#unique_active').removeClass('active').addClass('none');
	$('.uniqueTr').show();
	return false;
    });
    $('.uniqueTr').hide();
    
    $(".tovar .text").each(function () {
	height = $(this).height();
	if (height!=38) {
	    $(this).height(38);
	    if (height>38) $(this).prepend('<span style="height: 20px; width: 20px; margin-left: 184px; background-color: rgb(255, 255, 255); position: absolute; padding-left: 8px; margin-top: 20px;">...</span>');
	}
    });

});
