// A Rectangle is a simple overlay that outlines a lat/lng bounds on the
// map. It has a border of the given weight and color and can optionally
// have a semi-transparent background color.

var map_div = document.getElementById("map"); 
var container_div  = document.getElementById("container"); 
var message_div  = document.getElementById("message"); 
var map = new GMap2(map_div, {draggableCursor: 'crosshair', draggingCursor: 'pointer'});

map.setUIToDefault ();

var TotalMeasure = 0;
var Measure;
var title;
var title_div;

var g_waypoints = false;
var g_distance = false;
var g_location = true;

var WaypointPoints = new Array;
var ToolOverlays = new Array;
var DistanceOverlays = new Array;
var LocationMarker = null;
var MarkerArrayLandmarks = null;
var MarkerArrayCourse = null;

var first_point;
var second_point;

var next_sel_image = new Image();
next_sel_image.src="pics/next_sel.gif";
var prev_sel_image = new Image();
prev_sel_image.src="pics/prev_sel.gif";

var next_image = new Image();
next_image.src="pics/next.gif";
var prev_image = new Image();
prev_image.src="pics/prev.gif";

var first_sel_image = new Image();
first_sel_image.src="pics/first_sel.png";
var last_sel_image = new Image();
last_sel_image.src="pics/last_sel.png";

var first_image = new Image();
first_image.src="pics/first.png";
var last_image = new Image();
last_image.src="pics/last.png";

function HandleDistance (point)
{
	if (first_point && second_point)
	{
		first_point = null;
		second_point = null;

		message_div.innerHTML = "Distance points cleared";
		ClearDistanceOverlays ();
	}
	else if (first_point)
	{
		second_point = point;
		var miles = point_distance (first_point, second_point) * 0.000539956803;
		message_div.innerHTML = "From (" + first_point.x + "," + first_point.y + ") to (" + second_point.x + "," + second_point.y + ") is " + fmt(miles,2) + " Nautical Miles";

		var new_marker = new GMarker(second_point,get_old_icon ());
		map.addOverlay(new_marker);
		DistanceOverlays.push(new_marker);

		// draw the line
		var distance_points = new Array;
		distance_points.push (first_point);
		distance_points.push (second_point);

		var new_polyline = new GPolyline(distance_points, "#FF0000", 2);
		map.addOverlay(new_polyline);
		DistanceOverlays.push (new_polyline);
		distance_points = null;
	} 
	else 
	{
		first_point = point;
		message_div.innerHTML = "From (" + first_point.x + "," + first_point.y + ") to click for second point";

		ClearDistanceOverlays ();

		var new_marker = new GMarker(first_point,get_old_icon ());
		map.addOverlay(new_marker);
		DistanceOverlays.push(new_marker);
	}
}

function centerAndZoom (CenterLat, CenterLng)
{
	var CenterPoint = new GLatLng (CenterLat, CenterLng);
	map.setCenter(CenterPoint);
	map.setZoom (9);
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/; domain=cannonade.net;";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function HandleClick (i, course) 
{
	var InfoWindow = map.getInfoWindow ();
	map.closeInfoWindow ();

	var CurrentMarkerArray;
	if (course == "2")
		CurrentMarkerArray = MarkerArrayLandmarks;
	else
		CurrentMarkerArray = MarkerArrayCourse;

	if (CurrentMarkerArray[i].tabbed == 1)
	{
		var infoTabs = [
		  new GInfoWindowTab("Info", CurrentMarkerArray[i].html1),
		  new GInfoWindowTab("More...", CurrentMarkerArray[i].html2)
		];

		CurrentMarkerArray[i].openInfoWindowTabsHtml(infoTabs, 
					{maxWidth: 780, maxContent: CurrentMarkerArray[i].html1, maxTitle: "Info"} );
	}
	else
	{
		CurrentMarkerArray[i].openInfoWindowHtml (CurrentMarkerArray[i].html1, 
					{maxWidth: 780, maxContent: CurrentMarkerArray[i].html1, maxTitle: "Info"} );
	}
}

function HandleOver (id) 
{
	var button = document.getElementById(id);

	if (id == 'next')
		button.src= next_sel_image.src;
	if (id == 'prev')
		button.src= prev_sel_image.src;
	if (id == 'first')
		button.src= first_sel_image.src;
	if (id == 'last')
		button.src= last_sel_image.src;
}

function HandleLeave (id) 
{
	var button = document.getElementById(id);

	if (id == 'next')
		button.src= next_image.src;
	if (id == 'prev')
		button.src= prev_image.src;
	if (id == 'first')
		button.src= first_image.src;
	if (id == 'last')
		button.src= last_image.src;
}

function get_flags (text)
{
	text = text.toLowerCase();
	var result = "<p>";
	for (var i = 0; i < text.length; i++)
	{
		if (text[i] == ' ')
		{
			result = result + "<img height=\"16\" width=\"16\" src=\"pics/space.png\">";
		}

		if ((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z') || (text[i] >= '0' && text[i] <= '9'))
		{
			result = result + "<img height=\"16\" width=\"16\" src=\"pics/" + text[i] + "-flag.png\">";
		}
	}
	result = result + "</p>";
	return result;
}

function fmt_point (point, compass)
{
	// Convert to Degree Minutes Seconds Representation
    PointDeg = Math.floor(point);
    PointMin = Math.floor((point-PointDeg)*60);
    PointSec =  Math.floor((Math.round((((point - PointDeg) - (PointMin/60)) * 60 * 60) * 100) / 100 ) );

	if (PointSec == 60)
	{
		PointMin = PointMin + 1;
		PointSec = 0;
	}

	if (PointMin == 60)
	{
		PointDeg = PointDeg + 1;
		PointMin = 0;
	}

	if (PointMin > 0 && PointSec > 0)
	{
		results = PointDeg + '&deg; ' + PointMin + '\' ' + PointSec + '" ' + compass;
	}
	else if (PointMin > 0)
	{
		results = PointDeg + '&deg; ' + PointMin + '\' ' + compass;
	}
	else
	{
		results = PointDeg + '&deg; ' + compass;
	}

	return results;
}

function fmt_lat (lat)
{
	var results;

	var compass = 'test';

	if (lat >= 0)
	{
		compass = 'East';
	}
	else
	{
		compass = 'West';
	}

	lat = Math.abs (lat);

	return fmt_point (lat , compass);
}

function fmt_lng (lng)
{
	var results;

	var compass = 'test';

	if (lng >= 0)
	{
		compass = 'North';
	}
	else
	{
		compass = 'South';
	}

	lng = Math.abs (lng);

	return fmt_point (lng , compass);
}

function fmt(num, dp) {
  var exp = Math.pow(10, dp);
  return  Math.round(num * exp) / exp;
}

/* Convert degress to radians */
function deg2rad(deg) {
  return deg / (180 / Math.PI);
}

/* Calculate distance between two points */
function point_distance(a, b) {
  var r = 6378700;
  var lat1 = a.y;
  var lat2 = b.y;
  var lon1 = a.x;
  var lon2 = b.x;
  var dist = r * Math.acos(Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + 
			   Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
			   Math.cos(deg2rad(lon1 - lon2)));
  return dist;
}

function IsIE () 
{
 if (navigator.userAgent.indexOf ("MSIE") != -1)
 {
	 return 1;
 }
 return 0;
}

function MapResize ()
{
	if (IsIE ())
		map_div.style.height = menu_div.style.height;

	var center = map.getCenter();
	map.checkResize();
	map.setCenter(center);
}

function get_old_icon ()
{
	return get_icon_urls ("http://labs.google.com/ridefinder/images/mm_20_red.png","http://labs.google.com/ridefinder/images/mm_20_shadow.png");
}

function get_icon ()
{
	//return get_icon_urls ("http://labs.google.com/ridefinder/images/mm_20_red.png","http://labs.google.com/ridefinder/images/mm_20_shadow.png");
	return get_icon_urls ("pics/blue_marker.png","http://labs.google.com/ridefinder/images/mm_20_shadow.png");
}

function get_icon_urls (image, shadow)
{
	// Create our "tiny" marker icon
	var icon = new GIcon();
	icon.image = image;
	icon.shadow = shadow;
	icon.iconSize = new GSize(12, 20);
	icon.shadowSize = new GSize(22, 20);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);
	return icon;
}

function ClearDistanceOverlays ()
{
	for (var i = 0; i < DistanceOverlays.length; i++) 
	{
		map.removeOverlay (DistanceOverlays [i]);
	}
}

function ClearLocationOverlay ()
{
	if (LocationMarker)
	{
		map.removeOverlay (LocationMarker);
	}
}

function ClearToolOverlays ()
{
	WaypointPoints = new Array;

	for (var i = 0; i < ToolOverlays.length; i++) 
	{
		map.removeOverlay (ToolOverlays [i]);
	}

	ClearLocationOverlay ();
	ClearDistanceOverlays ();
	message_div.innerHTML = "&nbsp;";
}

function SelectDistanceTool ()
{
	g_waypoints = false;
	g_distance = true;
	g_location = false;
	ClearToolOverlays ();
}

function SelectWaypointsTool ()
{
	g_waypoints = true;
	g_distance = false;
	g_location = false;
	ClearToolOverlays ();
}

function SelectLocationTool ()
{
	g_waypoints = false;
	g_distance = false;
	g_location = true;
	ClearToolOverlays ();
}

function load ()
{
	var str = window.location.search;
	delim = str.indexOf("?");
	if (delim >= 0)
	{
		var keyword = str.substring(delim + 1, str.length);

		var ParamsArray = keyword.split('&');
		if (ParamsArray.length == 1)
		{
			reload (keyword);
			return;
		}

		if (ParamsArray.length == 3)
		{
			setTimeout(function() {
				HandleClick (parseInt (ParamsArray[1]), ParamsArray[2]);
			}, 3000);
			reload (ParamsArray[0]);
			return;
		}

		reload (keyword);
	}
	else
	{
		reload ('Master_and_Commander');
	}
} 

function reload (filter_book)
{
	message_div.innerHTML = "&nbsp;";

	ClearToolOverlays ();

	map.clearOverlays();
	map.enableContinuousZoom();
	GEvent.clearInstanceListeners(map);

	if (MarkerArrayCourse)
		MarkerArrayCourse = null;
	if (MarkerArrayLandmarks)
		MarkerArrayLandmarks = null;

	ToolOverlays = new Array;
	WaypointPoints = new Array;
	DistanceOverlays = new Array;
	MarkerArrayCourse = new Array;
	MarkerArrayLandmarks = new Array;

	map.addMapType(G_SATELLITE_3D_MAP);

	function createMarkerHTML (point, infoTitle, infoIndex, infoBook, infoCourse, quote, desc, image, image_link, prev_link, next_link, credit, icon_image, icon_shadow, current_book) 
	{
		var html = "<div class=\"iwstyle\"><div class=\"bookName\">" + current_book + "</div>"

		new_title = "<a href=\"http://www.cannonade.net/map.php?" + infoBook + "&" + infoIndex + "&" + infoCourse + "\">" + infoTitle + "</a>" 

		html = html + new_title + get_flags (infoTitle) + "<p>" + fmt_lng (point.y) + " - " + fmt_lat (point.x) + "</p>";

		if (credit && credit.length > 0)
		{
			html = html + "<p><a href=\"credits.php\">" + credit + "</a></p>";
		}

		if (quote && quote.length > 0 && desc && desc.length > 0) 
		{
			 html = html + "<p>" + desc + "</p><p style=\"font-style : italic;\">" + quote + "</p>";
		}
		else if (desc && desc.length > 0)
		{
			html = html+ " <p>" + desc + "</p>";
		} else if (quote && quote.length > 0)
		{
			html = html + "<p style=\"font-style : italic;\">" + quote + "</p>";
		}

		if (image && image.length > 0)
		{
			/*
			if (image_link && image_link.length > 0)
				html = html + "<a onclick='Slimbox.open(\""+image_link+"\", \""+infoTitle+"\");return false' rel=\"lightbox\" href=\""+image_link+"\"><img height=\"112\" class=\"iwthumb\" border=\"0\" src=\"" + image + "\"/></a>";
			else
				html = html + "<img height=\"112\" class=\"iwthumb\" border=\"0\" src=\"" + image + "\"/>";
			*/
			html = html + "<img height=\"112\" src=\"" + image + "\"/>";
		}

		html = html + "<div class=\"iwnavigate\">";
		html = html + prev_link;
 		html = html + "<img height=\"24\" onClick=\"centerAndZoom("+ point.y + ","+ point.x +")\" style=\"text-align: center; padding-left:20px; padding-right:20px; cursor: pointer\" src=\"pics/magnify_glass_small.png\">";
		html = html + next_link;
		html = html + "</div>";
			
		html = html + "</div>";

		return html;
	}

	function createMarkerIcon (point, icon_image, icon_shadow)
	{
		var marker;
		if (icon_image && icon_image.length && icon_shadow && icon_shadow.length)
			marker = new GMarker(point, get_icon_urls (icon_image, icon_shadow));
		else if (icon_shadow && icon_shadow.length)
			marker = new GMarker(point, get_icon_urls ("http://labs.google.com/ridefinder/images/mm_20_red.png",icon_shadow));
		else if (icon_image && icon_image.length)
			marker = new GMarker(point, get_icon_urls (icon_image, "http://labs.google.com/ridefinder/images/mm_20_shadow.png"));
		else
			marker = new GMarker(point, get_icon());

		return marker;
	}

	// Creates a marker whose info window displays the given number
	function createMarker(point, infoTitle, infoIndex, infoBook, infoCourse, quote, desc, image, image_link, prev_link, next_link, credit, icon_image, icon_shadow, current_book) {

		var marker = createMarkerIcon (point, icon_image, icon_shadow);
		
		marker.point = point;
		marker.html1 = createMarkerHTML (point, infoTitle, infoIndex, infoBook, infoCourse, quote, desc, image, image_link, prev_link, next_link, credit, icon_image, icon_shadow, current_book); 
		marker.tabbed = 0;

	  	GEvent.addListener(marker, "click", function() {
			if  (marker)
			{
				if (g_distance)
				{
					HandleDistance (marker.point);
					return;
				}
				marker.openInfoWindowHtml(marker.html1,
					{maxWidth: 780, maxContent: marker.html1, maxTitle: "Info"} );
			}
		  }
		  );
	
		return marker;
	}

	function createTabbedMarker (point, infoTitle, infoIndex, infoBook, infoCourse, quote, quote2, desc, desc2, image, image2, image_link, image_link2, prev_link, next_link, credit, icon_image, icon_shadow, current_book)
	{
		var marker = createMarkerIcon (point, icon_image, icon_shadow);
		
		marker.point = point;
		marker.html1 = createMarkerHTML (point, infoTitle, infoIndex, infoBook, infoCourse, quote, desc, image, image_link, prev_link, next_link, credit, icon_image, icon_shadow, current_book); 
		marker.html2 = createMarkerHTML (point, infoTitle, infoIndex, infoBook, infoCourse, quote2, desc2, image2, image_link2, prev_link, next_link, credit, icon_image, icon_shadow, current_book); 
		marker.tabbed = 1;

		GEvent.addListener(marker, "click", function() {
			if  (marker)
			{
				if (g_distance)
				{
					HandleDistance (marker.point);
					return;
				}

				var infoTabs = [
				  new GInfoWindowTab("Info", marker.html1),
				  new GInfoWindowTab("More...", marker.html2)
				];

				marker.openInfoWindowTabsHtml(infoTabs, 
					{maxWidth: 780, maxContent: marker.html1, maxTitle: "Info"} );
			}
		  }
		  );
	
		return marker;
	}

	if (!GBrowserIsCompatible ())
	{
		alert ("Incompatible Browser");
		window.location = "http://www.cannonade.net/";
		return;
	}
	
	// Download the data in data.xml and load it on the map.
	var request = GXmlHttp.create();
	if (filter_book == "Lissuns")
		request.open("GET", "xml/" + filter_book + ".php", true);
	else
		request.open("GET", "xml/" + filter_book + ".xml", true);

	request.onreadystatechange = function() {

	  if (request.readyState == 4) 
	  {
		var xmlDoc = request.responseXML;
		if (xmlDoc && xmlDoc.documentElement)
		{
			var current_book = "";
			var titleElements = xmlDoc.documentElement.getElementsByTagName(filter_book);
			if (titleElements.length == 1)
			{
				Measure = titleElements[0].getAttribute("Measure");
				TotalMeasure = 0;
				Title = titleElements[0].getAttribute("Title");
				title_div = document.getElementById("current_text")
					
				title_div.innerHTML = Title;

				var Zoom = titleElements[0].getAttribute("Zoom");
				var CenterLng = titleElements[0].getAttribute("CenterLng");
				var CenterLat = titleElements[0].getAttribute("CenterLat");

				if (Zoom && CenterLat && CenterLng)
				{
					var CenterPoint = new GLatLng (parseFloat(CenterLat), parseFloat(CenterLng));

					map.setCenter(CenterPoint);
					map.setZoom (parseInt(Zoom));

				}
				else
				{
					map.setCenter( new GLatLng (parseFloat(40) , parseFloat (5)));
					map.setZoom (11);
				}
			}
			else
			{
				document.getElementById("title").innerHTML = "The Voyages of Jack Aubrey and Steven Maturin";
			}

			var markerElements = xmlDoc.documentElement.getElementsByTagName("marker");

			var polyline_points = new Array;
			var points = new Array(markerElements.length);
			var link_array_course = new Array;
			var link_array_landmarks = new Array;

			for (var i = 0; i < markerElements.length; i++) 
			{
				var current_desc = markerElements[i].getAttribute("desc");
				var current_quote = markerElements[i].getAttribute("quote");
				var current_title = markerElements[i].getAttribute("title");
				var current_course = markerElements[i].getAttribute("course");

				var lng = markerElements[i].getAttribute("lng");
				var lat = markerElements[i].getAttribute("lat");

				if ((current_desc && current_desc.length > 0)  || (current_quote && current_quote.length > 0))
				{
					if (current_course == "2")
					{
						var link = "onClick=\"HandleClick("+link_array_landmarks.length+",'"+current_course+"')\"";
						link_array_landmarks.push (link);
					}
					else
					{
						var link = "onClick=\"HandleClick("+link_array_course.length+",'"+current_course+"')\"";
						link_array_course.push (link);
					}
				}
			}

			var indexCourse = 0;
			var indexLandmark = 0;

			for (var i = 0; i < markerElements.length; i++) 
			{
				// first tab
				var current_image = markerElements[i].getAttribute("image");
				var current_image_link = markerElements[i].getAttribute("image_link");

				// second tab
				var current_image2 = markerElements[i].getAttribute("image2");
				var current_image_link2 = markerElements[i].getAttribute("image_link2");

				var credit = markerElements[i].getAttribute("credit");
				var current_title = markerElements[i].getAttribute("title");

				// first tab
				var current_desc = markerElements[i].getAttribute("desc");
				var current_quote = markerElements[i].getAttribute("quote");

				// second tab
				var current_desc2 = markerElements[i].getAttribute("desc2");
				var current_quote2 = markerElements[i].getAttribute("quote2");

				var current_icon_image = markerElements[i].getAttribute("icon_image");
				var current_icon_shadow = markerElements[i].getAttribute("icon_shadow");
				var course = markerElements[i].getAttribute("course");
				var color = markerElements[i].getAttribute("color");
				var legend_name = markerElements[i].getAttribute("legend_name");
				var ignore_distance = markerElements[i].getAttribute("ignore_distance");

				book = markerElements[i].getAttribute ("book");
				if (book && book.length > 0)
					current_book = book;

				points[i] = new GPoint(parseFloat(markerElements[i].getAttribute("lng")), parseFloat(markerElements[i].getAttribute("lat")));

				if (course == null)
				{
					polyline_points.push(points[i]);
				}
				else if (course == "1")
				{
					// start a line
					polyline_points = new Array;
					polyline_points.push(points[i]);
				} else if (course == "0")
				{
					polyline_points.push(points[i]);
					// end a line
					if (polyline_points.length > 0)
					{
						var legend_ul = document.getElementById("legend");
						if (legend_name && legend_name.length > 0)
						{
							var item_div = document.createElement("li");
							var item_link = document.createElement("a");

							item_link.style.color = color;
							// item_div.style.color = "#F7F7F2";
							item_link.setAttribute ('href', "http://www.cannonade.net/map.php?" + filter_book + "&" + indexCourse + "&0");
							item_link.innerHTML += legend_name;

							item_div.appendChild (item_link);
							legend_ul.appendChild (item_div);
						}

						// now add the polyline_points
						map.addOverlay(new GPolyline(polyline_points, color, 2));

						if (Measure && Measure == 'true') 
						{
							// increment Measure by the length of the polylines

							var CurrentMeasure = 0;

							for (j = 0; j < polyline_points.length; j++)
							{
								if (!j)
									continue;

								CurrentMeasure = CurrentMeasure + (point_distance (polyline_points[j-1], polyline_points[j]) * 0.000539956803);
							}

							if (ignore_distance != "1" && !isNaN (CurrentMeasure))
								TotalMeasure = TotalMeasure + CurrentMeasure;
							//else
							//	alert (indexCourse);

							title_div.innerHTML = Title + " (" + Math.round(TotalMeasure) + " Nautical Miles)";
						}
					}
				} 
				else if (course == "2")
				{
					// no line
				}

				if ((current_desc && current_desc.length > 0) || 
					(current_quote && current_quote.length > 0) ||
					(current_quote2 && current_quote2.length > 0) ||
					(current_desc2 && current_desc2.length > 0))
				{
					if (!current_title || current_title.length <= 0)
						current_title = filter_book;

					// first
					var prev_link = "<img height=\"25\" src=\"pics/prev_disable.gif\">";
					// last
					var next_link = "<img height=\"25\" src=\"pics/next_disable.gif\">";
					// add the overlay marker (do tabbed marker if specified).
					
					var CurrentMarkerArray;

					var infoIndex;
					var infoTitle;
					var infoCourse;
					var infoBook;

					if (course == "2")
					{
						infoTitle= current_title;
						infoIndex = indexLandmark;
						infoBook = filter_book;
						infoCourse = course;

						if (indexLandmark)
						{
							// prev
							prev_link = "<img height=\"25\" id=\"first\" style=\"cursor:pointer;\" onMouseOver=\"HandleOver('first');\" onmouseout=\"HandleLeave('first');\" style=\"cursor:pointer;\" src=\"pics/first.png\" " + link_array_course [0] + ">" + "<img height=\"25\" id=\"prev\" style=\"cursor:pointer;\" onMouseOver=\"HandleOver('prev');\" onmouseout=\"HandleLeave('prev');\" style=\"cursor:pointer\" src=\"pics/prev.gif\" " + link_array_landmarks [indexLandmark - 1] + ">";
						}

						if (indexLandmark < link_array_landmarks.length - 1)
						{
							// next
							next_link = "<img height=\"25\" id=\"next\" onMouseOver=\"HandleOver('next');\" onmouseout=\"HandleLeave('next');\" style=\"cursor:pointer;\" src=\"pics/next.gif\" " + link_array_landmarks [indexLandmark + 1] + ">" + "<img height=\"25\" id=\"last\" onMouseOver=\"HandleOver('last');\" onmouseout=\"HandleLeave('last');\" style=\"cursor:pointer;\" src=\"pics/last.png\" " + link_array_landmarks [link_array_landmarks.length - 1] + ">";
						}

						if (!indexLandmark)
						{
							// link it to the last course point
							prev_link = "<img height=\"25\" id=\"first\" style=\"cursor:pointer;\" onMouseOver=\"HandleOver('first');\" onmouseout=\"HandleLeave('first');\" style=\"cursor:pointer;\" src=\"pics/first.png\" " + link_array_course [0] + ">" + "<img height=\"25\" id=\"prev\" style=\"cursor:pointer;\" onMouseOver=\"HandleOver('prev');\" onmouseout=\"HandleLeave('prev');\" style=\"cursor:pointer\" src=\"pics/prev.gif\" " + link_array_course [link_array_course.length - 1] + ">";
						}

						CurrentMarkerArray = MarkerArrayLandmarks;
						indexLandmark = indexLandmark + 1;
					}
					else
					{
						infoTitle= current_title;
						infoIndex = indexCourse;
						infoBook = filter_book;
						infoCourse = course;

						if (indexCourse)
						{
							// prev
							prev_link = "<img height=\"25\" id=\"first\" style=\"cursor:pointer;\" onMouseOver=\"HandleOver('first');\" onmouseout=\"HandleLeave('first');\" style=\"cursor:pointer;\" src=\"pics/first.png\" " + link_array_course [0] + ">" + "<img height=\"25\" id=\"prev\" style=\"cursor:pointer;\" onMouseOver=\"HandleOver('prev');\" onmouseout=\"HandleLeave('prev');\" style=\"cursor:pointer\" src=\"pics/prev.gif\" " + link_array_course [indexCourse - 1] + ">";
						}

						if (indexCourse < link_array_course.length - 1)
						{
							// next
							next_link = "<img height=\"25\" id=\"next\" onMouseOver=\"HandleOver('next');\" onmouseout=\"HandleLeave('next');\" style=\"cursor:pointer;\" src=\"pics/next.gif\" " + link_array_course [indexCourse + 1] + ">" + "<img height=\"25\" id=\"last\" onMouseOver=\"HandleOver('last');\" onmouseout=\"HandleLeave('last');\" style=\"cursor:pointer;\" src=\"pics/last.png\" " + link_array_landmarks [link_array_landmarks.length - 1] + ">";
						}

						if (indexCourse == link_array_course.length - 1)
						{
							// link it to the first landmark point
							next_link = "<img height=\"25\" id=\"next\" onMouseOver=\"HandleOver('next');\" onmouseout=\"HandleLeave('next');\" style=\"cursor:pointer;\" src=\"pics/next.gif\" " + link_array_landmarks [0] + ">" + "<img height=\"25\" id=\"last\" onMouseOver=\"HandleOver('last');\" onmouseout=\"HandleLeave('last');\" style=\"cursor:pointer;\" src=\"pics/last.png\" " + link_array_landmarks [link_array_landmarks.length - 1] + ">";
						}

						CurrentMarkerArray = MarkerArrayCourse;
						indexCourse = indexCourse + 1;
					}

					if ((current_quote2 && current_quote2.length > 0) || (current_desc2 && current_desc2.length > 0))
						CurrentMarkerArray.push(createTabbedMarker(points[i], infoTitle, infoIndex, infoBook, infoCourse, current_quote, current_quote2, current_desc, current_desc2, current_image, current_image2, current_image_link, current_image_link2, prev_link, next_link,credit, current_icon_image, current_icon_shadow, current_book));
					else
						CurrentMarkerArray.push(createMarker(points[i], infoTitle, infoIndex, infoBook, infoCourse, current_quote, current_desc, current_image, current_image_link, prev_link, next_link,credit, current_icon_image, current_icon_shadow, current_book));

					map.addOverlay(CurrentMarkerArray[CurrentMarkerArray.length - 1]);
				}
			}
		}
		else
		{
			window.location = "http://www.cannonade.net/";
			return;
		}
	  }
	}

	GEvent.addListener(map, "click", function(marker,point) {
			
			if (!point)
				return;

			if (g_waypoints)
			{			
				message_div.innerHTML += "&lt;marker lat=\"" + point.y + "\" lng=\"" + point.x + "\"/&gt;";

				//for debugging
				if (WaypointPoints.length == 0)
				{
					var new_marker = new GMarker(point,get_old_icon ());
					map.addOverlay(new_marker);
					ToolOverlays.push(new_marker);
				}

				WaypointPoints.push (point);

				if (WaypointPoints.length > 1)
				{
					// draw a line between the last two
					var temp_points = new Array;
					temp_points.push (WaypointPoints [WaypointPoints.length - 1]);
					temp_points.push (WaypointPoints [WaypointPoints.length - 2]);

					var new_polyline = new GPolyline(temp_points, "#AA0000", 2);
					map.addOverlay(new_polyline);
					ToolOverlays.push (new_polyline);
					temp_points = null;
				}
			}
			else if (g_distance)
			{
				HandleDistance (point);	
			}
			else
			{
				ClearLocationOverlay ();
				message_div.innerHTML = "Current Clicked Location is " + fmt_lng (point.y) + " - " + fmt_lat (point.x);
				LocationMarker = new GMarker(point,get_old_icon ());
				map.addOverlay(LocationMarker);
			}	
		}
		);

	request.send(null)
}


