$(document).ready(function(){
													 
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(40.45275, -80.16857), 15);
		map.setUIToDefault();
	}
	var lastLocationText = "";
	var markerCounter = 0;
	var rteNameCounter = 0;
	var currentRteName = "";
		
	map.addOverlay(
		createMarker(
			new GLatLng(40.452223,-80.167675),
			"../images/icons/ikea_icon.png",
			new GSize(91,56)
		)
	);
	
	map.addOverlay(
		createMarker(
			new GLatLng(40.45271,-80.160143),
			"../images/icons/mall_icon.png",
			new GSize(91,56)
		)
	);
	
	map.addOverlay(
		createMarker(
			new GLatLng(40.430459,-80.133608),
			"../images/icons/settlers_icon.png",
			new GSize(91,56)
		)
	);
	
	map.addOverlay(
		createMarker(
			new GLatLng(40.438287,-80.154809),
			"../images/icons/bayer_icon.png",
			new GSize(91,56)
		)
	);

	map.addOverlay(
		createMarker(
			new GLatLng(40.450855,-80.184232),
			"../images/icons/walmart_icon.png",
			new GSize(91,56)
		)
	);

	map.addOverlay(
		createMarker(
			new GLatLng(40.456102,-80.19059),
			"../images/icons/mainstay_icon.png",
			new GSize(91,56)
		)
	);
	
	map.addOverlay(
		createMarker(
			new GLatLng(40.511858,-80.198665),
			"../images/icons/ridc_icon.png",
			new GSize(91,56)
		)
	);
		
 
	$.getJSON(
		"http://www.myaccessride.com/JSONServices/ACTAService.aspx?rte=" + rte + "&jsonp=?",
		function(json) {
			$.each(json.points, function(i,item){
				if (currentRteName != item.rteName) {
					markerCounter = 0;
					rteNameCounter++;
					currentRteName = item.rteName;
				}
				if (lastLocationText != item.locationText) {
					markerCounter++;
					lastLocationText = item.locationText;
					var icon = new GIcon(G_DEFAULT_ICON);
					icon.image = "../images/icons/icon-marker-" + rteNameCounter + "-" + markerCounter + ".png";
					var marker = new GMarker(
						new GLatLng(item.lat, item.lon),
						{icon: icon}
					);
					
					var timeArray = item.timeStamp.split(':');
					var hour = parseInt(timeArray[0]);
					var ampm = "am";
					if (hour >= 12) ampm = "pm"
					if (hour > 12) hour = hour - 12;
					else hour = timeArray[0];
					var newTimeStamp = hour + ":" + timeArray[1] + ":" + timeArray[2] + ampm;
					
					var infoHtml =
						'<div style="max-height: 75px;"><p>At ' + newTimeStamp +
						' bus ' + currentRteName + ' was at the following location:</p><p><b>' +
						item.locationText
						'</b></p></div>';
					map.addOverlay(marker);
					marker.bindInfoWindowHtml(infoHtml);
					if (i == 0) {
						marker.openInfoWindow(infoHtml);
						map.setCenter(new GLatLng(item.lat, item.lon), 15);
					}
				}
			});
		}
	);
});

function createMarker(point,icon_image, size) {
	var icon = new GIcon(G_DEFAULT_ICON);
	icon.image = icon_image;
	icon.iconSize = size;
	var marker = new GMarker(
		point,
		{icon: icon}
	);
	return marker;
}
