
/**
 *
 * Routes Online - OrgMaps_Google.js
 * Copyright © Fluid Creativity 2008
 *
 */

OrgMaps = {

	init: function () {		
		if (!GBrowserIsCompatible()) return;

		if ($('routeMap')) {
			var orgId = $('routeMap').className.match(/org-([0-9]+)-([A-Za-z]+)-([A-Za-z]+)/)[1];

			new Request.JSON({
				url: '/ajax/route-map.php',
				onComplete: function (data) {
					if (!data) return;

					var map = new GMap2($('routeMap'));
					map.addControl(new GSmallMapControl());
					map.addControl(new GMapTypeControl());

					if (data.focalPoint) {
						var homeIcon = new GIcon(null, Config.staticBase + "/images/common/icons/homeTag.png");
						homeIcon.Size = new GSize(23, 18);
						homeIcon.iconAnchor = new GPoint(12, 18);
					
						var location = new GLatLng(data.focalPoint.latitude, data.focalPoint.longitude);
						map.setCenter(location, 2);
						map.addOverlay(new GMarker(location, { icon: homeIcon }));
					}
					else {
						map.setCenter(new GLatLng(10, 0), 2);
					}

					if (data.routes) {
						data.routes.each(function(route) {
							var polyline = new GPolyline([
									new GLatLng(route.start.latitude, route.start.longitude),
									new GLatLng(route.end.latitude, route.end.longitude)
								], route.colour, 2);
							map.addOverlay(polyline)
						});
					}
				}
			}).get({ 'orgid': orgId, lang: MultiLingual.langCode });
		} else if ($('profileMap')) {
			var orgId = $('profileMap').className.match(/org-(\d+)/)[1];

			new Request.JSON({
				url: '/ajax/org-location.php',
				onComplete: function (data) {
					if (!data) return;

					var map = new GMap2($('profileMap'));
					map.addControl(new GSmallMapControl());
					map.addControl(new GMapTypeControl());

					map.setCenter(new GLatLng(data.latitude, data.longitude), 8);
				}
			}).get({ 'orgid': orgId, lang: MultiLingual.langCode });
		} else if ($('opportunityMap')) {
			var currentPage;
		
			if ($('opportunityMap').className.test(/page-(\d+)/)) {
				currentPage = $('opportunityMap').className.match(/page-(\d+)/)[1];
			}
			
			if ($('opportunityMap').className.test(/org-(\d+)/)) {
				var searchOptions = { 'orgid': $('opportunityMap').className.match(/org-(\d+)/)[1], 'page': currentPage, lang: MultiLingual.langCode };
			} else {
				var searchOptions = { 'search': $('opportunityMap').className.match(/search-([^\s]+)/)[1], 'page': currentPage, lang: MultiLingual.langCode };
			}

			new Request.JSON({
				url: '/ajax/opportunities.php',
				onComplete: function (data) {
					if (!data) return;

					var map = new GMap2($('opportunityMap'));
					map.addControl(new GSmallMapControl());
					map.addControl(new GMapTypeControl());

					map.setCenter(new GLatLng(10, 0), 2);

					// Icons
					var baseIcon = new GIcon();
					baseIcon.Size = new GSize(23, 18);
					baseIcon.iconAnchor = new GPoint(12, 18);

					var wideIcon = new GIcon();
					wideIcon.Size = new GSize(25, 18);
					wideIcon.iconAnchor = new GPoint(13, 18);					
						
					var icons = new Hash();
					icons.set("unserved", new GIcon(baseIcon, Config.staticBase + "/images/" + MultiLingual.langCode + "/icons/unserved.png"));
					icons.set("underserved", new GIcon(wideIcon, Config.staticBase + "/images/" + MultiLingual.langCode + "/icons/underserved.png"));
					icons.set("upcoming", new GIcon(baseIcon, Config.staticBase + "/images/" + MultiLingual.langCode + "/icons/upcoming.png"));
					icons.set("home", new GIcon(baseIcon, Config.staticBase + "/images/common/icons/homeTag.png"));

					if (data.focalPoint) {
						var location = new GLatLng(data.focalPoint.latitude, data.focalPoint.longitude);
						map.addOverlay(new GMarker(location, { icon: icons.get("home") }));
					}
					
					if (data.routes) {
						data.routes.each(function(route) {
							var endPoint = new GLatLng(route.end.latitude, route.end.longitude);
							
							if (data.focalPoint) {
								var overlay = new GMarker(endPoint, { icon: icons.get(route.type) });
							} else {
								var overlay = new GPolyline([
									new GLatLng(route.start.latitude, route.start.longitude),
									endPoint
								], "#cc0000", 2);
							}
							
							map.addOverlay(overlay);
						});
					}
				}
			}).get(searchOptions);
		}
	},

	cleanup: function() {
		GUnload();
	}
}

window.addEvent('load', OrgMaps.init);
window.addEvent('unload', OrgMaps.cleanup);

