Ajax.Process = {
	S_PROCESS: "пожалуйста подождите...",

	show: function() {
		if (!this.process) {
			this.process = $("ajax-process");
			if (!this.process) return;
		}
		var top = window.pageYOffset ? window.pageYOffset : document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop;
		this.process.style.top = top + "px";
		this.process.innerHTML = this.S_PROCESS;
		this.process.style.display = "block";
	},

	hide: function() {
		if (!this.process) {
			this.process = $("ajax-process");
			if (!this.process) return;
		}
		this.process.style.display = "none";
	}
};

Ajax.Responders.register({
	onCreate: function() {
		Ajax.Process.show();
	},

	onComplete: function() {
		if(Ajax.activeRequestCount == 0){
			Ajax.Process.hide();
		}
	}
});

// ------------------------------------------------------------

function activateBasketLinks() {
	$$('a[rel="basket"]').each(function(i) {
		var tmp = i.id.split('_');
		var id = tmp[0];
		var index = tmp[1] ? tmp[1] : '';
		i.onclick = function(e) {
			return remote_basket_query(
				{action: 'add', iid: id, index: index}, 
				function(result, errors) {
					showBasketWindow(i, result, errors);
					if (!errors) {
						updateBasketSize(result);
					}
				}
			);
		};
	});
}

function activateCompareLinks() {
	$$('a[rel="compare"]').each(function(i) {
		var id = i.id;
		i.onclick = function(e) {
			return remote_compare_query(
				{action: 'add', iid: this.id}, 
				function(result, errors) {
					showCompareWindow(i, result, errors);
					if (!errors) {
						updateCompare(result);
					}
				}
			);
		};
	});
}

function remote_qurey(url, params, callback) {
	Ajax.Process.show();
	JsHttpRequest.query(url, params, function(result, errors) {
		Ajax.Process.hide();
		callback(result, errors);
	}, true);
	return false;
}

function remote_basket_query(params, callback) {
	return remote_qurey('/content/remote/cat/basket.php', params, callback);
}

function remote_compare_query(params, callback) {
	return remote_qurey('/content/remote/cat/compare.php', params, callback);
}

// ------------------------------------------------------------

function updateBasketSize(params) {
	$('basket_size').innerHTML = params['spell_size'];
	var rb_size = $('remote_basket_size');
	if (rb_size) { 
		rb_size.innerHTML = params['spell_size']; 
	}
	$('basket_link').style.visibility = params['is_empty'] ? 'hidden' : 'visible';
	var rb_link = $('remote_basket_link');
	if (rb_link) { 
		rb_link.style.visibility = params['is_empty'] ? 'hidden' : 'visible';
	}
}


// ------------------------------------------------------------

// показать/скрыть блок сравнения товаров
function updateCompare(params) {
	if (params['list']) {
		$('compare_list').innerHTML = params['list'];
		$('compare_block').style.display = 'block';
	} else {
		$('compare_block').style.display = 'none';
	}
	// показать/скрыть ссылку на сравнение товаров
	$('compare_do').style.display = (params['size'] >= 2) ? 'inline' : 'none';

	var rc_size = $('remote_compare_size');
	if (rc_size) { 
		rc_size.innerHTML = params['spell_size'];
	}
	var rc_link = $('remote_compare_link');
	if (rc_link) { 
		rc_link.style.visibility = (params['size'] >= 2) ? 'visible' : 'hidden';
	}
}

// ------------------------------------------------------------

// Отладочная информация.
/*function checkReqErrors(err) {
	var is_error = err.length
	$('debug').style.display = (is_error) ? 'block' : 'none';
	if (!is_error) { return; }
	$('debug').innerHTML = err;
}*/