

var markers;
var req2;
var reqRouteStops;
//var routeStopList;
var routeName;
//var infoforstop = <?php echo "\"".$_GET["name"]."\""; ?>; // the requested stop's name, as given by the GET string 

function loadXMLDocStops(url) 
{
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) 
	{
		req2 = new XMLHttpRequest();
		req2.onreadystatechange = processReqChangeStops;
		req2.open("GET", url, true);
		req2.setRequestHeader("If-Modified-Since","Sat, 1 Jan 2000");
		req2.send(null);
	// branch for IE/Windows ActiveX version
	}
	else if (window.ActiveXObject) 
	{
		req2 = new ActiveXObject("Microsoft.XMLHTTP");
		if (req2)
		{
			req2.onreadystatechange = processReqChangeStops;
			req2.open("GET", url, true);
			req2.setRequestHeader("If-Modified-Since","Sat, 1 Jan 2000");
			req2.send();
		}
	}
}

function retrieveStopList()
{

	if (window.XMLHttpRequest) 
	{
		req2 = new XMLHttpRequest();
		req2.onreadystatechange = processReqChangeStops2;
		req2.open("GET", "../../shared/stops.xml", true);
		req2.setRequestHeader("If-Modified-Since","Sat, 1 Jan 2000");
		req2.send(null);
	// branch for IE/Windows ActiveX version
	}
	else if (window.ActiveXObject) 
	{
		req2 = new ActiveXObject("Microsoft.XMLHTTP");
		if (req2)
		{
			req2.onreadystatechange = processReqChangeStops2;
			req2.open("GET", "../../shared/stops.xml", true);
			req2.setRequestHeader("If-Modified-Since","Sat, 1 Jan 2000");
			req2.send();
		}
	}	
	
	
	
}

function loadXMLDocRouteStops(url) 
{
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) 
	{
		reqRouteStops = new XMLHttpRequest();
		reqRouteStops.onreadystatechange = processReqRouteStops;
		reqRouteStops.open("GET", url, true);
		reqRouteStops.setRequestHeader("If-Modified-Since","Sat, 1 Jan 2000");
		reqRouteStops.send(null);
	// branch for IE/Windows ActiveX version
	}
	else if (window.ActiveXObject) 
	{
		reqRouteStops = new ActiveXObject("Microsoft.XMLHTTP");
		if (reqRouteStops)
		{
			reqRouteStops.onreadystatechange = processReqRouteStops;
			reqRouteStops.open("GET", url, true);
			reqRouteStops.setRequestHeader("If-Modified-Since","Sat, 1 Jan 2000");
			reqRouteStops.send();
		}
	}
}

function addStopInfo(marker, html, html2) 
{	
	//marker.openInfoWindowHtml("<a href=index.php?name="+html.replace(/ /g,"%20")+">"+html+"</a> <br>" + html2);
	GEvent.addListener(marker, "click", function() 
	{
		//opts={onOpenFn:setTimeout("map.closeInfoWindow()",20000)};
		marker.openInfoWindowHtml("<a href=index.php?name="+html.replace(/ /g,"%20")+">"+html+"</a> <br>" + html2);
	
	});
	
	return marker;
}

function makeStopList(html) 
{
	htmlnode = document.createTextNode(html);
	linknode = document.createElement("a");
	linknode.setAttribute("href", "index.php?name="+html.replace(/ /g,"%20"));
	linknode.appendChild(htmlnode);

	linode = document.createElement("li");
	linode.appendChild(linknode);
	document.getElementById("stopList").appendChild(linode);
	//document.getElementById("footer").innerHTML = activeStopList.length;
}
function makeStopList2(html) 
{
	htmlnode = document.createTextNode(html);
	linknode = document.createElement("a");
	linknode.setAttribute("href", "index.php?name="+html.replace(/ /g,"%20"));
	linknode.appendChild(htmlnode);

	linode = document.createElement("li");
	linode.appendChild(linknode);
	document.getElementById("stopList2").appendChild(linode);
	//document.getElementById("footer").innerHTML = activeStopList.length;
}

function processReqRouteStops()
{	
	// only if req shows "loaded"
	if (reqRouteStops.readyState == 4 && reqRouteStops.status == 200)
	{
		var stops = reqRouteStops.responseXML.documentElement.getElementsByTagName("stop");
		var routeStops = new Array();
		for(i = 0; i < stops.length; i++)
		{
			stopRoutes = stops[i].getElementsByTagName("route");
			
			for(k = 0; k < stopRoutes.length; k++)
			{
				if(stopRoutes[k].childNodes[0].nodeValue == routeName)
				{
					routeStops.push(stops[i]);
				}
			}
			/*if(routes[i].getElementsByTagName("name")[0].childNodes[0].nodeValue == routeName)
			{
				stops = routes[i].getElementsByTagName("stop");
			}*/
		}
		removeAllStops();
		drawStops(routeStops);
	}
}

function processReqChangeStops() 
{
	// only if req shows "loaded"
	if (req2.readyState == 4 && req2.status == 200) 
	{
		var xmlDoc2 = req2.responseXML;
		markers = xmlDoc2.documentElement.getElementsByTagName("stop");
		removeAllStops();
		drawStops(markers);
	}
}

function processReqChangeStops2() 
{
	// only if req shows "loaded"
	if (req2.readyState == 4 && req2.status == 200) 
	{
		var xmlDoc2 = req2.responseXML;
		markers = xmlDoc2.documentElement.getElementsByTagName("stop");
		removeAllStops();
		drawStops2(markers);
	}
}

function removeAllStops()
{
	var count = map.getMarkerCount();

	var marker = map.getFirstMarker();
	while (marker != null)
	{
		if(marker.getUserData() == "stop")
		{
			map.removeOverlay(marker);
			//delete marker;	
		}
		marker = map.getNextMarker();
	}

	return count;
}

function drawStops(markers)
{
	var stopListArray = Array();
	
	for(i = 0; i < markers.length; i++)
	{
		if(document.getElementById("mapdiv") != null)
		{
			if(markers[i].getElementsByTagName("lat")[0].childNodes[0].nodeValue!=null && markers[i].getElementsByTagName("lon")[0].childNodes[0].nodeValue!=null && stopicon!=null)
			{
				busStop = new PdMarker(
					new GLatLng(parseFloat(markers[i].getElementsByTagName("lat")[0].childNodes[0].nodeValue),
					parseFloat(markers[i].getElementsByTagName("lon")[0].childNodes[0].nodeValue)), 
					stopicon
					);
				busStop.setUserData("stop");
				//if(markers[i])
				if( markers[i].getElementsByTagName("name3")[0].childNodes[0].nodeValue == "None")
					addStopInfo(busStop, markers[i].getElementsByTagName("name2")[0].childNodes[0].nodeValue,"");
				else
				{
					addStopInfo(busStop, markers[i].getElementsByTagName("name2")[0].childNodes[0].nodeValue ,markers[i].getElementsByTagName("name3")[0].childNodes[0].nodeValue);
				}
				map.addOverlay(busStop);
			}
			else
			{
				//alert("stop:/n" + markers[i]);
			}
		}
		
		if(document.getElementById("stopList") != null)
		{
/*			if(markers[i].getElementsByTagName("name3")[0].childNodes[0].nodeValue != "None")
			{*/
				stopListArray[i] = markers[i].getElementsByTagName("name2")[0].childNodes[0].nodeValue;
				// makeStopList(markers[i].getElementsByTagName("name3")[0].childNodes[0].nodeValue);
			/*}
			else if (markers[i].getElementsByTagName("name2")[0].childNodes[0].nodeValue != "")
			{
				stopListArray[i] = markers[i].getElementsByTagName("name2")[0].childNodes[0].nodeValue;
				// makeStopList(markers[i].getElementsByTagName("name2")[0].childNodes[0].nodeValue);
			}*/
		}
	}
	stopListArray = stopListArray.unsortedRemoveDuplicates();
	stopListArray = stopListArray.sort();
	for(i = 0; i < stopListArray.length; i++)
	{
		makeStopList(stopListArray[i]);
	}
	
		
	/*mbus = new GMarker(new GPoint(-83.711813,42.294403));
	map.addOverlay(mbus);
	
	mbus.openInfoWindowHtml("Magic Bus Arrivals<br>Click a stop to view its arrivals");*/

}


function drawStops2(markers)
{
	var mergedStopList = Array();
	
	for(i = 0; i < markers.length; i++)
	{		
		if(document.getElementById("stopList2") != null)
		{
				mergedStopList[i] = markers[i].getElementsByTagName("name2")[0].childNodes[0].nodeValue;
		}
	}
	mergedStopList = mergedStopList.unsortedRemoveDuplicates();
	mergedStopList = mergedStopList.sort();
	for(i = 0; i < mergedStopList.length; i++)
	{
		makeStopList2(mergedStopList[i]);
	}

}

function getStops()
{
	loadXMLDocStops("../../shared/stops.xml");
}

function getRouteStops(theRouteName)
{
	routeName = theRouteName;
	loadXMLDocRouteStops("../../shared/stops.xml");
}
