function init_map() {
	markers=["http://www.loadmax.com/images/icons/lm_custom_start.png",
		"http://www.loadmax.com/images/icons/lm_custom_start_over.png",
		"http://www.loadmax.com/images/icons/lm_custom_end.png",
		"http://www.loadmax.com/images/icons/lm_custom_end_over.png"];

	map=new GMap2($('#vu')[0]);
	map.setCenter(new GLatLng(37.212639,-93.31817),4);
	map.addControl(new GSmallMapControl());
	
	
	directions=new GDirections(map);
	bounds=new GLatLngBounds();
	
	//initialize secondary maps
	mapo=new GMap2($('#vuo')[0]);
	mapd=new GMap2($('#vud')[0]);
}
function draw_result(index) {
	if (posts[index].destination)
		directions.loadFromWaypoints([posts[index].origin.location,posts[index].destination.location],	{preserveViewport:true});
	posts[index].origin['marker'].setImage(markers[1]);
	if (posts[index].destination)
		posts[index].destination['marker'].setImage(markers[3]);
	mapo.setCenter(new GLatLng(posts[index].origin.latitude,posts[index].origin.longitude),6);
	
	if (posts[index].destination)
		mapd.setCenter(new GLatLng(posts[index].destination.latitude,posts[index].destination.longitude),6);
	var omarker=new GMarker(new GLatLng(posts[index].origin.latitude,posts[index].origin.longitude));
	mapo.addOverlay(omarker);
	posts[index].origin['mapo_marker']=omarker;
	if (posts[index].destination) {
		var marker=new GMarker(new GLatLng(posts[index].destination.latitude,posts[index].destination.longitude));	
		mapd.addOverlay(marker);
		posts[index].destination['mapd_marker']=marker;
	}
	var controller=search['lt']=="L"?"loads":"trucks";
	$('#post').empty().html("Retrieving Post Information");		
	var url="/"+controller+"/see_template/"+posts[index].id+"?template=table";
	$.get(url,{},function(details,code) {
		$('#post').empty().html(details);
	});
	$.each(posts,function(i,o) {
			if (i!=index) {
				o.origin['marker'].setImage(markers[0]);
				if (o.destination['marker'])
					o.destination['marker'].setImage(markers[2]);
				
			}
	});
		
}
function withdraw_result(index) {
	posts[index].origin['marker'].setImage(markers[0]);
	mapo.removeOverlay(posts[index].origin['mapo_marker']);
	if (posts[index].destination) {
		
		posts[index].destination['marker'].setImage(markers[2]);
	
		mapd.removeOverlay(posts[index].destination['mapd_marker']);
	}
}
function load_results(map,results) {
	posts=results;
	var lm_icon=new GIcon(G_DEFAULT_ICON);
	lm_icon.shadow="";
	lm_icon.image="http://www.loadmax.com/images/icons/lm_custom_start.png";
	lm_icon.iconSize=new GSize(15,15);
	lm_icon.iconAnchor=new GPoint(6,7);
	lm_icon.infoWindowAnchor=new GPoint(7,6);	
	lm_icon_end=new GIcon(lm_icon);
	lm_icon_end.image=markers[2];		
	$.each(posts,function(i,post) {
		var post=this;
		//render origin marker
			var opoint=new GLatLng(post.origin.latitude,post.origin.longitude);
			var omarker=new GMarker(opoint,lm_icon);
			GEvent.addListener(omarker,"mouseover",function() {
				draw_result(i);
			});
			GEvent.addListener(omarker,"mouseout",function() {
				withdraw_result(i);
			});
			GEvent.addListener(omarker,"click",function() {
				var controller=search['lt']=='L'?'loads':'trucks';
				$('#tb_link').attr('href',"/"+controller+"/see_template/"+posts[i].id+"?template=see").trigger('click');	
			});
		//render destination marker
		if (post.destination) {
			var dpoint=new GLatLng(post.destination.latitude,post.destination.longitude);
			var dmarker=new GMarker(dpoint,lm_icon_end);
		}
		//store the references in the tree
			posts[i].origin['marker']=omarker;
		if (post.destination)
			posts[i].destination['marker']=dmarker;
		//add the points to the map
			map.addOverlay(posts[i].origin['marker']);
		if (post.destination) 
			map.addOverlay(posts[i].destination['marker']);
		//extend bounds
			bounds.extend(opoint);
		if (post.destination)
			bounds.extend(dpoint);
	});
	map.setZoom(map.getBoundsZoomLevel(bounds));	
	map.setCenter(bounds.getCenter());
}



