///////////////////////////////////////////////////////////////////////////////
//                                                                           //
//                       Copyright (c) 2003-2006                             //
//                            Marc Peterson                                  //
//                     marc.s.peterson at gmail.com                          //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

// Last updated:
//   08-20-2006 - created

// External Variables needed on the page
//	var map;							// Stores the GMap object
//	var map_lat		= ;					// Latitiude for map
//	var map_lng		= ;					// Longitude for map
//	var map_zoom	= ;					// Zoom for map
//	var map_type	= ;					// Map type
//	var lines;							// Stores the GPolyline object
//	var make_lines 	= true|false;		// Connect markers with a line?
//	var draggable	= true|false;		// Are markers draggable?
//	var num_markers = 0;				// Number of markers created
//	var markers		= new Object();		// Stores all created GMarker objects
//  var form_id		= null;				// Name of the form to update when map is moved/zoomed.  Set to null if no form.

// Default function called when page is loaded
function load()
{
	if ( GBrowserIsCompatible() )
	{
		map = new GMap2(document.getElementById("map"));
		var center = new GLatLng(map_lat, map_lng);

		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(center, map_zoom);
		map.setMapType(map_type);

		// If there is a form 
		if ( form_id && document["form_id"] )
		{
			if ( document["form_id"].map_lat ) document["form_id"].map_lat.value = center.lat();
			if ( document["form_id"].map_lng ) document["form_id"].map_lng.value = center.lng();
			if ( document["form_id"].map_zoom ) document["form_id"].map_zoom.value = map.getZoom();
		}

		GEvent.addListener(map, "dragend", function()
		{
			if ( form_id && document["form_id"] )
			{
				var point = map.getCenter();
				if ( document["form_id"].map_lat ) document["form_id"].map_lat.value = point.lat();
				if ( document["form_id"].map_lng ) document["form_id"].map_lng.value = point.lng();
			}
		});

		GEvent.addListener(map, "zoomend", function(oldLevel, newLevel)
		{
			if ( form_id && document["form_id"] )
			{
				if ( document["form_id"].map_zoom ) document["form_id"].map_zoom.value = newLevel;
			}
		});

		GEvent.addListener(map.getInfoWindow(),"closeclick", function()
		{
			map.returnToSavedPosition();
		})

		// Create any markers from database
		setTimeout(createMarkers, 500);
		if ( make_lines ) redrawLines();
	}
}

function addMarker(lat, lng, owner_id, desc, icon)
{
	if ( lat === undefined || lng === undefined ) return;
	
	var point = new GLatLng(lat, lng);
	if ( owner_id === undefined || owner_id == null ) owner_id = "";
	if ( desc === undefined || desc == null ) desc = "";
	if ( icon === undefined || desc == null || icon == "" ) icon = "G_DEFAULT_ICON";

	var marker = new GMarker(point, {draggable: draggable, icon: icon});

	markers[num_markers]= marker;				// Save reference to marker
	marker.marker_num	= num_markers;			// Store marker's number
	marker.owner_id		= owner_id;				// Store marker's owner id
	marker.desc			= desc;					// Store marker's description (appears when clicked)

	if ( desc != "" )
	{
		GEvent.addListener(marker, "click", function()
		{
			map.savePosition();
			marker.openInfoWindow(marker.desc);
		});
	}
	map.addOverlay(marker);
	num_markers++;
}

function redrawLines()
{
	if ( lines ) map.removeOverlay(lines);
	delete lines;
	lines = null;

	var points = new Array();
	if ( num_markers > 1 )
	{
		var i = 0;
		for (var key in markers)
		{
			points[i] = markers[key].getPoint();
			i++;
		}
		lines = new GPolyline(points, "#ffffff", 2, 0.5);
		map.addOverlay(lines);
	}
}
