var can_add_to_cart = true;
function cart(hash, id) {
	if(can_add_to_cart) {
		can_add_to_cart = false;
		$('#cart_link_' + hash).fadeOut('fast', function() {
			$('#cart_wait_' + hash).fadeIn('fast', function() {
				if ($('#cart_link_' + hash).attr('rel') == '1') {
					cart_process(hash, id, 'add');
				} else {
					cart_process(hash, id, 'delete');
				}
			});
		});
	}
	return false;
}

function cart_process(hash, id, type) {
	$.getJSON('/cart/'+type, { product_id: id }, function(json) {
		$('#cart_link_' + hash).text(json.iink_text);
		$('#cart_link_' + hash).attr('rel', json.rel);
		$('#cart_nb_items').text(json.cart_nb_items);
		$('#cart_total_price').text(json.cart_total_price);
		$('#cart_wait_' + hash).fadeOut('fast', function() {
			$('#cart_link_' + hash).fadeIn('fast'); 
			can_add_to_cart = true;
		});
	});
}

function cart_quantity(id, type) {
	$('#quantity_'+id).fadeOut('fast', function() {
		$('#quantity_wait_'+id).fadeIn('fast', function(){
			$.getJSON('/cart/quantity/type/'+type, { product_id: id }, function(json) {
				if(json.item_nb>1) {
					$('#quantity_'+id+' input[value="-"]').show();
				} else {
					$('#quantity_'+id+' input[value="-"]').hide();
				}
				$('#quantity_'+id+' .num').text(json.item_nb);
				$('#cart_total_price').text(json.cart_total_price);
				$('#cart_total_price_with_tax').text(json.cart_total_price_with_tax);
				$('#cart_total_weight').text(json.cart_total_weight);
				$('#cart_total_items_count').text(json.cart_total_items_count);
				if(json.item_nb!=1) {
					$('#item_total_'+id).html('Стоимость за <b>'+json.item_nb+'</b> шт.: <span style="color: #D60808; font-weight: bold;">'+json.item_total_price+'</span> ');
				} else {
					$('#item_total_'+id).text('');
				}
				
				$('#quantity_wait_'+id).fadeOut('fast', function() {
					$('#quantity_'+id).fadeIn('fast');
				});
			});			
		});
	});	
}

function get_comments(page, product_id) {
	$.post('/catalog/getComments', { page: page, product_id: product_id }, function(data) {
		$('#comments_result').html(data);
	});
}

function debug(text) {
	$('#core_debug').append(text + ' ');
};