function resetBasketCookie(){
    var currentDate = new Date();
    
    //basketId = 's_' + currentDate.getTime() + '_' + Math.random();
    //basketId = 's_' + Math.uuid(); //Should include /js/guid.js
    basketId = 's_' + currentDate.getTime() + '_' + $.cookie('PHPSESSID') + '_' + Math.random();
    
    $.cookie('basketid', basketId, { expires: 7, path: '/' });
}
function raAjax() {
	var t = this;

	t.error = function(o, text, tr) {
		alert('Error: ' + o + '\n' + text + '\n' + tr);
	};

	t.showLoader = function() {
		$("#amount_count, #price_count").html('<img src="/images/ajax-loader.gif" alt="" />');
	};

	t.cartSuccess = function(data) {
        resetBasketCookie();
		t.cartDisplay(data);
		if (confirm('Товар успешно добавлен. Перейти в корзину?')) {
			location.href = '/basket/';
		}
	};

	t.cartDisplay = function(data) {
		$("#amount_count").html(data.result.amount);
		$("#price_count").html(t.number_format(data.result.price, 0, ',', ' '));
	};

	t.cartAdd = function(id) {
		t.showLoader();
		$.ajax({
			type:'POST',
			url:'/ajax_handler.php',
			dataType:'json',
			data:{
				'action':'Add',
				'id':id
			},
			success:t.cartSuccess,
			error: t.error
		});
		return false;
	};

	t.cartShow = function() {
		$.ajax({
			type:'GET',
			url:'/ajax_handler.php',
            cache:false,
			dataType:'json',
			data:{
				'action':'Show'
			},
			success: t.cartDisplay,
			error: t.error
		});
		return false;
	};

	t.number_format = function ( 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;
	};

	return t;
}
$(function(){
	raAjax().cartShow();
});
