/**
 * JUGGLING THEME
 * ==============
 *
 * This javascript library contains functions and
 * classes dedicated to Juggling theme.
 */

var config_theme_path = '';



/**
 * GOOGLE MAP
 * ==========
 *
 * Functions and classes to easly integrate google
 * maps.
 */

// Maps data
var map 			= null;
var map_geocoder 	= null;
var map_focus_lat 	= 0;
var map_focus_lng 	= 0;
var map_focus_zoom 	= 13;

// Icons
var icon_user_male 		= null;
var icon_user_female 	= null;
var icon_group_male 	= null;
var icon_group_female 	= null;
var icon_group_mixed 	= null;


// Load and return an icon, that can be used
// as marker on a Google Map
function map_load_marker_icon(image, shadow)
{
	var icon 	= new google.maps.Icon();
	icon.image 	= image;
	icon.shadow = shadow;

	icon.iconSize 			= new google.maps.Size(32, 32);
	icon.iconAnchor 		= new google.maps.Point(16, 40);
	icon.infoWindowAnchor 	= new google.maps.Point(16, 1);

	return icon;
}

// Load an iconset used as markers on
// Google Maps.
function map_load_marker_iconset(iconset)
{
	if (iconset == 'users')
	{
		icon_user_male 		= map_load_marker_icon(config_theme_path + "/images/icons/markers/user_male.png", "");
		icon_user_female 	= map_load_marker_icon(config_theme_path + "/images/icons/markers/user_female.png", "");
		icon_group_male 	= map_load_marker_icon(config_theme_path + "/images/icons/markers/group_male.png", "");
		icon_group_female 	= map_load_marker_icon(config_theme_path + "/images/icons/markers/group_female.png", "");
		icon_group_mixed 	= map_load_marker_icon(config_theme_path + "/images/icons/markers/group_mixed.png", "");
	}
}

function map_create_user_marker(point, users)
{
	var icon;
	var males = 0;
	var females = 0;

	var html = "";
	for (var u = 0; u < users.length; u++)
	{
		var id 		= users[u].getAttribute("id");
		var name 	= users[u].getAttribute("name");
		var gender 	= users[u].getAttribute("gender").toLowerCase();
		var age 	= users[u].getAttribute("age");
		var picture = users[u].getAttribute("picture");

		if (gender == "male")
			males++;
		else if (gender == "female")
			females++;

		html += "<div class=\"" + gender + "\">";
		html += "<div style=\"float: left; width: 50px; margin: 2px 5px 2px 2px;\">";
		html += "<img src=\"system/image.php?file=" + picture + "&width=50\" hspace=\"2\" border=\"0\" />";
		html += "</div>";
		html += "<div style=\"float: left; margin: 2px 2px 2px 5px;\"";
		html += "<a href=\"?L=users.profile&id=" + id + "\"><strong>" + name + "</strong></a><br/>";
		html += "<span class=\"small\">" + age + " years old</span>";
		html += "</div><div style=\"clear: both;\"></div>";
		html += "</div><br/>";
	}

	// Icon
	if (males == 1 && females == 0)
		icon = icon_user_male;
	else if (females == 1 && males == 0)
		icon = icon_user_female;
	else if (males == 0)
		icon = icon_group_female;
	else if (females == 0)
		icon = icon_group_male;
	else
		icon = icon_group_mixed;

	// Create the marker
	var marker = new google.maps.Marker(point, icon);
	GEvent.addListener(marker, "click", function()
	{
		marker.openInfoWindowHtml(html);
	});

	return marker;
}


function map_show_address_zoom(address, zoom)
{
	if (map == null)
		return;

	if (map_geocoder == null)
		map_geocoder = new google.maps.ClientGeocoder();

	map_geocoder.getLatLng(address, function(point)
	{
		if (point)
			map.setCenter(point, zoom);
	});
}

function map_show_address(address)
{
	map_show_address_zoom(address, 13);
}


function map_show_location_zoom(latitude, longitude, zoom)
{
	if (map == null)
		return;

	map.setCenter(new google.maps.LatLng(latitude, longitude), zoom);
}

function map_show_location(latitude, longitude)
{
	map_show_location_zoom(latitude, longitude, 13);
}

function map_initialize(map_id, type)
{
	// Load iconset
	map_load_marker_iconset(type);

	// Create the map
	map = new google.maps.Map2(document.getElementById(map_id));
	map.addControl(new google.maps.LargeMapControl());
    map.setCenter(new google.maps.LatLng(map_focus_lat, map_focus_lng), map_focus_zoom);
	
	// Create the marker manager
	var manager = new google.maps.MarkerManager(map);

	// Add markers to the manager
	GDownloadUrl("?L=map.locations&" + type + "=true", function(data, responseCode)
	{
		var xml = GXml.parse(data);
		var locations = xml.documentElement.getElementsByTagName("location");
		var markers = [];

		if (type == 'users')
		{
			for (var i = 0; i < locations.length; i++)
			{
				var point = new google.maps.LatLng(locations[i].getAttribute("lat"), locations[i].getAttribute("lng"));
				var users = locations[i].getElementsByTagName("user");
				markers.push(map_create_user_marker(point, users));
			}
		}

		manager.addMarkers(markers, 1);
		manager.refresh();
	});
}

