var map = null; var mgr = null; var tabs = []; var markerOpen = false; var currentMarker = null; var geoCodeResults = null; var AMBASSADORS = 1; var BRAND = 2; var COLLECTIVITY = 4; var PARTNERS = 8; var GROUP = 16; var ACTION_PAST = 32; var ACTION_FUTURE = 64; var ACTION_EVENT = 128; //company-only mask if (DEFAULT_MASK == null) { var DEFAULT_MASK = AMBASSADORS + BRAND + COLLECTIVITY + PARTNERS + GROUP; } //by default show the companies only var mask = DEFAULT_MASK; //if an action is present in URL, we need to show the actions if (DEFAULT_ACTION_ID != null) { mask = ACTION_PAST + ACTION_FUTURE + ACTION_EVENT; } //or if a company id is present, then we show the companies //which is the default anyway else if (DEFAULT_ACTION_ID != null) { mask = AMBASSADORS + BRAND + COLLECTIVITY + PARTNERS + GROUP; } var DEFAULT_RADIUS = 1000000; //1000 Km var radius = DEFAULT_RADIUS; var DEFAULT_CENTER = null; var current_center = null; window.addEvent('domready', function() { initialize(); DEFAULT_CENTER = map.getCenter(); current_center = DEFAULT_CENTER; loadMarkers(map.getZoom(), map.getBounds(), current_center, radius, mask); }); function initialize() { if (GBrowserIsCompatible()) { map = new GMap2(document.getElementById("googleMap")); map.setCenter(new GLatLng(DEFAULT_LATITUDE, DEFAULT_LONGITUDE), DEFAULT_ZOOM); //centers the map on France /* Adding controls */ var mapControl = new GMapTypeControl(); //map.addControl(mapControl); map.addControl(new GSmallMapControl()); /* End Adding controls */ GEvent.addListener(map, "zoomend", function(oldLevel, newLevel) { mgr.clearMarkers(); loadMarkers(map.getZoom(), map.getBounds(), current_center, radius, mask); }); GEvent.addListener(map, "moveend", function() { if (!markerOpen) { mgr.clearMarkers(); loadMarkers(map.getZoom(), map.getBounds(), current_center, radius, mask); } }); mgr = new MarkerManager(map); } } //This holds the markers to be shown on the map var points = []; /** * Loops through marker data and create a GMarker for each entry * The GMarkers are specific to an Action * JSON myObject JSON array containing the data */ function addActionPoints(myObject) { for (var i = 0; i < myObject.length; i++) { var action = myObject[i]; points.push(createActionMarker(action)); } } /** * Loops through marker data and create a GMarker for each entry * The GMarkers are specific to a Company (ie. structure) * JSON myObject JSON array containing the data */ function addCompanyPoints(myObject) { for (var i = 0; i < myObject.length; i++) { var company = myObject[i]; points.push(createCompanyMarker(company)); } } /** * Just a convenient method that add the GMarkers in points[] to the GMap */ function prepareGMMap(points) { mgr.clearMarkers(); mgr.addMarkers(points, marker.zoomlevel); mgr.refresh(); } var action_tmp_points = []; var company_tmp_points = []; function reloadMarkers(mask) { reloadActions(mask); reloadCompanies(mask); OPENED = false; } function reloadCompanies(mask) { for(var i = 0; i < points.length; i++) { var marker = points[i]; if(marker.type == 'company') { var marker_mask = -1; switch(marker.structureid) { case 2: marker_mask = GROUP; break; case 3: marker_mask = PARTNERS; break; case 4: marker_mask = COLLECTIVITY; break; case 5: marker_mask = BRAND; break; case 6: marker_mask = AMBASSADORS; break; } if (!marker.isVisible && marker_mask & mask) { mgr.addMarker(marker, marker.zoomlevel); marker.isVisible = !marker.isVisible; if (OPENED && marker.id == DEFAULT_COMPANY_ID) { GEvent.trigger(marker, 'click'); } } else if (marker.isVisible && !(marker_mask & mask)) { mgr.removeMarker(marker); marker.isVisible = !marker.isVisible; } } } } function reloadActions(mask) { for(var i = 0; i < points.length; i++) { var marker = points[i]; if(marker.type == 'action') { if (marker.isEvent) //marker is an Event { if (!(mask & ACTION_EVENT)) { mgr.removeMarker(marker); } else { mgr.addMarker(marker, marker.zoomlevel); } } else // normal marker { var show = (marker.ispast && (mask & ACTION_PAST)) || (!marker.ispast && (mask & ACTION_FUTURE)); if (!show && marker.isVisible) { mgr.removeMarker(marker); marker.isVisible = !marker.isVisible; } else if (show && !marker.isVisible) { mgr.addMarker(marker, marker.zoomlevel); marker.isVisible = !marker.isVisible; } } if (OPENED && marker.id == DEFAULT_ACTION_ID) { GEvent.trigger(marker, 'click'); } } } } /* * This is the method call anytime the GMap is drawn or redrawn * int zoomlevel current zoomlevel * GLatLngBounds bounds boundary of the GMap * int mask bitmap representing the action/companies to display on the map */ function loadMarkers(zoomlevel, bounds, center, radius, mask) { //first of all we reset the list of GMarkers points = []; loadActions(zoomlevel, bounds, center, radius, function(e) { //add the GMarkers and place loadCompanies() as the callback addActionPoints(e); loadCompanies(zoomlevel, bounds, center, radius, mask, function(e) //this is the callback when loadCompanies() terminates { //add the GMmarkers and display addCompanyPoints(e); reloadMarkers(mask); } ); } ); } /* * Makes an Ajax call to retrieve a JSON array containing the actions * int zoomlevel current zoomlevel * GLatLngBounds bounds boundary of the GMap * int mask bitmap representing the action/companies to display on the map * Function callback function to call when Ajax call terminates */ function loadActions(zoomlevel, bounds, center, radius, callback) { var southwest = bounds.getSouthWest(); var northeast = bounds.getNorthEast(); //the query string var url = "/fair_friends/ajax/getActions.php?zoomlevel=" + zoomlevel + "&swlat=" + southwest.lat() + "&swlong=" + southwest.lng() + "&nelat=" + northeast.lat() + "&nelong=" + northeast.lng() + "¢erlat=" + center.lat() + "¢erlong=" + center.lng() + "&radius=" + radius; var myAjax = new Ajax( url, { async : true, method: 'get', onComplete: function(e){ if (callback != null) { callback(eval(e)); } } } ).request(); } /* * Makes an Ajax call to retrieve a JSON array containing the companies * int zoomlevel current zoomlevel * GLatLngBounds bounds boundary of the GMap * Function callback function to call when Ajax call terminates */ function loadCompanies(zoomlevel, bounds, center, radius, mask, callback) { var structureids = []; //for every type of company present in mask, we add the company's id to structureids //Those company's id are the ones from the DB. Here they are harcoded for simplicity purposes. //TODO: make those ID dynamic if(mask & AMBASSADORS) { structureids.push(6); } if(mask & BRAND) { structureids.push(5); } if(mask & COLLECTIVITY) { structureids.push(4); } if(mask & PARTNERS) { structureids.push(3); } if(mask & GROUP) { structureids.push(2); } var southwest = bounds.getSouthWest(); var northeast = bounds.getNorthEast(); var url = "/fair_friends/ajax/getCompanies.php?zoomlevel=" + zoomlevel + "&swlat=" + southwest.lat() + "&swlong=" + southwest.lng() + "&nelat=" + northeast.lat() + "&nelong=" + northeast.lng() + "¢erlat=" + center.lat() + "¢erlong=" + center.lng() + "&radius=" + radius; //adds the company ids to the query string for(var i = 0; i < structureids.length; i++) { url += "&structure_ids[]=" + structureids[i]; } var myAjax = new Ajax( url, { async : true, method: 'get', onComplete: function(e){ if (callback != null) { callback(eval(e)); } } } ).request(); } /** * Create a GMarker for an action * JSON hd JSON array containing the data of 1 action * returns The GMarker */ function createActionMarker(hd) { var d = new Date(); var t = d.getTime() / 1000; //in seconds var isPast = hd.date_end < t; var isEvent = hd.event == 1 ? true : false; var actionIcon = new GIcon(G_DEFAULT_ICON); actionIcon.iconSize = new GSize(29, 28); actionIcon.shadowSize = new GSize(0, 0); //no shadow if (isEvent) { actionIcon.image = "/fair_friends/img/googleMap/picto_map_8.gif"; } else { switch(isPast) { case true: actionIcon.image = "/fair_friends/img/googleMap/picto_map_7.gif"; break; default: actionIcon.image = "/fair_friends/img/googleMap/picto_map_6.gif"; break; } } markerOptions = { icon:actionIcon, clickable: true, title: hd.title }; var point = new GLatLng(hd.latitude, hd.longitude); var marker = new GMarker(point, markerOptions); marker.type = 'action'; marker.id = hd.id; marker.date_start = hd.date_start; marker.date_end = hd.date_end; marker.ispast = isPast; marker.isVisible = false; marker.zoomlevel = hd.zoomlevel; marker.isEvent = isEvent; //when the icon is clicked, open up the info window GEvent.addListener( marker, "click", function() { if(opening == false){ opening = true; tabs = []; currentMarker = marker; if(hd.video_url != null && hd.video_url != "") //only add a Multemedia tab if video_url is there { getActorContent(hd, marker, getMultimediaContent); } else { getActorContent(hd, marker); } } return false; } ); GEvent.addListener( marker, "infowindowclose", function() { currentMarker = null; markerOpen = false; } ); return marker; } /** * Create a GMarker for a company * JSON hd JSON array containing the data of 1 company * returns The GMarker */ function createCompanyMarker(hd) { var actionIcon = new GIcon(G_DEFAULT_ICON); //picto is changing depending on the type of company switch(hd.structure_id) { case 2 : //groups actionIcon.image = "/fair_friends/img/googleMap/picto_map_1.gif"; break; case 3 : //partners actionIcon.image = "/fair_friends/img/googleMap/picto_map_5.gif"; break; case 4 : //collectivity actionIcon.image = "/fair_friends/img/googleMap/picto_map_2.gif"; break; case 5 : //brands actionIcon.image = "/fair_friends/img/googleMap/picto_map_4.gif"; break; case 6 : //ambassadors actionIcon.image = "/fair_friends/img/googleMap/picto_map_3.gif"; break; } actionIcon.iconSize = new GSize(29, 28); actionIcon.shadowSize = new GSize(0, 0); //no shadow markerOptions = { icon:actionIcon, clickable: true, title: hd.goal }; var point = new GLatLng(hd.aRegistrationUserUk.latitude , hd.aRegistrationUserUk.longitude); var marker = new GMarker(point, markerOptions); marker.type = 'company'; marker.id = hd.id; marker.structureid = hd.structure_id; marker.isVisible = false; marker.zoomlevel = hd.aRegistrationUserUk.zoomlevel; GEvent.addListener( marker, "click", function() { tabs = []; currentMarker = marker; getCompanyContent(hd, marker); } ); GEvent.addListener( marker, "infowindowclose", function() { currentMarker = null; markerOpen = false; } ); return marker; } /** * Retrieve the content of an infowindow for a company * JSON hd The company data * GMarker the marker on the GMap */ function getCompanyContent(hd, marker) { //url to query var url = "/fair_friends/ajax/getCompanyContent.php?type=info&id=" + hd.id; var myAjax = new Ajax( url, { async : true, method: 'get', onComplete: function(e){ marker.openInfoWindowHtml(e); markerOpen = true; } } ).request(); } /** * Retrieve the actor content of an infowindow for an action * JSON hd The action data * GMarker the marker on the GMap */ var opening = false; function getActorContent(hd, marker, callback) { var label = "Action"; var url = "/fair_friends/ajax/getMarkerContent.php?type=info&id=" + hd.id; var myAjax = new Ajax( url, { async : true, method: 'get', onComplete: function(e){ var t = new GInfoWindowTab(label, e); tabs.push(t); // declared in createActionMarker() if (callback == null) { marker.openInfoWindowTabsHtml(tabs); markerOpen = true; } else { callback(hd, marker); } opening = false; } } ).request(); } /** * Retrieve the multimedia content of an infowindow for an action * JSON hd The action data * GMarker the marker on the GMap */ function getMultimediaContent(hd, marker) { var label = "Multimédia"; var url = "/fair_friends/ajax/getMarkerContent.php?type=multimedia&id=" + hd.id; var myAjax = new Ajax( url, { async : true, method: 'get', onComplete: function(e){ var t = new GInfoWindowTab(label, e); tabs.push(t); // declared in createActionMarker() marker.openInfoWindowTabsHtml(tabs); markerOpen = true; } } ).request(); }