function modtimetable_refresh_map (id)
{
	var transport, marshrut, ostanovka, map;
	
	transport = document.getElementById("modtimetable_transport_" + id);
	marshrut = document.getElementById("modtimetable_marshrut_" + id);
	ostanovka = document.getElementById("modtimetable_ostanovka_" + id);
	map = document.getElementById("modtimetable_map_" + id);
	
	if (!(transport && marshrut && ostanovka && map))
		return;
	
	modtimetable_clear_div(map);
	map.appendChild(document.createTextNode("Загрузка..."));
	
	ajax_get("http://" + S_domain + "/ajax/timetable",
			"blockid=" + encodeURIComponent(id) +
				"&transtype=" + encodeURIComponent(transport.value) +
				"&marshrut=" + encodeURIComponent(marshrut.value) +
				"&ostanovka=" + encodeURIComponent(ostanovka.value) +
				"&request=map",
			modtimetable_ostanovka_callback);
}

function modtimetable_refresh_rasp (id)
{
	var marshrut, ostanovka, raspisanie;
	
	marshrut = document.getElementById("modtimetable_marshrut_" + id);
	ostanovka = document.getElementById("modtimetable_ostanovka_" + id);
	raspisanie = document.getElementById("modtimetable_raspisanie_" + id);
	
	if (!(marshrut && ostanovka && raspisanie))
		return;
	
	modtimetable_clear_div(raspisanie);
	raspisanie.appendChild(document.createTextNode("Загрузка..."));
	
	ajax_get("http://" + S_domain + "/ajax/timetable",
			"blockid=" + encodeURIComponent(id) +
				"&marshrut=" + encodeURIComponent(marshrut.value) +
				"&ostanovka=" + encodeURIComponent(ostanovka.value) +
				"&request=rasp",
			modtimetable_ostanovka_callback);
}

function modtimetable_refresh_bliz (id)
{
	var transport, marshrut, ostanovka, bliz;
	
	transport = document.getElementById("modtimetable_transport_" + id);
	marshrut = document.getElementById("modtimetable_marshrut_" + id);
	ostanovka = document.getElementById("modtimetable_ostanovka_" + id);
	bliz = document.getElementById("modtimetable_bliz_" + id);
	
	if (!(transport && marshrut && ostanovka && bliz))
		return;
	
	modtimetable_clear_div(bliz);
	bliz.appendChild(document.createTextNode("Загрузка..."));
	
	ajax_get("http://" + S_domain + "/ajax/timetable",
			"blockid=" + encodeURIComponent(id) +
				"&transtype=" + encodeURIComponent(transport.value) +
				"&marshrut=" + encodeURIComponent(marshrut.value) +
				"&ostanovka=" + encodeURIComponent(ostanovka.value) +
				"&request=bliz",
			modtimetable_ostanovka_callback);
}

function modtimetable_check_callback (result, status)
{
	if (status != 200)
	{
		modmessageblock_set('Ошибка протокола: ' + status, 'errorblock');
		return null;
	}
	
	var data = eval('(' + result + ')');
	
	if (data.status == 'error')
	{
		modmessageblock_set('Ошибка обработчика: ' + data.message, 'errorblock');
		return null;
	}
	
	return data.response;
}

function modtimetable_transport_callback (result, status)
{
	var i, option, marshrut;
	
	var response = modtimetable_check_callback(result, status);
	if (!response)
		return;
	
	marshrut = document.getElementById("modtimetable_marshrut_" + response.blockid);
	if (!marshrut)
		return;
	
	for (i in response.data)
	{
		option = document.createElement("option");
		option.value = response.data[i].marshrut_id + "_" + response.data[i].mnumber;
		option.appendChild(document.createTextNode(response.data[i].description));
		marshrut.appendChild(option);
	}
	marshrut.disabled = false;
}

function modtimetable_marshrut_callback (result, status)
{
	var i, option, ostanovka, lastdir;
	
	var response = modtimetable_check_callback(result, status);
	if (!response)
		return;
	
	ostanovka = document.getElementById("modtimetable_ostanovka_" + response.blockid);
	if (!ostanovka)
		return;
	
	for (i in response.data)
	{
		if (i == 0)
			lastdir = response.data[i].direction;
		else if (response.data[i].direction != lastdir)
		{
			option = document.createElement("option");
			option.value = -2;
			option.innerHTML = "---Обратно---";
			ostanovka.appendChild(option);
			lastdir = response.data[i].direction;
		}
		option = document.createElement("option");
		option.value = response.data[i].ostanovka_id + "_" + response.data[i].direction;
		option.appendChild(document.createTextNode(response.data[i].description));
		ostanovka.appendChild(option);
	}
	ostanovka.disabled = false;
}

function modtimetable_ostanovka_callback (result, status)
{
	var raspisanie, bliz, map, img, span;
	
	var response = modtimetable_check_callback(result, status);
	if (!response)
		return;
	
	switch (response.type)
	{
		case "raspisanie":
		{
			raspisanie = document.getElementById("modtimetable_raspisanie_" + response.blockid);
			if (!raspisanie)
				return;
			
			modtimetable_clear_div(raspisanie);
			
			for (i in response.data)
			{
				if (raspisanie.firstChild)
					raspisanie.appendChild(document.createElement('hr'));
				span = document.createElement('span');
				span.className = 'modtimetable_rasptitle';
				span.appendChild(document.createTextNode(response.data[i].description));
				raspisanie.appendChild(span);
				raspisanie.appendChild(document.createElement('br'));
				raspisanie.appendChild(document.createTextNode(response.data[i].stimes));
			}
			break;
		}
		case "bliz":
		{
			bliz = document.getElementById("modtimetable_bliz_" + response.blockid);
			if (!bliz)
				return;
			
			modtimetable_clear_div(bliz);
			bliz.appendChild(document.createTextNode(response.data));
			break;
		}
		case "map":
		{
			map = document.getElementById("modtimetable_map_" + response.blockid);
			if (!map)
				return;
			
			modtimetable_clear_div(map);
			
			if (response.data)
			{
				img = document.createElement('img');
				img.src = response.data;
				map.appendChild(img);
			}
			else
				map.appendChild(document.createTextNode("Карта недоступна"));
			break;
		}
	}
}
