/***************************************************************
This file supports the Cross Country Pictures Section
It is AJAX-a-riffic!
****************************************************************/

var xmlHttp;
var sURL;
var map;
var arNames = [];
var iPrevLat = 24.527135;
var iPrevLng = -23.578125;

function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());
		GEvent.addListener(map, "moveend", function() {
			//var center = map.getCenter();
			//document.getElementById("message").innerHTML = center.toString();
		});
		map.setCenter(new GLatLng(24.527135, -23.578125), 2);
		
		reqBigList();
		
	}
}

function reqBigList() {
	// REQUEST BigList
	sURL = "airports.php"
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleBigListStateChange;
	xmlHttp.open("GET", sURL, true);
	xmlHttp.send(null);
}

function gotBigList() {
	var responseXML = xmlHttp.responseXML;
	var currNode = responseXML.getElementsByTagName("points").item(0);
	currNode = currNode.firstChild;
	processPoint(currNode);
	while (currNode = currNode.nextSibling) {
		processPoint(currNode);
	}
}

function processPoint(currNode) {
	sName = currNode.firstChild.firstChild.nodeValue;
	iLat = currNode.firstChild.nextSibling.firstChild.nodeValue;
	iLng = currNode.firstChild.nextSibling.nextSibling.firstChild.nodeValue;
	sTitle = currNode.firstChild.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;
	sLoc = currNode.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;
	sType = currNode.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;
	//alert(">>" + sName + " " + iLat + " " + iLng + "<<");
	
	sHTML = "<div style=padding:10px><b>" + sTitle + "</b> (" + sName + ")<br/>"+ sLoc +"</div>";
	addMarker(iLat, iLng, sType, sHTML);
	
}

function addMarker(iLat, iLng, sType, sHTML) {
	var point = new GLatLng(iLat, iLng);
	map.addOverlay(createMarker(point, sType, sHTML));
}

function createMarker(point, sType, sHTML) {
	var myIcon = new GIcon();
	if (sType == "AD") {
		myIcon.image = "http://www.very-appealing.com/misc/airports/markerBlue.png";
	}
	if (sType == "A") {
		myIcon.image = "http://www.very-appealing.com/misc/airports/markerRed.png";
	}
	if (sType == "D") {
		myIcon.image = "http://www.very-appealing.com/misc/airports/markerGreen.png";
	}
	if (sType == "C") {
		myIcon.image = "http://www.very-appealing.com/misc/airports/markerYellow.png";
	}
	myIcon.iconAnchor = new GPoint(10, 34);
	myIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	myIcon.infoWindowAnchor = new GPoint(10, 0);
	
	
	var marker = new GMarker(point, myIcon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(sHTML);
	});
	return marker;
}


/****************** AJAX ***********************/
function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}


function handleBigListStateChange() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			gotBigList();
		}
		else {
			alert ("AJAX error!");
		}
	}
}
