

/*
	File: tracking.js
	Description:
		This file provides the GMaps tracking tools, including functions to load data
		from a location feed, populate a GMap with icons, and update bus positions.
	Author: Prashanth Pandian
	Date: 5/22/2006

*/

// 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;

// global vars
var markers;	// data from xml
var req;	// xml request	
var map;	// map


// XMLHTTP request function, called with url to fetch
function loadXMLDoc(url) 
{
	// 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();
		}
	}
}


// handle onreadystatechange event of XMLHTTP request object   
function busDataReceived() 
{
	// 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

// This functions ensures that buses that have disconnected/Out of Service, are removed from the map
// Currently not being used
function cleanBuses()
{
	for(var id in busPoints)
	{
		// remove the overlay(bus marker) from the map if it hasnt been valid
		if(busPoints[id] && busPoints[id].valid != true)
		{
			map.removeOverlay(busPoints[id])
			busPoints[id] = null;
			movePoints[id] = null;
		}
		else if(busPoints[id])
			busPoints[id].valid = false;
	}
}

// Move the buses around
function moveBuses()
{
	
	if(moveCount <= splitMove)
	{
		// for each bus
		for(var id in busPoints)
		{
			// Move the marker by the interpolation amount
			if(busPoints[id].valid)
			{
				newLat = busPoints[id].getPoint().lat() + movePoints[id].lat();
				newLng = busPoints[id].getPoint().lng() + movePoints[id].lng();
				busPoints[id].setPoint(new GLatLng(newLat, newLng));
			}
		}
		moveCount++;
	}
}


// extraneous function to perform AJAX get
function getBuses()
{
	loadXMLDoc("../../shared/location_feed.xml");
}
