var common_backend_url = '/ajax/ajax_backend.js.php';
var debug_level = 'dev';

function ajax_action(action, param) {
	param['action'] = action;
	var backend_url = param['backend_url'] || common_backend_url;
	$.ajax({
		type: "POST",
		url: backend_url,
		data: param,
		dataType: "json",
		success: function(response) {
			if (response) {
				if (!response.js.action) {
					debug_message('Invalid response');
				} else {
					if(response.text) {
						debug_message(response.text);
					}
					var func = response.js['response_func'] || 'action_'+action+'_response';
					eval(func+'(response.js)');
				}
			}
		}
	});
}
function debug_message(message) {
	if (!message) return;
	if (debug_level == 'dev') alert(message);
	var obj = $("#debug").text(message);
}
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_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';
	}
}

function cart_add(object_class, object_id) {
	ajax_action('cart_add', {'object_class': object_class, 'object_id': object_id, 'response_func': 'action_cart_response'});
}
function cart_del(cart_item_id) {
	ajax_action('cart_del', {'item_id': cart_item_id, 'response_func': 'action_cart_response'});
}
function cart_clear() {
	if (confirm('Вы действительно ходите очистить корзину?')) {
		ajax_action('cart_clear', {'response_func': 'action_cart_response'});
	}
}

/* получение комментария для редактирования */
function GetComment(object_class, object_id, id) {
	ajax_action('comment', {
		'object_class': object_class,
		'object_id': object_id,
		'comment_id': id,
		'response_func': 'comment_response'
	});	
}
function comment_response(response) {
	if (response.status == 0) {
		var elm = gebi('other_error');
		if(elm)elm.innerHTML = response.error;
	} else {
		document.comments.elements['form[text]'].value = response.text;
		document.comments.elements['form[title]'].value = response.title;
	}
}

/* календарь, смена месяца */
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
	});
}

/* вывод списка городов в стране */
function cities_by_country(country) {
	if (!country) return;
	var ajax_loading = gebi("ajax_loading");
	var form_city = gebi("form_city");
	ajax_loading.style.display = "inline";
	var len = form_city.options.length;
	for (var i = len - 1; i >= 0; i--) {
		form_city.remove(i);
	}
	form_city.options[0] = new Option("Идёт загрузка...", "");
	form_city.options[0].selected = true;
	form_city.disabled = false;
	ajax_action("cities", {"country": country});
	ajax_loading.style.display = "none";
}

function action_cities_response(response) {
	if (!response.status) return false;
	var obj = gebi(response.dest_obj_id || "form_city");
	var cities = eval(response.html); // JSON in response.html
	var len = cities.length;
	for (var i = 0; i < len; i++) {
		obj.options[i] = new Option(cities[i], cities[i]);
	}
}
