
// arrays to hold copies of the markers used by the side_bar
	// because the function closure trick doesnt work there
	var gmarkers = [];
// This function picks up the click and opens the corresponding info window
	function myclick(i) {
		GEvent.trigger(gmarkers[i], "click");
	}
function loadMap(page){
	if (GBrowserIsCompatible()) {
	// this variable will collect the html which will eventually be placed in the side_bar
	var side_bar_html = '<h2>Catch the Tour</h2><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td colspan="2" width="100%" style="height:4px; line-height:4px;background-image:URL(/images/newshr.gif)">&nbsp;</td></tr>';
	var old_header = ''
	var old_side_bar_html = '<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td colspan="2" width="100%" style="height:4px; line-height:4px;background-image:none;">&nbsp;</td></tr>';
	var nextstopid;
	var today = new Date();
	var foundit = false;
	var closestTourDate = new Date()
	closestTourDate.setFullYear(1990,1,1)
	var temp = new Date();
	temp.setFullYear(1990,2,1)
	var id;
	var nextstophtml = "";
		
	
	
	var i = 0;
	// This icon uses the same shape as the default Google marker
	// So we can use its details for everything except the image 
	var oldIcon = new GIcon();
	oldIcon.image = "/images/oldevent.png";
	oldIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	oldIcon.iconSize = new GSize(20, 34);
	oldIcon.shadowSize = new GSize(37, 34);
	oldIcon.iconAnchor = new GPoint(9, 34);
	oldIcon.infoWindowAnchor = new GPoint(9, 2);
	oldIcon.infoShadowAnchor = new GPoint(18, 25);
	oldIcon.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
		
	var upcomingIcon = new GIcon();
	upcomingIcon.image = "/images/upcoming.png";
	upcomingIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	upcomingIcon.iconSize = new GSize(20, 34);
	upcomingIcon.shadowSize = new GSize(37, 34);
	upcomingIcon.iconAnchor = new GPoint(9, 34);
	upcomingIcon.infoWindowAnchor = new GPoint(9, 2);
	upcomingIcon.infoShadowAnchor = new GPoint(18, 25);
	upcomingIcon.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
	
	
								
								
								

	// A function to create the marker and set up the event window
	function createMarker(point,name,html,nextstoplocal,date, markerid, link,locName,locphone,locAddress, datelabel, city, state, zip) {
		var directions = ""
		
//		nextstophtml ="<b>" + datelabel + "</b><br>" +  nextstoplocal + "<br>" + locName + "<br>"  + locAddress + "<br>" + city + ", " + state + " " + zip + "<br>" + locphone;

		if (closestTourDate > date){
			var marker = new GMarker(point, oldIcon);
			// add a line to the old side_bar html
			var old_header = '<h2>Previous Old Tour Stops</h2>'
			//old_side_bar_html += '<tr><td width="18"><IMG SRC="/images/old_list_style_green.gif" width="14" height="9" alt=""></td><td class="rightnavtext"><a href="javascript:myclick(' + i + ')">' + name + '</a></td></tr><tr><td width="18"></td><td>' + datelabel + '</td></tr><tr><td colspan="2" width="100%" style="height:4px; line-height:4px;background-image:URL(/images/newshr.gif)">&nbsp;</td></tr>';
		}else if (closestTourDate <= date){
			if(markerid==id){
				//var marker = new GMarker(point);
				// add a line to the side_bar html
				//side_bar_html += '<tr><td width="18"><IMG SRC="/images/list_style_green.gif" width="14" height="9" alt=""></td><td class="rightnavtext"><a href="javascript:myclick(' + i + ')">' + name + '</a></td></tr><tr><td width="18"></td><td>DATE</td></tr><tr><td colspan="2" width="100%" style="height:4px; line-height:4px;background-image:URL(/images/newshr.gif)">&nbsp;</td></tr>';
				var marker = new GMarker(point, upcomingIcon);
				// add a line to the side_bar html
				side_bar_html += '<tr><td width="18"><IMG SRC="/images/list_style_green.gif" width="14" height="9" alt=""></td><td class="rightnavtext"><a href="javascript:myclick(' + i + ')">' + name + '</a></td></tr><tr><td width="18"></td><td>' + datelabel + '</td></tr><tr><td colspan="2" width="100%" style="height:4px; line-height:4px;background-image:URL(/images/newshr.gif)">&nbsp;</td></tr>';
				nextstopid = i;
				document.getElementById("nextstop").innerHTML = nextstophtml;
			}else{
				var marker = new GMarker(point, upcomingIcon);
				// add a line to the side_bar html
				side_bar_html += '<tr><td width="18"><IMG SRC="/images/list_style_green.gif" width="14" height="9" alt=""></td><td class="rightnavtext"><a href="javascript:myclick(' + i + ')">' + name + '</a></td></tr><tr><td width="18"></td><td>' + datelabel + '</td></tr><tr><td colspan="2" width="100%" style="height:4px; line-height:4px;background-image:URL(/images/newshr.gif)">&nbsp;</td></tr>';
			}
		}
		html +="<h2 class='Gpopup'>" + locName+"</h2>"   
		html +="<p class='Gpopup'>" + locAddress + " " + city + ", " + state + " " + zip
		html +="<br>" + locphone
		html +="<h5 class='Gpopup'>Enter your address to get directions to this location</h5>"
		html+="<form action='http://maps.google.com/maps' method='get'>"
		html+="		<input type='text' name='saddr' id='saddr' value='' /><br />"
		html+='		<input type="image" src="/images/but_getdir.gif" onclick="this.form.target='+"'_blank'"+';return true;"  />'
		html+="		<input type='hidden' name='daddr' value='"+locAddress + " " + city + ", " + state + " " + zip +"' />"
		html+="		<input type='hidden' name='hl' value='en' /><div>&nbsp;</div>"
		html+="</form><div>&nbsp;</div></p>"
		//html +="<br>" + locName   
		//html +="<br>" + locAddress
		//html +="<br>" + locphone  
		//html += "<br><a href='"+ link +"'>Get Directions</a>"
		GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
		});
		// save the info we need to use later for the side_bar
		gmarkers[i] = marker;
		
		i++;
		return marker;
	}

	function opennextstopinfo(){	
		GEvent.trigger(gmarkers[nextstopid], "click");
	}
	
//function loadPage(page) {
	// create the map
	var map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng( 37.4419,-92.1419), 3);

	// Read the data from data.xml
	var request = GXmlHttp.create();
	request.open("GET", "/artery-explorer/data.xml", true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
	
		var isPrintPreview = (window.location.search.indexOf('print=true') >= 0 ? true : false);
		var xmlDoc = GXml.parse(request.responseText);
		// obtain the array of markers and loop through it
		var markers1 = xmlDoc.documentElement.getElementsByTagName("marker");
		var markers = new Array();
		//var c = 0;
		//find the next tour stop
		for (var i = 0; i < markers1.length; i++) {
			// obtain the attribues of each marker          
			var day = markers1[i].getAttribute("day");
			var month = markers1[i].getAttribute("month");
			var year = markers1[i].getAttribute("year");
			
			temp.setFullYear(parseInt(year),parseInt(month),parseInt(day));
			
			if (temp > today) {
				markers.push(markers1[i]);
			}
			
			if (closestTourDate < temp && foundit==false){
				if(temp > today){
					closestTourDate.setFullYear(parseInt(year),parseInt(month),parseInt(day));
					foundit = true;
					id = markers1[i].getAttribute("id");	
					nextstophtml = "<b>" + markers1[i].getAttribute("datelabel") + "</b><br>" +  markers1[i].getAttribute("html") + "<br>" + markers1[i].getAttribute("locName") + "<br>"  + markers1[i].getAttribute("locAddress") + "<br>" + markers1[i].getAttribute("city") + ", " + markers1[i].getAttribute("state") + " " + markers1[i].getAttribute("zip") + "<br>" + markers1[i].getAttribute("locphone");
					document.getElementById("nextstop").innerHTML = nextstophtml;
				}
			}           
		}
		
		if (isPrintPreview) {
			var itemsPerPage = 6000;
		} else {
			var itemsPerPage = 6;
		}
		var pageCount = Math.ceil(markers.length / itemsPerPage);
		if (page <= 0 || page === undefined ) {
			page = 1;
		} else if (page > pageCount) {
			page = pageCount;
		}

	if (!isPrintPreview) {
		$('#pagingcontainer').html('<a onclick="loadMap(' + (page - 1) + ');"><img style="display: inline;" src="/images/global/left_arrow.png" /></a> <input type="input" value="' + page + '" style="width: 15px;" onfocus="this.select();" onkeypress="(event.which == 13 ? loadMap(this.value) : eval());" /> of ' + pageCount + ' <a onclick="loadMap(' + (page + 1) + ');"><img src="/images/global/right_arrow.png" style="display: inline;" /></a>');
	}
		var firstOnPage = false;

		for (var i = 0; i < markers.length; i++) {
			if (!(((i+1) <= (itemsPerPage * page)) && ((i+1) >= (itemsPerPage * page - (itemsPerPage - 1))))) {
				continue;
			}
			if (!firstOnPage) {
				firstOnPage = true;
				id = markers[i].getAttribute("id");	
			}
			
			// obtain the attribues of each marker
			var lat = parseFloat(markers[i].getAttribute("lat"));
			var lng = parseFloat(markers[i].getAttribute("lng"));
			var point = new GLatLng(lat,lng);
			var html = "<h4 class='Gpopup'>"+markers[i].getAttribute("html")+"</h4>";
			var nextstoplocal = markers[i].getAttribute("html");
			var label = markers[i].getAttribute("label");
			var year = markers[i].getAttribute("year");
			var month = markers[i].getAttribute("month");
			var day = markers[i].getAttribute("day");
			var date = new Date();
			var markerid = markers[i].getAttribute("id");
			var link = markers[i].getAttribute("link");
			var locAddress = markers[i].getAttribute("locAddress");
			var locphone = markers[i].getAttribute("locphone");
			var locName = markers[i].getAttribute("locName");
			var datelabel = markers[i].getAttribute("datelabel");
			var city = markers[i].getAttribute("city");
			var state = markers[i].getAttribute("state");
			var zip = markers[i].getAttribute("zip");
			date.setFullYear(parseInt(year),parseInt(month),parseInt(day));
			
			// create the marker
			var marker = createMarker(point,label,html,nextstoplocal,date, markerid, link,locName,locphone,locAddress, datelabel, city, state, zip);
			map.addOverlay(marker);
		}
		// put the assembled side_bar_html contents into the side_bar div
		side_bar_html += '</table>'
		old_side_bar_html +='</table>'
		document.getElementById("side_bar").innerHTML = side_bar_html;
		document.getElementById("old_side_bar").innerHTML = old_header + old_side_bar_html;
		
		opennextstopinfo();
		}
	}
		
	request.send(null);
	
//		}
	//	loadPage(1);
	}

	else {
	alert("Sorry, the Google Maps API is not compatible with this browser");
	}
}
//loadMap(1);


function LASTMINUTEFUDGE(){

FollowURL(2,'http://www.spacecenter.org/CurrentExhibit.html')

}

