jQuery.fn.siluetcart = function () {
return this.each(function (i) {
var cart = this;
var jq_cart = jQuery(cart);

cart.currency_format = function (value) {
	value = String(value);
	while (value.match(/([0-9])([0-9]{3})( |\.|$)/)) {
		value = value.replace(/([0-9])([0-9]{3})( |\.|$)/, '$1 $2');
	}
	return value;
}

cart.do_request = function (data, callback, type) {
	data = jQuery.merge(data, [{name: 'random', value: Math.random()}]);
	if (jq_cart.attr('method').toLowerCase() === 'post') {
		jQuery.post(jq_cart.attr('action'), data, callback, type);
	} else {
		jQuery.get(jq_cart.attr('action'), data, callback, type);
	}
}

cart.insert_request = function (data, callback, type) {
	cart.do_request(jQuery.merge(data, [{name: 'request', value: 'insert'}]), callback, type);
}

cart.update_request = function (data, callback, type) {
	cart.do_request(jQuery.merge(data, [{name: 'request', value: 'update'}]), callback, type);
}

cart.delete_request = function (data, callback, type) {
	cart.do_request(jQuery.merge(data, [{name: 'request', value: 'delete'}]), callback, type);
}

cart.update_items = function (request_cart_item) {
	var unique_quantity_val = 0;
	var total_quantity_val = 0;
	var total_cost_val = 0;
	if($(jq_cart).attr('id') == 'hidden-form'){
		var selector = 'span.cart_item';
	}else{
		var selector = '.cart_item:visible';
	}
	var hasRequestedPrice = false;
	var hasRealPrice = false;
	jQuery(selector, jq_cart).each(function (i) {
		var cart_item = this;
		var jq_cart_item = jQuery(cart_item);
		// quantity
		var price_val = Number(jQuery(':input.cart_item_price', jq_cart_item).val());
		var quantity_val = Number(jQuery(':input.cart_item_quantity', jq_cart_item).val());
		var cost_val = price_val * quantity_val;
		if (isNaN(cost_val)) cost_val = 0;
		if(price_val == 0){
			hasRequestedPrice = true;
			span_cost_val = 'по запросу';
		}else{
			hasRealPrice = true;
			span_cost_val = cart.currency_format(cost_val);
		}
		
		jQuery(':input.cart_item_cost', jq_cart_item).val(cost_val);
		jQuery('span.cart_item_cost', jq_cart_item).html(span_cost_val);
		// checked
		if (jQuery('.cart_item_checkbox', jq_cart_item).attr('checked')) {
			jQuery('.cart_item_tag', jq_cart_item).removeClass('cart_item_tag_pallor');
			unique_quantity_val ++;
			total_quantity_val += quantity_val;
			total_cost_val += cost_val;
		} else {
			jQuery('.cart_item_tag', jq_cart_item).addClass('cart_item_tag_pallor');
		}
		// request
		if (request_cart_item === cart_item) {
			cart.update_request(jQuery(':input', jq_cart_item).serializeArray());
		}
	});
	if (isNaN(total_quantity_val)) total_quantity_val = 0;
	if (isNaN(total_cost_val)) total_cost_val = 0;
	if(total_cost_val == 0){
		jQuery('#cart-prefix').hide();
		span_total_cost_val = 'по запросу';
	}else{
		jQuery('#cart-prefix').show();
		span_total_cost_val = total_cost_val;
	}
	jQuery(':input.cart_unique_quantity', jq_cart).val(unique_quantity_val);
	jQuery(':input.cart_total_quantity', jq_cart).val(total_quantity_val);
	jQuery(':input.cart_total_cost', jq_cart).val(total_cost_val);
	jQuery('span.cart_unique_quantity').html(unique_quantity_val);
	jQuery('span.cart_total_quantity').html(total_quantity_val);
	jQuery('span.cart_total_cost').html(cart.currency_format(total_cost_val));
	jQuery('span.cart_total_cost', jq_cart).html(cart.currency_format(span_total_cost_val));
////////////
if (String(unique_quantity_val).match(/^([0-9]*[023456789])?1$/)) {
$('span.cart_unique_quantity_word').html('позиция');
} else if (String(unique_quantity_val).match(/^([0-9]*[023456789])?[234]$/)) {
$('span.cart_unique_quantity_word').html('позиции');
} else {
$('span.cart_unique_quantity_word').html('позиций');
}
////////////
}

cart.delete_items = function (request_cart_item) {
	jQuery('.cart_item:visible', jq_cart).each(function (i) {
		var cart_item = this;
		var jq_cart_item = jQuery(cart_item);
		// request/checked
		if ((request_cart_item === cart_item) || (!request_cart_item && !jQuery('.cart_item_checkbox', jq_cart_item).attr('checked'))) {
			jQuery('.cart_item_checkbox', jq_cart_item).attr('checked', '');
			cart.delete_request(jQuery(':input', jq_cart_item).serializeArray());
			jq_cart_item.hide();
		}
	});
}

jQuery('.cart_item', jq_cart).each(function (i) {
	var cart_item = this;
	var jq_cart_item = jQuery(cart_item);
	// quantity
	jQuery('.cart_item_quantity', jq_cart_item).change(function () {
		cart.update_items(cart_item);
	}).keyup(function () { jQuery(this).change(); });
	// checkbox
	jQuery('.cart_item_checkbox', jq_cart_item).change(function () {
		cart.update_items(cart_item);
	}).click(function () { jQuery(this).change(); });
	// delete
	jQuery('.cart_item_delete', jq_cart_item).click(function () {
		cart.delete_items(cart_item);
	});
});

jQuery('.cart_button_delete', jq_cart).click(function () {
	cart.delete_items();
	return false;
}).show();

jQuery('.cart_button_check', jq_cart).click(function () {
	jQuery('.cart_item_checkbox:visible', jq_cart).attr('checked', ((jQuery('.cart_item_checkbox:visible:not(:checked)', jq_cart).size() > 0) ? 'checked' : '')).change();
	return false;
}).show();

cart.update_items();

});

}
