// JavaScript Document

// An array containing the Marker on the Map 
var busPoints = new Array();
// An array containing interpolation points for the Markers
var movePoints = new Array();
// How many points to interpolate a move by
var splitMove = 10;
// Counter Variable
moveCount = 0;

var markers;	// data from xml
var req;	// xml request	
var map;	// map


// XMLHTTP request function, called with url to fetch
function loadXMLDoc(system) 
{

	var url = "systems/" + system + "/location_feed.xml";
		//alert(url);
	//window.location="http://www.google.com";
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) 
	{

		req = new XMLHttpRequest();
		req.onreadystatechange = busDataReceived;
		req.open("GET", url, true);
		req.setRequestHeader("If-Modified-Since","Sat, 1 Jan 2000");
		req.send(null);
	}
	else if (window.ActiveXObject) 
	{

		// branch for IE/Windows ActiveX version
		isIE = true;
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) 
		{
			req.onreadystatechange = busDataReceived;
			req.open("GET", url, true);
			req.setRequestHeader("If-Modified-Since","Sat, 1 Jan 2000");
			req.send();
		}
	}
	var new_url = "stopView.php?system=" + system;
	window.location = new_url;
}

function busDataReceived() 
{
	//document.write("in bus data received");
	//window.location="http://www.google.com";
	// only if request shows "loaded"
	if (req.readyState == 4 && req.status == 200) 
	{
		var xmlDoc = req.responseXML;
		// markers contains array of "items", each item is a bus 
		markers = xmlDoc.documentElement.getElementsByTagName("item");

		// loop through buses
		for (var i = 0; i < markers.length; i++) 
		{
			
			// relevant variables from location feed	
			var heading = parseFloat(markers[i].getElementsByTagName("heading")[0].childNodes[0].nodeValue);
			var longitude = parseFloat(markers[i].getElementsByTagName("longitude")[0].childNodes[0].nodeValue);
			var latitude = parseFloat(markers[i].getElementsByTagName("latitude")[0].childNodes[0].nodeValue);
			var route = markers[i].getElementsByTagName("route")[0].childNodes[0].nodeValue;
			var id = markers[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
			var routeid = markers[i].getElementsByTagName("routeid")[0].childNodes[0].nodeValue;
	
			// if a marker for this bus has not been created yet
			if(busPoints[id] == null)
			{
				// create a new marker, and interpolation variable for it
				movePoints[id] = new GLatLng(0, 0);
				//busPoints[id] = new PdMarker(new GLatLng(latitude,longitude),null,route);
				busPoints[id] = new PdMarker(new GLatLng(latitude,longitude),updateBusRouteIcon(heading,routeid),route);
				busPoints[id].valid = true;

				map.addOverlay(busPoints[id]);	// add the marker to the map
			}
			else
			{
				// save the amount to interpolate by into the movePoints array	
				movePoints[id] = new GLatLng((latitude - busPoints[id].getPoint().lat()) /  splitMove,
							(longitude - busPoints[id].getPoint().lng()) /  splitMove);
			
			
				var browserName=navigator.appName; 
				if (browserName=="Microsoft Internet Explorer")
				{ 
					if(busPoints[id].icon != updateBusRouteIcon(heading,routeid))
					{
						map.removeOverlay(busPoints[id]);
						newLat = busPoints[id].getPoint().lat() + movePoints[id].lat();
						newLng = busPoints[id].getPoint().lng() + movePoints[id].lng();
						busPoints[id] = new PdMarker(new GLatLng(newLat,newLng),updateBusRouteIcon(heading,routeid),route);
						busPoints[id].valid = true;
						map.addOverlay(busPoints[id]);
						//busPoints[id].setImage(updateBusRouteIcon(heading,routeid).image);
					}
				}
				else 
				{
					if(busPoints[id].icon != updateBusRouteIcon(heading,routeid))
					{
						busPoints[id].setImage(updateBusRouteIcon(heading,routeid).image);
					}
					
				}		
			
			}
			moveCount = 0;
		}// for, looping over buses
	}// if request has loaded
	else
	{
		//alert("There was a problem retrieving the bus XML data:\n" + req.statusText);	
	}
}//end fcn



function loadCity(system)
{
	//for now just assume ann arbor
	//alert(system);
	
	//window.location="http://www.google.com";	
	//var url = "http://mbus.pts.umich.edu/shared/public_feed.xml";
//NOTE have to update this if the option changes
	if(system != "Choose a System"){
		loadXMLDoc(system);
	}

	
	
}
