/**
 * objekt, ki se po ajaxu pogovarja s php košarico
 */
function tranQ() {
	this.transaction = 0;
};


tranQ.prototype.sendRequest = function (method) {
	method = encodeURIComponent(method);

	var qs = 'method=' + method;
	var url = SITE_PATH+'ajax_basket.php5';
	
	if (this.transaction == 0 || this.transaction > 5) {
		$.ajax({
			type: "GET",
			url: url,
			data: qs,
			dataType: "json",
			success: function(doc) {
				ajax.loadFromJSON(doc);
				this.transaction = 0;
			},
			parsererror: function(doc) {
				$.unblockUI();
				this.transaction = 0;
			},
			error: function(doc) {
				$.unblockUI();
				this.transaction = 0;
			}
		});
	} else {
		if (this.transaction > 6) {
			this.transaction = 0;
		}
	}
	++this.transaction;
};


tranQ.prototype.loadFromJSON = function (xmlDoc) {
	id = xmlDoc.state;
	if (0 == id) {
		/* confirmed */
		$.unblockUI();
		location.reload();
	} else if (1 < id) {
		/* aborted or cancelled */
		$.unblockUI();
		location.reload();
	} else if (-2 == id) {
		/* exception */
		location.reload();
	}
	$.blockUI({ message: '<h1>'+STR_TRAN_WAIT+'</h1>' }); 
	
};


/**
 * Ponovno nalozi podatke iz XML-lja
 */
tranQ.prototype.waitTran = function() {
	setTimeout('ajax.sendRequest(\'waitTran\');', 0);
	setTimeout('ajax.waitTran();', 2000); // 2 sek
};
