
var common_backend_url = '/ajax/ajax_backend.js.php';
var debug_level = 'dev';

function debug_message(message) {
	if (!message) return;
	if (debug_level == 'dev') alert(message);
	var obj = document.getElementById("debug");
	if (obj) obj.innerHTML = message;
}

function ajax_action(action, param) {
	param['action'] = action;
	backend_url = param['backend_url'] || common_backend_url;
	JsHttpRequest.query( backend_url, param, function(response, message) {
		debug_message(message);
		if (response) {
			if (!response.action) {
				debug_message('Invalid response');
			} else {
				var func = response['response_func'] || 'action_'+action+'_response';
				eval(func+'(response)');
			}
		}
	}, true);
}

function ajax_response(response) {
	var obj = gebi(response.dest_obj_id);
	if (obj) obj.innerHTML = response.html;
}


/* рейтинги объектов */
function vote(object_class, object_id, vote, dest_obj_id) {
	ajax_action('vote', {'object_class': object_class, 'object_id': object_id, 'vote': vote, 'dest_obj_id' : dest_obj_id, 'response_func': 'ajax_response'});
}

/* голосования */
function polling_answer(polling_id, answer_id, dest_obj_id) {
	ajax_action('polling_answer', {'polling_id': polling_id, 'answer_id': answer_id, 'dest_obj_id' : dest_obj_id, 'response_func': 'ajax_response'});
}

function polling_display(polling_id, view, dest_obj_id) {
	ajax_action('polling_display', {'polling_id': polling_id, 'view': view, 'dest_obj_id' : dest_obj_id, 'response_func': 'ajax_response'});
}

/* работа с пользовательской корзиной */
function action_cart_multi_response(response) {
	if (!response.status) return false;
	var obj = gebi(response.dest_obj_id || 'div_cart_floater');
	obj.innerHTML = response.html;
	if (!response.html) { // пустая корзина
		obj.style.display = 'none';
	} else {
		obj.style.display = 'block';
	}
	if (response.sync_ids) {	
		for (var obj_id in response.sync_ids) {
			var obj = gebi(response.sync_ids[obj_id][0]);
			if (obj) {
				obj.innerHTML = response.sync_ids[obj_id][1];
			}
		}
	}
}

function cart_add(object_class, object_id) {
	ajax_action('cart_add', {'object_class': object_class, 'object_id': object_id, 'response_func': 'action_cart_multi_response'});
}
function cart_del(cart_item_id) {
	ajax_action('cart_del', {'item_id': cart_item_id, 'response_func': 'action_cart_multi_response'});
}
function cart_clear() {
	if (confirm('Вы действительно ходите очистить корзину?')) {
		ajax_action('cart_clear', {'response_func': 'action_cart_multi_response'});
		var s = window.location.href;
		t = s.split('#');
		window.location = t[0];// + '?r='+Math.random();
		return true;
	}
	return false;
}

function cart_add_multi(object_class, object_ids) {
	ajax_action('cart_add_multi', {'object_class': object_class, 'object_ids': object_ids, 'response_func': 'action_cart_multi_response'});
}
function cart_del_multi(cart_item_ids) {
	ajax_action('cart_del_multi', {'item_ids': cart_item_ids, 'response_func': 'action_cart_multi_response'});
}


/* календарь */
function cal_month_change(year, month, curday, dest_obj_id) {
	ajax_action('cal_month_change', {'year': year, 'month': month, 'curday': curday, 'response_func': 'ajax_response', 'dest_obj_id': dest_obj_id});
}
