//Google Maps API and Events Ticker JS Functions
//John Driftmier, http://www.treenumbertwo.com
//January 2009
//
//This document holds the functions used to get the bus and pass availability information
//and then post them to the spans on the main page. Fairly simple.


//Turns the JS function Date() into an easier to manage minutes from midnight.
//Thus, 6:30AM turns into (6*60)+30 = 690.
//The XML files for the bus schedules are ALL in this format.
function minutescalc(currentTime){
	var minutes = currentTime.getMinutes()
	minutes += currentTime.getHours() * 60;
	return minutes;
}


/*function openbusXML(busfilename)
{
	try //Internet Explorer
	  {
	  busXML=new ActiveXObject("Microsoft.XMLDOM");
	  }
	catch(e)
	  {
	  try //Firefox, Mozilla, Opera, etc.
		{
		busXML=document.implementation.createDocument("","",null);
		}
	  catch(e) {alert(e.message)}
	  }
	try 
	  {
		busXML.async=false;
		busXML.load(busfilename);
	  }
	catch(e) {alert(e.message)}
}*/


//this reloads the Pass Availabilty XML, writes it to page.
//Note the XML file is just a single Boolean operator -- 0 if false (not available) or 1 if true (available)
function writeAvail()
{
	try //load the XML file again, see if it's changed
	  {
		passesXML.async=false;
		passesXML.load("xml/passes.xml");
	  }
	catch(e) {alert(e.message)}
	
	//initialize and write pass availibility, fairly simple IF conditional
	if (passesXML.documentElement.childNodes[0].nodeValue == 1) {document.getElementById('passes').innerHTML = "AVAILABLE ";}
	else if (passesXML.documentElement.childNodes[0].nodeValue == 0) {document.getElementById('passes').innerHTML = "NOT AVAILABLE ";}
	else {document.getElementById('passes').innerHTML = "NO DATA ";}
}


//--------------------------------------------------------------------------------------------------------------------------
//This is the big 'un, ladies and gentlemen. This is what calculates the bus schedules and displays them to the screen.
//Let's go through the steps one by one.
//First, the function gets the last display time, along with the override value. If that override value is true(1), then it's updated regardless of time because it's probably the first time running.
//Second, the function gets the current time and does a conditional check. If the current time and the display time are correct (in minutes) then there's no need to update the function again, it exits. But if it's different, then it needs to update the screen again.
//The function then goes through each of the 5 buses. It finds the correct bus (the first childNode) and then goes through all of the value until it finds the one right after the current time. It then calculates the next bus arrival minus the current time to find out how many minutes are left until the next bus shows up.
//If it's one minute, there's a conditional statement to make the text "minute" instead of "minutes" (details, details.) It then displays it to the screen by editing the HTML inside the respective <span> value on the main page.
//It does this for all possible values (true, this could have been written more elegantly, but there it is) and then updates the new time.
//----------------------------------------------------------------------------------------------------------------------------

function updateschedule(functiondisplaytime, override) {
currenttime = new Date();

if (minutescalc(functiondisplaytime) != minutescalc(currenttime) || override == 1){
for (var i=0;i<busXML.documentElement.childNodes[0].childNodes.length;i++)
{
	if (busXML.documentElement.childNodes[0].childNodes[i].childNodes[0].nodeValue >= minutescalc(currenttime))
	{
		if (busXML.documentElement.childNodes[0].childNodes[i].childNodes[0].nodeValue - minutescalc(currenttime) == 1)
			{document.getElementById('b13').innerHTML = busXML.documentElement.childNodes[0].childNodes[i].childNodes[0].nodeValue - minutescalc(currenttime) + " MINUTE";}
		else 
			{document.getElementById('b13').innerHTML = busXML.documentElement.childNodes[0].childNodes[i].childNodes[0].nodeValue - minutescalc(currenttime) + " MINUTES";}	
		break;
	}
}

for (var i=0;i<busXML.documentElement.childNodes[1].childNodes.length;i++)
{
	if (busXML.documentElement.childNodes[1].childNodes[i].childNodes[0].nodeValue >= minutescalc(currenttime))
	{
		if (busXML.documentElement.childNodes[1].childNodes[i].childNodes[0].nodeValue - minutescalc(currenttime) == 1)
			{document.getElementById('b17ballard').innerHTML = busXML.documentElement.childNodes[1].childNodes[i].childNodes[0].nodeValue - minutescalc(currenttime) + " MINUTE";}
		else 
			{document.getElementById('b17ballard').innerHTML = busXML.documentElement.childNodes[1].childNodes[i].childNodes[0].nodeValue - minutescalc(currenttime) + " MINUTES";}
		break;
	}
}

for (var i=0;i<busXML.documentElement.childNodes[2].childNodes.length;i++)
{
	if (busXML.documentElement.childNodes[2].childNodes[i].childNodes[0].nodeValue >= minutescalc(currenttime))
	{
		if (busXML.documentElement.childNodes[2].childNodes[i].childNodes[0].nodeValue - minutescalc(currenttime) == 1)
			{document.getElementById('b17seattle').innerHTML = busXML.documentElement.childNodes[2].childNodes[i].childNodes[0].nodeValue - minutescalc(currenttime) + " MINUTE";}
		else 
			{document.getElementById('b17seattle').innerHTML = busXML.documentElement.childNodes[2].childNodes[i].childNodes[0].nodeValue - minutescalc(currenttime) + " MINUTES";}
		break;
	}
}

for (var i=0;i<busXML.documentElement.childNodes[3].childNodes.length;i++)
{
	if (busXML.documentElement.childNodes[3].childNodes[i].childNodes[0].nodeValue >= minutescalc(currenttime))
	{
		if (busXML.documentElement.childNodes[3].childNodes[i].childNodes[0].nodeValue - minutescalc(currenttime) == 1)
			{document.getElementById('b31magnolia').innerHTML = busXML.documentElement.childNodes[3].childNodes[i].childNodes[0].nodeValue - minutescalc(currenttime) + " MINUTE";}
		else 
			{document.getElementById('b31magnolia').innerHTML = busXML.documentElement.childNodes[3].childNodes[i].childNodes[0].nodeValue - minutescalc(currenttime) + " MINUTES";}
		break;
	}
}

for (var i=0;i<busXML.documentElement.childNodes[4].childNodes.length;i++)
{
	if (busXML.documentElement.childNodes[4].childNodes[i].childNodes[0].nodeValue >= minutescalc(currenttime))
	{
		if (busXML.documentElement.childNodes[4].childNodes[i].childNodes[0].nodeValue - minutescalc(currenttime) == 1)
			{document.getElementById('b31udist').innerHTML = busXML.documentElement.childNodes[4].childNodes[i].childNodes[0].nodeValue - minutescalc(currenttime) + " MINUTE";}
		else 
			{document.getElementById('b31udist').innerHTML = busXML.documentElement.childNodes[4].childNodes[i].childNodes[0].nodeValue - minutescalc(currenttime) + " MINUTES";}
		break;
	}
}

functiondisplaytime = new Date();
}
//end if statement
return functiondisplaytime;
}