//<![CDATA[
var map;
var geocoder;
var bounds = new GLatLngBounds();

var postIndex = 0;
var posts = [];

// Create default red marker
var icon = new GIcon();
icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
icon.iconSize = new GSize(12, 20);
icon.shadowSize = new GSize(22, 20);
icon.iconAnchor = new GPoint(6, 20);
icon.infoWindowAnchor = new GPoint(5, 1);

// Create green "I am here" marker
var nowicon = new GIcon();
nowicon.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
nowicon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
nowicon.iconSize = new GSize(12, 20);
nowicon.shadowSize = new GSize(22, 20);
nowicon.iconAnchor = new GPoint(6, 20);
nowicon.infoWindowAnchor = new GPoint(5, 1);

// Trim foreign characters, spaces, tabs, breaks, from post titles
function trim(str) 
{  if((str.charAt(0) < 32) || (str.charAt(0) > 126))
  {  str = trim(str.substring(1));
  }
  if ((str.charAt(str.length-1) < 32) || (str.charAt(str.length-1) > 126))
  {  str = trim(str.substring(0,str.length-1));
  }
  return str;
}

function load() {

  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallZoomControl());
    map.setCenter(new GLatLng(0,0),0,G_HYBRID_MAP);	
	geocoder = new GClientGeocoder();

	gatherPosts();

	for (i=0;i<postIndex;i++)
	{		
		updateMap(i);
	}

	map.checkResize();

	map.setCenter(bounds.getCenter(), maxZoom(map.getBoundsZoomLevel(bounds)));

  }
}

//Parse the document and build an array of posts
//Creates a posts array like this:
//posts[x][Title]
//posts[x][Date]
//posts[x][Location]
//posts[x][URL]
function gatherPosts()
{
	var postDiv=document.getElementsByTagName("span");
	var postTitle;
	var postURL;
	var postDate;
	var postLocation;
	var foundLocation;

	var classText;
	//Go through all div's in document looking for posts

	for(i=0;i<postDiv.length;i++)
	{
		classText=postDiv[i].getAttribute('class');

		// Workaround for IE
		if(classText == null)
		{
			classText=postDiv[i].getAttribute('className');
		}

		if (classText == "post")
		{
			// Found a post, add to array
			posts.push(new Array(4));
			foundLocation = 0;
			
//NOTE: Post titles also have permalink <h3><a href="permalink">POST TITLE</a></h3>

			// Add post title and URL
			postTitle=postDiv[i].getElementsByTagName("h3")[0].getElementsByTagName("a")[0];
			posts[postIndex][3] = postTitle.href;
			posts[postIndex][0] = trim(postTitle.innerHTML);

//NOTE: This is now <h2 class><span>DATE</span></h2>

			// Add post date
			postDate=postDiv[i].getElementsByTagName("h2");
			for(j=0;j<postDate.length;j++)
			{
				if(postDate[j].getAttribute('class') == "date-header")
				{
					postDate = postDate[j].getElementsByTagName("span");
					posts[postIndex][1] = new Date(postDate[0].innerHTML);
					break;
				}
			}

//NOTE: This should be OK

			// Add post location
			postLocation=postDiv[i].getElementsByTagName("input");

			for(j=0;j<postLocation.length;j++)
			{
				if(postLocation[j].getAttribute('name') == "location")
				{
					if (foundLocation > 0)
					{
						posts.push(new Array(4));
						posts[postIndex+foundLocation][0] = posts[postIndex][0];
						posts[postIndex+foundLocation][1] = posts[postIndex][1];
						posts[postIndex+foundLocation][3] = posts[postIndex][3];
					}
					posts[postIndex+foundLocation][2] = postLocation[j].value;
					foundLocation++;
				}
				
			}

			if (foundLocation == 0)
			{
				foundLocation = 1;
			}
			postIndex = postIndex + foundLocation;

		}
	}

}


function updateMap(pIdx)
{
	// Don't mark the same location more than once
	for (x=0; x<pIdx; x++)
	{
		if (posts[x][2] == posts[pIdx][2])
		{
			return;
		}
	}

	//NEW: Untested code for allowing manual coordinate specification
	if (posts[pIdx][2].indexOf('coord:') >= 0)
	{
		var lng;
		var lat;
		var pt;
		var z = posts[pIdx][2];

		lng = parseFloat(z.substring(z.indexOf(':')+1, z.indexOf(',')));
		lat = parseFloat(z.substring(z.indexOf(',')+1));

		pt = new GLatLng(lat, lng);
		if (pIdx==0)
		{
			var marker = new GMarker(pt, {title:posts[0][0], icon:nowicon});
		}
		else
		{
			var marker = new GMarker(pt, {title:posts[pIdx][0], icon:icon});
		}
		map.addOverlay(marker);
		bounds.extend(pt);
		map.setCenter(bounds.getCenter(), maxZoom(map.getBoundsZoomLevel(bounds)));

		// On click, load related post
		GEvent.addListener(marker, "click", function() {
			if (marker) {
				window.location = posts[pIdx][3];
			}
		});
	}

	//Look up string address if needed
	else
	{
	  geocoder.getLatLng(posts[pIdx][2],	function(point) {
		if (point) {
			if (pIdx==0)
			{
				var marker = new GMarker(point, {title:posts[0][0], icon:nowicon});
			}
			else
			{
				var marker = new GMarker(point, {title:posts[pIdx][0], icon:icon});
			}

			map.addOverlay(marker);
			bounds.extend(point);
			map.setCenter(bounds.getCenter(), maxZoom(map.getBoundsZoomLevel(bounds)));

			// On click, load related post
			GEvent.addListener(marker, "click", function() {
				if (marker) {
					window.location = posts[pIdx][3];
				}
			});
		}
	  });
	}
}

function maxZoom(zoomLvl)
{
	if (zoomLvl > 8)
	{
		zoomLvl = 8;
	}

	return(zoomLvl);
}
//]]>
