function number_format( number, decimals, dec_point, thousands_sep ) {    
	// Format a number with grouped thousands
    // 
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://crestidg.com)
 
    var i, j, kw, kd, km;
 
    // input sanitation & defaults
    if( isNaN(decimals = Math.abs(decimals)) ){
        decimals = 2;
    }
    if( dec_point == undefined ){
        dec_point = ",";
    }
    if( thousands_sep == undefined ){
        thousands_sep = ".";
    }
 
    i = parseInt(number = (+number || 0).toFixed(decimals)) + "";
 
    if( (j = i.length) > 3 ){
        j = j % 3;
    } else{
        j = 0;
    }
 
    km = (j ? i.substr(0, j) + thousands_sep : "");
    kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
    //kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : "");
    kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");
 
 
    return km + kw + kd;
}

$(document).ready(function()
{

	$('.clear-cart').click(function(){
		if (confirm('Вы действительно хотите очистить корзину?'))
			return true
		else return false;
	});
	
	$('.head > .busket > a').bind('click',  function (e) {
		var cart = this;
		$('body').css('cursor', 'wait');
		$.get(cart.href, null, function (data, textStatus) {
			$('body').css('cursor', 'default');

			if (textStatus != '200')
			{
				$(cart).addClass('filled');
				$(cart).html('<b>'+data.count+'</b>');
				
				$('.hor-menu > .invisible a').show(500);
				$('.hor-menu > .cart b').html(number_format(Math.ceil(data.total_cost),0,'',' ')); 
			}	
			else
				alert('При добавлении товара в корзину произошла ошибка!');
		}, 'json');
		return false;
	});
	
	document.set_count = function(edit) {
		if (edit.value == '')
			return false;
		
		var list_item = $(edit).parent(1).parent(1);
		
		href = '/shop/cart/set/' + list_item.find('.item-id').get(0).value + '/' + edit.value;

		$.get(href, null, function (data, textStatus) {
			if (textStatus != '200')
			{
				edit.value = data.count;
				document.update_prices(list_item, data);
			}
		}, 'json');
	}

	document.update_prices = function(list_item, data) {
		list_item.find('.count > input').get(0).value = data.count;
		
		if (data.cost != 0)
		{
			var cost = Math.ceil(data.cost);
			list_item.show(500);
			list_item.find('.price').html('<b>' + number_format(cost,0,'',' ') + '</b>руб. ');
		}
		else
		{
			list_item.hide(500);
		}
		
		var cost = Math.ceil(data.total_cost);
		$('.tool-panel > .cost').html('Итого: <br><b>' + number_format(cost,0,'',' ') + '</b> руб. ')
	}
	
	$('.count > input').bind('keydown', function(e) {

		if (e.keyCode != 13)
			if ((e.keyCode >= 48 && e.keyCode <= 57) ||
				(e.keyCode >= 37 && e.keyCode <= 40) ||
				(e.keyCode >= 8 && e.keyCode <= 9)) 
				return true;
			else
				return false;

		document.set_count(this);
		
	});
	
	$('.count > input').bind('change', function(e) {
		document.set_count(this);
	});
	
	$('.even > a').bind('click',  function (e) {
		var btn = this;
		$('body').css('cursor', 'wait');
		$.get(btn.href, null, function (data, textStatus) {
			$('body').css('cursor', 'default');

			if (textStatus != '200')
			{
				var list_item = $(btn).parent(1); 
				
				document.update_prices(list_item, data);
			}	
			else
				alert('При добавлении товара произошла ошибка!');
		}, 'json');
		return false;
	});
	
	$('.order-status > select > option').bind('click',  function (e) {
		$.post(' ', {status: $(this).text()});
		return false;
	});	

	$('.order-tools .show-variant .change').bind('click',  function (e) {
		$('.order-tools .show-variant').hide(100);
		$('.order-tools .edit-variant').show(100);
		return false;
	});	

	$('.order-tools .edit-variant .save').bind('click',  function (e) {
		$.post(' ', {comment: $('.order-tools .edit-variant > textarea').val()}, function(e) {
			$('.order-tools .show-variant > p').html($('.order-tools .edit-variant > textarea').val());
			$('.order-tools .edit-variant').hide(100);
			$('.order-tools .show-variant').show(100);
		});
		return false;
	});	
	
});