var airports = new Array();
var airportStates = new Array();
var boundary = false;

function showBoundary(code)
{
	alert(code);
}

function showAirport(airport, noZoom)
{
	var country = airport.substr(0, 2);
	var a = airports[country][airport];
	var p = new GPoint(a[1], a[2]);	
	
	//Only add the overlay if it's not there already
	if(!a[3])
	{
		var m = new GMarker(p);
		var html = '<table><tr><th colspan="2">'+a[0]+'</th></tr><tr><th>Longitude</th><td>'+a[1]+'</td></tr><tr><th>Lattitute</th><td>'+a[2]+'</td></tr></table>';
		GEvent.addListener(m, "click", function() {this.openInfoWindowHtml(html);});
		airports[country][airport][3] = m;
		map.addOverlay(m);
	}
	
	if(!noZoom)
		map.centerAndZoom(p, 8);
	
	return p;
}

function getAirports(country)
{
	var elm = document.getElementById('tbody_' + country);
	
	elm.style.display = elm.style.display=='none'?'':'none';
	
	
	if(elm.innerHTML.replace(/\s/g, '') != '')
		return;
	
	var tr = document.createElement('tr');
	var td = document.createElement('td');
	var text = document.createTextNode('Loading...');
	
	td.appendChild(text);
	tr.appendChild(td);
	elm.appendChild(tr);
	var xmlhttp = XMLHTTPObj();
	if(typeof xmlhttp == 'boolean') return;

	xmlhttp.open("GET", "getAirports.php?country=" + country, true);
	xmlhttp.send(null)
}

function toggleAll(country)
{
	if(typeof airportStates[country] == 'undefined')
		airportStates[country] = false;
	
	airportStates[country] = !airportStates[country];
	var minX = 0
	for(var i in airports[country])
	{
		if(airportStates[country])
		{
			var p = showAirport(i, true);
		}
		else
		{
			map.removeOverlay(airports[country][i][3]);
			airports[country][i][3] = false;
		}
	}
	
	if(airportStates[country])
	{
		map.recenterOrPanToLatLng(p);
		map.zoomTo(13);
	}
	
	return false;
}


var lastSearch = '';
function search(elm)
{
	if(!elm.value) return;
	if(elm.value == lastSearch) return;
	lastSearch = elm.value;
	
	var xmlhttp = XMLHTTPObj();
	if(typeof xmlhttp == 'boolean') return;

	xmlhttp.open("GET", "search.php?q=" + elm.value, true);
	xmlhttp.send(null)
}
