/*
*
* styles/prosilver/template/th23_gmap.js
*
* @package th23_gmap
* @author Thorsten Hartmann (www.th23.net)
* @copyright (c) 2008 by Thorsten Hartmann (www.th23.net)
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*
* Example:
*
* var th23_gmap_data = new Array(
*    // map1
*    // 'map_id', map_center_lat, map_center_lng, map_zoom, map_type, 
*    //    map_nav_control, map_type_control, map_scale, map_overview, 
*    //    marker(s)
*    new Array('th23_gmap_map1', 50.699578, 7.144203, 12, 2, 2, 1, 0, 2, new Array(
*       // marker 1
*       // marker_lat, marker_lng, 'marker_info_html', marker_open_info
*       new Array(50.682981, 7.159808, '<strong>Home</strong>', 1), 
*       // marker 2
*       new Array(50.716024, 7.122713, '<strong>Work</strong>', 0)
*    )), 
*    // map 2
*    new Array('th23_gmap_map2', 50.250823, 8.646219, 16, 0, 3, 0, 0, 0, new Array(
*       // marker 1
*       new Array(50.252236, 8.648365, '<em>Home</em>', 0)
*    ))
* );
*
*
* Explanation (for non standard variables):
*
* Map:
* - map_type
*   Defines the map type that is loaded initially
*   Options: 0 = G_NORMAL_MAP (the default view, vector graphic)
*            1 = G_SATELLITE_MAP (satellite images)
*            2 = G_HYBRID_MAP (mixture of normal and satellite views)
* - map_nav_control
*   Type of navigation control
*   Options: 0 = none
*            1 = GLargeMapControl (full controll field)
*            2 = GSmallMapControl (+/- for zoom and arrows)
*            3 = GSmallZoomControl (only +/- buttons for zoom)
* - map_type_control
*   Show select buttons to choose from different map types
*   Options: 0 = no
*            1 = yes
* - map_scale
*   Show the scale of the actual map in the lower left corner
*   Options: 0 = no
*            1 = yes
* - map_overview
*   Show overview window for map in bottom right corner
*   Options: 0 = no
*            1 = yes
*            2 = hidden
*
* Marker:
* - marker_open_info
*   Show info window for this marker after map is loaded
*   Options: 0 = hide
*            1 = show
*   Note: Can only be done for one of window, if used for more, only
*         last one will be shown
*
*/

function th23_gmap_init() 
{
	if (GBrowserIsCompatible())
	{
		// init maps and populate with markers
		for (var i = 0; i < th23_gmap_data.length; i++)
		{
			// initialize map
			th23_gmap_create_map(th23_gmap_data[i]);
		}

		// init editor map
		var map_editor = th23_gmap_create_map(new Array('th23_gmap_editor', 49.996440, 8.085937, 2, 1, 1, 1, 1, 1, new Array()));
		// define listener to grab current coordinates
		GEvent.addListener(map_editor, "click", function(overlay, point) {
			if (point)
			{
				this.openInfoWindowHtml(point, "Lat: " + point.lat() + "<br />Lng: " + point.lng() + "<br />Zoom: " + this.getZoom());
			}
		});
		GEvent.addListener(map_editor, "zoomend", function() {
			this.closeInfoWindow();
		});
	}
}

function th23_gmap_create_map(map_data)
{
	// init map, if target element exists
	var map_element = document.getElementById(map_data[0]);
	if (map_element)
	{
		var map = new GMap2(map_element);
	}
	else
	{
		return;
	}

	// set map type
	map.setMapType(G_DEFAULT_MAP_TYPES[parseInt(map_data[4])]);
	
	// navigation control
	if (parseInt(map_data[5]) == 1)
	{
		map.addControl(new GLargeMapControl());
	}
	else if (parseInt(map_data[5]) == 2)
	{
		map.addControl(new GSmallMapControl());
	}
	else if (parseInt(map_data[5]) == 3)
	{
		map.addControl(new GSmallZoomControl());
	}
	
	// map type control
	if (parseInt(map_data[6]) == 1)
	{
		map.addControl(new GMapTypeControl());
	}

	// scale control
	if (parseInt(map_data[7]) == 1)
	{
		map.addControl(new GScaleControl());
	}

	// init overview window
	if (parseInt(map_data[8]) > 0)
	{
		var map_overview = new GOverviewMapControl();
		map.addControl(map_overview);
		if (parseInt(map_data[8]) == 2)
		{
			map_overview.hide(true);
		}
	}

	// set center, zoom
	map.setCenter(new GLatLng(parseFloat(map_data[1]), parseFloat(map_data[2])), parseInt(map_data[3]));

	// populate map with markers
	for (var j = 0; j < map_data[9].length; j++)
	{
		var marker_data = map_data[9][j];
		
		// create marker
		var marker = th23_gmap_create_marker(new GLatLng(parseFloat(marker_data[0]), parseFloat(marker_data[1])), marker_data[2]);
		map.addOverlay(marker);
		if (parseInt(marker_data[3]) == 1)
		{
			GEvent.trigger(marker, "click");
		}
	}

	return map;
}

function th23_gmap_create_marker(point, html) 
{
	var basis_marker = new GMarker(point);
	
	GEvent.addListener(basis_marker, "click", function() {
		this.openInfoWindowHtml(html);
	});

	return basis_marker;
}

