function loadmap() {
	var map = new GMap2(document.getElementById("map_canvas"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(41.875696, -87.624207), 12);
	map.enableScrollWheelZoom();
	return map;
}

//Given the stacks marks and points, fills *marks with GMarker objects
function loadmarks(marks, points, runnermarks, incentivemarks) {
	var temppoint = points.pop();
	var temppoint2 = points.pop();
	var distance = 0;
	var distancebuffer = 0;

	//sort marks
	marks.sort(sortMarksByDist);

	var tempmark = marks.pop();
	while(points.length > 0 && tempmark != null) {
		distancebuffer = temppoint.distanceFrom(temppoint2);
		distance = distance + distancebuffer;

		if(distance < tempmark.dist) {
			temppoint = temppoint2;
			temppoint2 = points.pop();
		} else {
			var tempdist = tempmark.dist;
			distance = distance - distancebuffer;
			var meterremainder = tempmark.dist - distance;
			
			//coordinates
			var lat = temppoint.lat() + meterremainder / temppoint.distanceFrom(temppoint2) * (temppoint2.lat() - temppoint.lat());
			var lng = temppoint.lng() + meterremainder / temppoint.distanceFrom(temppoint2) * (temppoint2.lng() - temppoint.lng());
			
			//the look
			var markerIcon = new GIcon(G_DEFAULT_ICON);
			markerIcon.image = tempmark.icon;
			var markerOptions = { icon:markerIcon };
			var tempgmarker = new GMarker(new GLatLng(lat,lng), markerOptions);
			//we are going to store these as tabs.
			//if there is only one thing at this location,
			//then the tab will work as a GInfoWindow
			//otherwise we'll display tabs for each thing, with
			//the first icon being the one we use

			//in pratice this isn't that big a deal, since usually
			//it's a lot of runners
			var infowindowstring = "";
			var originaltype = tempmark.type;

			do {
				infowindowstring += tempmark.infowindow + "<br/>";
				tempmark = marks.pop();
				if(!tempmark) break;
			}while (tempmark.dist == tempdist);
			tempgmarker.bindInfoWindowTabs(infowindowstring);

			switch(originaltype) {
				case 'runner':
					runnermarks.push(tempgmarker);
					break;
				case 'incentive':
					incentivemarks.push(tempgmarker);
					break;
				default:
					//erm?
					break;
			}
			if(!tempmark) break; //break again
		}
	}
}

function sortMarksByDist(a, b) {
	var x = a.dist;
	var y = b.dist;
	//not sure why this is reversed
	return (-1)*((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function addmarkers(markers,map) {
	jQuery.each(markers,function() {
		map.addOverlay(this);
	});
}

function togglemarkers(markers) {
	jQuery.each(markers,function() {
		if(this.isHidden()) this.show();
		else this.hide();
	});
}
