//EXODUS main initialization and output
//John Driftmier, http://www.treenumbertwo.com
//January 2009
//
//There aren't any functions in here. This line takes the functions in the header, sets up variables, writes it out to the screen, and sets it on a refresh loop. It goes after the last div in the main index page.

//Start to initialize functions and variables------------------------------------------------------
//-------------------------------------------------------------------------------------------------

//This gets the day of the week to load the proper bus schedule. King County Metro has three different schedules: Weekday, Saturday, and Sunday.
currenttime = new Date();
if (currenttime.getDay() == 0) {var busfilename = "xml/busSunday.xml";}
if (currenttime.getDay() == 6) {var busfilename = "xml/busSaturday.xml";}
if (currenttime.getDay() == 1 || currenttime.getDay() == 2 || currenttime.getDay() == 3 || currenttime.getDay() == 4 || currenttime.getDay() == 5) {var busfilename = "xml/busWeekday.xml";}

//time to load the XML files for the page. Each function has its data on its own XML file as seen below, and XML is initialized on a different way for IE and Firefox/Safari, you know, all of the proper browsers.
var eventsXML;
var passesXML;
var busXML;


//This function uses XMLHttpRequest to load the XML with cross-browser compatibility.
//(Safari won't use .load, which is a shame.)
function loadXMLDoc(xmlUrl) {
var req = new XMLHttpRequest();
req.open('GET', xmlUrl, false); 
req.send(null);
if(req.status == 200)
  return req.responseXML;
} 

try //Internet Explorer
  {
  eventsXML=new ActiveXObject("Microsoft.XMLDOM");
  passesXML=new ActiveXObject("Microsoft.XMLDOM");
  busXML=new ActiveXObject("Microsoft.XMLDOM");
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    eventsXML=document.implementation.createDocument("","",null);
	passesXML=document.implementation.createDocument("","",null);
	busXML=document.implementation.createDocument("","",null);
    }
  catch(e) {alert(e.message)}
  }
try //load the XML files
  {
	eventsXML.async=false;
	eventsXML = loadXMLDoc("xml/events.xml");
	passesXML.async=false;
	passesXML = loadXMLDoc("xml/passes.xml");
	busXML.async=false;
	busXML = loadXMLDoc(busfilename);
  }
catch(e) {alert(e.message)}

//Useful shortcut for later.
var x=eventsXML.documentElement.childNodes;

//Google Maps Variables---------------------------   
// arrays to hold copies of the markers used by the ticker
var gmarkers = [];
// create the map
var map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());
//map.addControl(new GMapTypeControl());
//initializes the map to SPU, the zoom level, and the hybrid map type
map.setCenter(new GLatLng(47.649637,-122.360875), 14, G_HYBRID_MAP);     

//Scroller Variables-----------------------------
//scroller width
var swidth=500;
//scroller height
var sheight=150;
//background color
var sbcolor='';
//scroller's speed
var sspeed=2;
var msg=''
var resumesspeed=sspeed
//googlemaps variable for initializing the XML, not needed but useful shortcut.
var googlemaps = eventsXML.documentElement.getElementsByTagName("googlemap");


//Start to write everything out to the page--------------------------------------------------------
//-------------------------------------------------------------------------------------------------

//Creating the text for the message ticker.
//This grabs everything out of events.xml and parses if for the google maps API and places it in the ticker
for (var i=0;i<x.length;i++)
{ 
  // obtain the attribues of each marker
  var lat = parseFloat(googlemaps[i].getAttribute("lat"));
  var lng = parseFloat(googlemaps[i].getAttribute("lng"));
  var point = new GLatLng(lat,lng);
  //long line, it gets the the childNodes from the googlemaps and places them into formatting for the map pointer
  //if you don't declare the width and height, google tends to add 4 times what you need, which is not ideal for a small map.
  var html = '<div style="width:300px; min-height:75px;"><p>' + googlemaps[i].childNodes[0].childNodes[0].nodeValue + '</p><br /><p><b>' + googlemaps[i].getAttribute("label") + '</b></p><p>' + googlemaps[i].childNodes[1].childNodes[0].nodeValue + '</p><p>' + googlemaps[i].childNodes[2].childNodes[0].nodeValue + '</p></div>';
  var label = googlemaps[i].getAttribute("label");
  //This function (as seen above) creates the marker for Google Maps
  var marker = createMarker(point,label,html);
  // this next line adds a line to the ticker html. once everything's been parsed from the XML, then msg will become the HTML inside the scrolling ticking function. Note that the div is tied to another JS function that randomizes the list every page load (though not every scrollthough), otherwise everyone would always see the same 5-10 events before leaving the page.
	msg += '<div class="randomscroll group1"><a href="javascript:myclick(' + (gmarkers.length-1) + ')"><span id="yellow">' + x[i].childNodes[0].childNodes[0].childNodes[0].nodeValue + '</span><br />' +  x[i].childNodes[0].childNodes[1].childNodes[0].nodeValue + '<\/a></div><br />';
  map.addOverlay(marker);
}

//initialize bus schedules
displaytime = new Date();
displaytime = updateschedule(displaytime,1);

//this function initialize and write pass availibility
writeAvail();

//start Events Ticker
start();
//randomize Events Ticker, otherwise everyone will inevitably see the same first ten
//events every time the page loads. Let the others have some face time.
randomordercontentdisplay.init();

//Set up the bus time check every second
setInterval("displaytime = updateschedule(displaytime,0)",1000);
//Set up the Pass Availability check every 30 seconds
setInterval("writeAvail()",30000);