//var busObjects = new Array(32);  // to hold all of the bus icons
//var map; // new GMap(document.getElementById("map")); initialized in onLoad
var busIcon;
var numBuses = 0;

var secs = 0;
var timerID = null;
var timerRunning = false;
var delay = 1500;

function stopTimer()
{
	if (timerRunning) 
	{
      	clearTimeout(timerID);
      }
	timerRunning = false;
} // stopTimer

function busReloadTimer()
{
	if (secs==0)
	{
		stopTimer();
		loadBuses();
		secs = 15;
		timerID = self.setTimeout("busReloadTimer()", delay);
	} 
	else
	{
		self.status = secs;
		secs = secs - 1;
		timerRunning = true;
		timerID = self.setTimeout("busReloadTimer()", delay);
	}
} // startTimer

// Creates one of our tiny markers at the given point
function createMarker(point,icon,message) 
{
	var marker = new GMarker(point, icon);
	map.addOverlay(marker);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(message);
	});
	return(marker);
}

function direction(heading) 
{
	if ((heading < 23) || (heading > 337)) {
		return("North");
	} else if ((heading >= 23) && (heading <= 67)) {
		return("Northeast");
	} else if ((heading >= 68) && (heading <= 113)) {
		return("East");
	} else if ((heading >= 114) && (heading <= 157)) {
		return("Southeast");
	} else if ((heading >= 158) && (heading <= 203)) {
		return("South");
	} else if ((heading >= 204) && (heading <= 247)) {
		return("Southwest");
	} else if ((heading >= 248) && (heading <= 293)) {
		return("West");
	} else if ((heading >= 294) && (heading <= 337)) {
		return("Northwest");
	}
} // direction

function route(routecode) 
{
	if ((routecode >= 100) && (routecode < 200)) {
		return("Trolley Route");
	} else if ((routecode >= 200) && (routecode < 300)) {
		return("Red Route");
	} else if ((routecode >= 300) && (routecode < 400)) {
		return("Blue Route");
	} else if ((routecode >= 400) && (routecode < 500)) {
		return("Green Route");
	} else if ((routecode >= 500) && (routecode < 600)) {
		return("GT/Emory Route");
	} else if ((routecode >= 600) && (routecode < 700)) {
		return("Midtown Trolley Route");
	} else {
		return("Unknown Route");
	}
} // route

function loadBuses() 
{
	if(busIcon == null)
	{
		// Create our "tiny" marker icon
		busIcon = new GIcon();
		busIcon.image = "http://www.rnoc.gatech.edu/locate/Trolley.png";
		busIcon.shadow = "http://www.rnoc.gatech.edu/locate/icon_files/mm_20_shadow.png";
		busIcon.iconSize = new GSize(29, 29);
		busIcon.shadowSize = new GSize(22, 20);
		busIcon.iconAnchor = new GPoint(6, 20);
		busIcon.infoWindowAnchor = new GPoint(11, 10);
	}

	// first, unload the previous buses if any
	if (numBuses > 0 ) {
		for (var i = 0; i < numBuses; i++) 
		{
			map.removeOverlay(markers[9][i]);
            	delete markers[9][i];
		}
		numBuses = 0;
      }
  
	var request = GXmlHttp.create();
	request.open("GET", "scripts/stinger_location.php", true);
	request.onreadystatechange = function() 
	{
		if (request.readyState == 4) 
		{
			var xmlDoc = request.responseXML;
			var busmarkers = xmlDoc.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < busmarkers.length; i++) 
			{
				var point = new GPoint(parseFloat(busmarkers[i].getAttribute("lng")),parseFloat(busmarkers[i].getAttribute("lat")));
				var timestamp = busmarkers[i].getAttribute("timestamp");
				var message = "<b>" + route(busmarkers[i].getAttribute("route")) + " (Bus " + busmarkers[i].getAttribute("bus") + ")</b><br>" + "Heading " + direction(busmarkers[i].getAttribute("head")) + " at " + busmarkers[i].getAttribute("speed") + " mph<br/> As of " + timestamp;
				markers[9][i] = createMarker(point,busIcon,message);
			}
			numBuses = busmarkers.length;
		}
	}
      request.send(null);

}  // loadBuses


function LoadBusStops() 
{
	if (GBrowserIsCompatible()) 
	{
		// Create the map and center it on Georgia Tech
		//map = new GMap(document.getElementById("map"));
		//map.addControl(new GSmallMapControl());
		//map.addControl(new GMapTypeControl());
		//map.centerAndZoom(new GPoint(-84.398, 33.777), 2);	

		// Creating Icons
		//
		// Creates a new type of icon, using the
		// <a href="http://labs.google.com/ridefinder/">Google Ride Finder</a> "mini"
		// markers as an example. We have to specify the foreground image, the shadow
		// image, and the points at which we anchor the icon to the map and anchor the
		// info window to the icon.

		// Create our "tiny" marker icon
		var stopIconRed = new GIcon();
		stopIconRed.image = "http://www.rnoc.gatech.edu/locate/icon_files/mm_20_red.png";
		stopIconRed.shadow = "http://www.rnoc.gatech.edu/locate/icon_files/mm_20_shadow.png";
		stopIconRed.iconSize = new GSize(12, 20);
		stopIconRed.shadowSize = new GSize(22, 20);
		stopIconRed.iconAnchor = new GPoint(6, 20);
		stopIconRed.infoWindowAnchor = new GPoint(5, 1);

		// Create colored busstop icons
		var stopIconBlue = new GIcon();
		stopIconBlue.image = "http://www.rnoc.gatech.edu/locate/icon_files/mm_20_blue.png";
		stopIconBlue.shadow = "http://www.rnoc.gatech.edu/locate/icon_files/mm_20_shadow.png";
		stopIconBlue.iconSize = new GSize(12, 20);
		stopIconBlue.shadowSize = new GSize(22, 20);
		stopIconBlue.iconAnchor = new GPoint(6, 20);
		stopIconBlue.infoWindowAnchor = new GPoint(5, 1);
      
		var stopIconGreen = new GIcon();
		stopIconGreen.image = "http://www.rnoc.gatech.edu/locate/icon_files/mm_20_green.png";
		stopIconGreen.shadow = "http://www.rnoc.gatech.edu/locate/icon_files/mm_20_shadow.png";
		stopIconGreen.iconSize = new GSize(12, 20);
		stopIconGreen.shadowSize = new GSize(22, 20);
		stopIconGreen.iconAnchor = new GPoint(6, 20);
		stopIconGreen.infoWindowAnchor = new GPoint(5, 1);

		var stopIconYellow = new GIcon();
		stopIconYellow.image = "http://www.rnoc.gatech.edu/locate/icon_files/mm_20_yellow.png";
		stopIconYellow.shadow = "http://www.rnoc.gatech.edu/locate/icon_files/mm_20_shadow.png";
		stopIconYellow.iconSize = new GSize(12, 20);
		stopIconYellow.shadowSize = new GSize(22, 20);
		stopIconYellow.iconAnchor = new GPoint(6, 20);
		stopIconYellow.infoWindowAnchor = new GPoint(5, 1);
      
		var stopIconEmory = new GIcon();
		stopIconEmory.image = "http://www.rnoc.gatech.edu/locate/icon_files/mm_20_emory.png";
		stopIconEmory.shadow = "http://www.rnoc.gatech.edu/locate/icon_files/mm_20_shadow.png";
		stopIconEmory.iconSize = new GSize(12, 20);
		stopIconEmory.shadowSize = new GSize(22, 20);
		stopIconEmory.iconAnchor = new GPoint(6, 20);
		stopIconEmory.infoWindowAnchor = new GPoint(5, 1);

		// Create the bus stop markers
		var stopRequest = GXmlHttp.create();
		stopRequest.open("GET", "trolley-stops.xml", true);
		stopRequest.onreadystatechange = function() 
		{
			if (stopRequest.readyState == 4) 
			{
				var xmlDoc = stopRequest.responseXML;
				var xmlmarkers = xmlDoc.documentElement.getElementsByTagName("marker");
				for (var i = 0; i < xmlmarkers.length; i++) 
				{
					var point = new GPoint(parseFloat(xmlmarkers[i].getAttribute("lng")),
					parseFloat(xmlmarkers[i].getAttribute("lat")));
					var message = "<b>Stop " + xmlmarkers[i].getAttribute("stopnum") + ": " + xmlmarkers[i].getAttribute("stop") + "</b><br>Route: " + xmlmarkers[i].getAttribute("route") + "<br>Direction: " + xmlmarkers[i].getAttribute("direction");
					if (xmlmarkers[i].getAttribute("route") == "Blue") 
					{
						markers[8][i] = createMarker(point,stopIconBlue,message);
					}
					else if (xmlmarkers[i].getAttribute("route") == "Green") 
					{
						markers[8][i] = createMarker(point,stopIconGreen,message);
					}
					else if (xmlmarkers[i].getAttribute("route") == "Trolley") 
					{
						markers[8][i] = createMarker(point,stopIconYellow,message);
					}
					else if (xmlmarkers[i].getAttribute("route") == "Emory") 
					{
						markers[8][i] = createMarker(point,stopIconEmory,message);
					}
					else if (xmlmarkers[i].getAttribute("route") == "Red") 
					{
						markers[8][i] = createMarker(point,stopIconRed,message);
					}
					else 
					{  // use the red one if we don't have it
						markers[8][i] = createMarker(point,stopIconRed,message);
					}
				}
			}
		}
		stopRequest.send(null);
		//busReloadTimer();
	} // is compatible browser 
}