

function loadXMLDoc(dname) {
	var xmlDoc;
	if (window.XMLHttpRequest){
		xmlDoc=new window.XMLHttpRequest();
		xmlDoc.open("GET",dname,false);
		xmlDoc.send("");
		return xmlDoc.responseXML;
	}	
}

function createLink(p1, p2){
	
	lat2 = Math.PI/180*p2.lat();
	lat1 = Math.PI/180*p1.lat();
	lon2 = Math.PI/180*p2.lng();
	lon1 = Math.PI/180*p1.lng();
	var R = 6371; // km
	var dLat = (lat2-lat1);
	var dLon = (lon2-lon1); 
	var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
	        Math.cos(lat1) * Math.cos(lat2) * 
	        Math.sin(dLon/2) * Math.sin(dLon/2); 
	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
	var d = R * c;
	return [p1,p2,d];
}

function createStation(point, msg) {


	
	markerOptions = {icon:ico, zIndex:2};

	
	var marker = new GMarker(point,markerOptions);
	GEvent.addListener(marker, "click", function(){marker.openInfoWindowHtml(msg);});
	return marker;
}

function createBus(){
	
	
	
	//bus has a link, and a fraction along a route
	
	l = 0;
	f = 0;
	
	var lat = links[l][0].lat() + f*(links[l][1].lat()-links[l][0].lat());
	var lng = links[l][0].lng() + f*(links[l][1].lng()-links[l][0].lng());

	markerOptions = {icon:bico, zIndex:2};
	var point = new GLatLng(lat,lng);
	var bus = new GMarker(point,markerOptions);	
	map.addOverlay(bus);
	
	return [l,f,bus];
}

function move(bus){
	
	//alert("move");
	
	var l = bus[0];
	var f = bus[1]+.55/links[l][2];
	bus[2].remove();
	if (f>1){f=0;l=l+1;}
	if (l==24){f=0;l=0;}
	
	var lat = links[l][0].lat() + f*(links[l][1].lat()-links[l][0].lat());
	var lng = links[l][0].lng() + f*(links[l][1].lng()-links[l][0].lng());
	
	markerOptions = {icon:bico, zIndex:2};
	var point = new GLatLng(lat,lng);
	var bus = new GMarker(point,markerOptions);	
	map.addOverlay(bus);
	return [l,f,bus];
	
	
}




function launchmap(map){ //initialize map

	map.setCenter(new GLatLng(37.779224, -122.313831), 11);
	map.setUIToDefault();
	
}

function launchlines(map,lats,lngs){ //initialize BART lines
	
	links = new Array();
	
	
	
	for(i=0;i<lats.length-1;i=i+1){
		s1 = new GLatLng(lats[i].childNodes[0].nodeValue,lngs[i].childNodes[0].nodeValue);
		s2 = new GLatLng(lats[i+1].childNodes[0].nodeValue,lngs[i+1].childNodes[0].nodeValue);
		links[i]=createLink(s1,s2);		
		map.addOverlay(new GPolyline([s1,s2],"#c43fc8",25));
		map.addOverlay(createStation(s1,"Station"));
		
	}

		
	map.addOverlay(createStation(s2,"Station"));


}

function windowHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return (myHeight);
}

function windowWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return (myWidth);
}

function sizeWindow() {
	windowheight = windowHeight();
	windowwidth = windowWidth();
	mapframe = document.getElementById("map_canvas");
	mapframe.style.height = (windowheight - 80) + "px";	
}



function initialize() {
  if (GBrowserIsCompatible()) {
	
	ico = new GIcon(G_DEFAULT_ICON);
	ico.image = "images/bart.png";
	ico.iconSize = new GSize(20, 20);
	ico.iconAnchor = new GPoint(10, 10);
	ico.infoWindowAnchor = new GPoint(10, 10);
	ico.shadowSize = new GSize(0, 0);

	
	bico = new GIcon(G_DEFAULT_ICON);
	bico.image = "bus.png";
	bico.iconSize = new GSize(20, 20);
	bico.iconAnchor = new GPoint(10, 10);
	bico.infoWindowAnchor = new GPoint(10, 10);
	bico.shadowSize = new GSize(0, 0);
	
	
	busses = new Array();
	n=1;

	//Read XML Docs    	
	
	links=loadXMLDoc("links.xml");
	stat_lat_long=loadXMLDoc("pbp.xml");
	
	//data
	
	lookup_abbr = stat_lat_long.getElementsByTagName("Abbr");
	lats = stat_lat_long.getElementsByTagName("Lat");
	lngs = stat_lat_long.getElementsByTagName("Long");
	/*link_list = links.getElementsByTagName("link");
	starts = links.getElementsByTagName("startlatlong");
	ends = links.getElementsByTagName("endlatlong");
	colors = links.getElementsByTagName("color");*/

	
	launchmap(map);
	
	launchlines(map,lats,lngs);
	
	busses[0] = createBus();
	busfeed = setInterval(function (){busses[n] = createBus(); 
			n++;		
			if(n==10) clearInterval(busfeed);
			//console.log(n);
			},1000);


	setInterval(function (){for (j=0;j<busses.length;j++){busses[j] = move(busses[j]);}},250);

	sizeWindow();
	
}}
