function disdatLoad() {
	if (GBrowserIsCompatible()) {
		PrintDebug("GBrowserIsCompatible() = true");
		window.map = new GMap2(document.getElementById("disdatMap"));
		//map.setCenter(new GLatLng(35, -35), 1);
		map.setCenter(new GLatLng(27, -5), 1);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		
		safeRequest("js/json.php?brief=true");
	}
}

function disdatUnload() {
	GUnload();
}

function DrawMarkers(data) {
	for (i=0;i<data.length;i++) {
		PrintDebug("Adding: " + data[i].info);
		var point = new GLatLng(data[i].lat, data[i].lon);
		var marker = createMarker(point, data[i].info);
		window.map.addOverlay(marker);
	}
}

function createMarker(point, info) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(info);
	});
	
	return marker;
}

function PrintDebug(message) {
	var debug = document.getElementById("debug");
	if (debug != null) {
		debug.innerHTML += "<div>" + message + "</div>";
	}
}

/* See: http://developer.apple.com/internet/webcontent/xmlhttpreq.html */

function safeRequest(url) {
	var req = false;
	
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
    
	if(req) {
		req.onreadystatechange = function() {
		    if (req.readyState == 4 && req.status == 200) {
				var data = eval(req.responseText);
		    	DrawMarkers(data);
			}
		}
		req.open("GET", url, true);
		req.send("");
	}
}
