var gmap;
var icon;
var default_status = "(Map updates every 30 seconds)";
var update_status = "(Updating map...)";

function load() {
	if (GBrowserIsCompatible()) {
		// Init the map
    	gmap = new GMap2(document.getElementById("map"));
		gmap.addControl(new GLargeMapControl());
    	gmap.addControl(new GMapTypeControl());
        gmap.setCenter(new GLatLng(40.714476,-74.006996), 15);
		// Init the marker icon
		icon = new GIcon();
		icon.image = "http://biobus.ublip.com/icons/ublip_marker.png";
    	icon.shadow = "http://biobus.ublip.com/images/ublip_marker_shadow.png";
    	icon.iconSize = new GSize(23, 34);
    	icon.iconAnchor = new GPoint(11, 34);
    	icon.infoWindowAnchor = new GPoint(11, 34);
		$("update").innerHTML = default_status;
		
	
		loadGeoRSS(); // Load the first time
		
		window.setInterval("loadGeoRSS();", 30000); // Load every 30 seconds after
		
	}
}

function loadGeoRSS() {
	
	$("update").innerHTML = update_status; // Display that we're updating
	
	var timer = Number(new Date());
	
	GDownloadUrl("georss.php?" + timer, function(data, responseCode) {
		gmap.clearOverlays(); // Clear the map
		var xml = GXml.parse(data);
		var title = xml.documentElement.getElementsByTagName("title")[1].firstChild.nodeValue;
		var description = xml.documentElement.getElementsByTagName("description")[1].firstChild.nodeValue;
		var location = xml.documentElement.getElementsByTagName("georss:point");
		if(location.length == 0) // For FF, Safari, etc
			location = xml.documentElement.getElementsByTagNameNS("http://www.georss.org/georss", "point")[0].firstChild.nodeValue.split(" ");
		else // For IE
			location = location[0].firstChild.nodeValue.split(" ");
		var point = new GLatLng(location[0], location[1]);
		gmap.addOverlay(new GMarker(point, icon));
		gmap.panTo(point);
		
		$("update").innerHTML = default_status; // Done updating
		
	});
	
}

function $(id) {
	return document.getElementById(id);	
}

/* Response format
<rss version="2.0" xmlns:georss="http://www.georss.org/georss">
  <channel>
    <title>Ublip Location Feed for BioBus</title>
    <link>http://www.ublip.com</link>
    <description>Location Matters</description>
    <language>en-us</language>
    <item>

      <title>Location reading on Wed, 20 Feb 2008 13:47:31 -0800</title>
      <description>351 Flushing Ave, New York, New York</description>
      <georss:point>40.6985 -73.9614</georss:point>
      <pubDate>Wed, 20 Feb 2008 13:47:31 -0800</pubDate>
    </item>
  </channel>
</rss>
*/