function number_format(_number, _cfg){//http://art-blog.ru/blog/topic/49/
  function obj_merge(obj_first, obj_second){
    var obj_return = {};
    for (key in obj_first){
      if (typeof obj_second[key] !== 'undefined') obj_return[key] = obj_second[key];
      else obj_return[key] = obj_first[key];
      }
    return obj_return;
  }
  function thousands_sep(_num, _sep){
    if (_num.length <= 3) return _num;
    var _count = _num.length;
    var _num_parser = '';
    var _count_digits = 0;
    for (var _p = (_count - 1); _p >= 0; _p--){
      var _num_digit = _num.substr(_p, 1);
      if (_count_digits % 3 == 0 && _count_digits != 0 && !isNaN(parseFloat(_num_digit))) _num_parser = _sep + _num_parser;
      _num_parser = _num_digit + _num_parser;
      _count_digits++;
      }
    return _num_parser;
  }
  if (typeof _number !== 'number'){
    _number = parseFloat(_number);
    if (isNaN(_number)) return false;
  }
  var _cfg_default = {before: '', after: '', decimals: 2, dec_point: '.', thousands_sep: ','};
  if (_cfg && typeof _cfg === 'object'){
    _cfg = obj_merge(_cfg_default, _cfg);
  }
  else _cfg = _cfg_default;
  _number = _number.toFixed(_cfg.decimals);
  if(_number.indexOf('.') != -1){
    var _number_arr = _number.split('.');
    var _number = thousands_sep(_number_arr[0], _cfg.thousands_sep) + _cfg.dec_point + _number_arr[1];;
  }
  else var _number = thousands_sep(_number, _cfg.thousands_sep);
  return _cfg.before + _number + _cfg.after;
}

$(function() {
	$("#cart tr .remove input").click(function() {
		var orderCode = $(this).val();
		$.ajax({
			type: "GET",
			url: "http://www.trikvetra.ru/cartdemo/cart_action.php",
			data: "remove[]=" + orderCode,
			success: function() {
				$("#cart tr .remove input[value=" + orderCode + "]").parent().parent().fadeOut(500, function() {
					$(this).remove();
					calcPrice();
				});
			},
			error: function() {
				window.location("http://www.trikvetra.ru/cartdemo/cart_action.php?remove[]="+orderCode);
			}
		});
		$.ajax({
			type: "GET",
			url: "http://www.trikvetra.ru/cartdemo/acart.php?action=cartinfo",
			success: function(data) {
				resp=data.split("||");
				if(window.parent.document.getElementById("num") != null)
				{
					if (!resp[1]) {resp[1] = "0";}
					window.parent.document.getElementById("num").innerHTML=resp[1];
				}
				if(window.parent.document.getElementById("jcart-subtotal") != null)
				{
				if (!resp[0]) {resp[0] = "0 руб.";}
				window.parent.document.getElementById("jcart-subtotal").innerHTML=resp[0];
				}
			},
			error: function() {
				window.location("http://www.trikvetra.ru/cartdemo/cart_action.php?remove[]="+orderCode);
			}
		});
	});

	$("#cart tr .quantity input").change(function() {
		var orderCode = $(this).attr("name").slice(9, -1);
		var quantity = $(this).val();
		$.ajax({
			type: "GET",
			url: "http://www.trikvetra.ru/cartdemo/cart_action.php",
			data: "quantity[" + orderCode + "]=" + quantity,
			success: function() {
				var startColor = $("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().hasClass("odd") ? "#eee" : "#fff";
				$("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().find("td").animate({ backgroundColor: "#ff8" }, 100).animate({ backgroundColor: startColor }, 800);
				calcPrice();
			},
			error: function() {
				window.location("http://www.trikvetra.ru/cartdemo/cart_action.php?quantity[" + orderCode + "]=" + quantity);
			}
		});
		$.ajax({
			type: "GET",
			url: "http://www.trikvetra.ru/cartdemo/acart.php?action=cartinfo",
			success: function(data) {
				resp=data.split("||");
				if(window.parent.document.getElementById("num") != null)
				{
					if (!resp[1]) {resp[1] = "0";}
					window.parent.document.getElementById("num").innerHTML=resp[1];
				}
				if(window.parent.document.getElementById("jcart-subtotal") != null)
				{
				if (!resp[0]) {resp[0] = "0 руб.";}
				window.parent.document.getElementById("jcart-subtotal").innerHTML=resp[0];
				}
			},
			error: function() {
				window.location("http://www.trikvetra.ru/cartdemo/cart_action.php?remove[]="+orderCode);
			}
		});
	});
});
/*
function calcPrice() {
	var totalPrice = 0;
	$("#cart tr .quantity").parent().each(function() {
		var quantity = $(".quantity input", this).val();
		var unitPrice = $(".unit_price", this).text().slice(1);
		var extendedPrice = quantity*unitPrice;
		totalPrice += extendedPrice;

		$("#extended_price").html(extendedPrice);
	});

	$("#total_price").html(totalPrice);

	if ( totalPrice == 0 ) {
		$("#cart").parent().replaceWith("<p class='center'>Ваша корзина пуста.</p>");
	}
}"$"+"$" +
*/
function calcPrice() {
	var totalPrice = 0;
	$("#cart tr .quantity").parent().each(function() {
		var quantity = $(".quantity input", this).val();
		var unitPrice = $(".unitprice", this).text().slice(0);//$(".unit_price", this).text().slice(0);
		var extended_Price = quantity*unitPrice;
		var extendedPrice = number_format(quantity*unitPrice, {decimals: 0, after: " руб.", thousands_sep: " "});
		totalPrice += extended_Price;

		$(".extended_price", this).html(extendedPrice);
		$("#total_price").html( number_format(totalPrice, {decimals: 0, after: " руб.", thousands_sep: " "}) );
	});
	if ( totalPrice == 0 ) {
		$("#cart").parent().replaceWith("<p class='center'>Ваша корзина пуста.</p>");
	}
}
