gremmie@1: var mmap = {
gremmie@1: map: null,
gremmie@1: geocoder: null,
gremmie@1: users: Object,
gremmie@1: userOnMap: false,
gremmie@1: userClick: function() {
gremmie@1: var name = $('option:selected', this).text();
gremmie@1: if (name != mmap.selectText)
gremmie@1: {
gremmie@1: mmap.clickUser(name);
gremmie@1: }
gremmie@1: },
gremmie@1: clickUser: function(name) {
gremmie@1: pt = new GLatLng(mmap.users[name].lat, mmap.users[name].lon);
gremmie@1: mmap.map.setCenter(pt);
gremmie@1: mmap.users[name].marker.openInfoWindowHtml(mmap.users[name].message);
gremmie@1: },
gremmie@1: clear: function() {
gremmie@1: mmap.users.length = 0;
gremmie@1: },
gremmie@1: selectText: "(select)",
gremmie@1: onMapDir: 'You have previously added yourself to the member map. Your information appears below. You may change ' +
gremmie@1: 'the information if you wish. To delete yourself from the map, click the Delete button.',
gremmie@1: offMapDir: 'Your location is not on the map. If you would like to appear on the map, please fill out the form below ' +
gremmie@1: 'and click the Submit button.'
gremmie@1: };
gremmie@1: $(document).ready(function() {
gremmie@1: if (GBrowserIsCompatible())
gremmie@1: {
gremmie@1: $(window).unload(GUnload);
gremmie@1: mmap.map = new GMap2($('#member_map_map')[0]);
gremmie@1: mmap.map.setCenter(new GLatLng(15.0, -30.0), 2);
gremmie@1: mmap.map.enableScrollWheelZoom();
gremmie@1: mmap.map.addControl(new GLargeMapControl());
gremmie@1: mmap.map.addControl(new GMapTypeControl());
gremmie@1: mmap.geocoder = new GClientGeocoder();
gremmie@1:
gremmie@1: if (mmapUser.userName)
gremmie@1: {
gremmie@1: $.getJSON('/member_map/query/',
gremmie@1: function(data) {
gremmie@1: mmap.map.clearOverlays();
gremmie@1: var sel = $('#member_map_members');
gremmie@1: sel[0].length = 0;
bgneal@153: sel.append($('').html(mmap.selectText));
gremmie@1: mmap.clear();
gremmie@1: $.each(data.users, function(i, item) {
gremmie@1: sel.append($('').html(item.name));
gremmie@1: var marker = new GMarker(new GLatLng(item.lat, item.lon));
gremmie@1: marker.bindInfoWindowHtml(item.message);
gremmie@1: mmap.map.addOverlay(marker);
gremmie@1: mmap.users[item.name] = item;
gremmie@1: mmap.users[item.name].marker = marker;
gremmie@1: if (mmapUser.userName == item.name)
gremmie@1: {
gremmie@1: mmap.userOnMap = true;
gremmie@1: }
gremmie@1: });
gremmie@1: $('#member_map_count').html(data.users.length);
gremmie@1:
gremmie@1: sel = $('#member_map_recent');
gremmie@1: sel[0].length = 0;
gremmie@1: sel.append($('').html(mmap.selectText));
gremmie@1: $.each(data.recent, function(i, item) {
gremmie@1: sel.append($('').html(item));
gremmie@1: });
gremmie@1: var submitButton = $('#member_map_submit');
gremmie@1: var deleteButton = $('#member_map_delete');
gremmie@1:
gremmie@1: submitButton.click(function() {
gremmie@1: if (mmap.geocoder)
gremmie@1: {
gremmie@1: $(this).attr('disabled', 'disabled').val('Updating Map...');
gremmie@1: var address = $('#id_location').val();
gremmie@1: mmap.geocoder.getLatLng(address,
gremmie@1: function(point) {
gremmie@1: if (!point)
gremmie@1: {
gremmie@1: alert(address + ' could not be found on Google Maps.');
bgneal@184: submitButton.removeAttr('disabled').val('Update');
gremmie@1: return;
gremmie@1: }
bgneal@150: $.ajax({
bgneal@150: url: '/member_map/add/',
bgneal@150: type: 'POST',
bgneal@150: data: {
bgneal@150: loc : address,
bgneal@150: lat : point.lat(),
bgneal@150: lon : point.lng(),
bgneal@150: msg : $('#id_message').val()
gremmie@1: },
bgneal@150: dataType: 'json',
bgneal@150: success: function(data, textStatus) {
gremmie@1: var wasOnMap = mmap.userOnMap;
gremmie@1: if (mmap.userOnMap)
gremmie@1: {
gremmie@1: mmap.map.removeOverlay(mmap.users[mmapUser.userName].marker);
gremmie@1: }
gremmie@1: else
gremmie@1: {
gremmie@1: $('#member_map_members').append($('').html(data.name));
bgneal@153: $('#member_map_recent').append($('').html(data.name));
gremmie@1: mmap.userOnMap = true;
gremmie@1: deleteButton.removeAttr('disabled').val('Delete');
gremmie@1: }
gremmie@1: var marker = new GMarker(new GLatLng(data.lat, data.lon));
gremmie@1: marker.bindInfoWindowHtml(data.message);
gremmie@1: mmap.map.addOverlay(marker);
gremmie@1: mmap.users[mmapUser.userName] = data;
gremmie@1: mmap.users[mmapUser.userName].marker = marker;
gremmie@1: mmap.clickUser(mmapUser.userName);
gremmie@1: submitButton.removeAttr('disabled').val('Update');
gremmie@1: $('#member_map_directions').html(mmap.onMapDir);
bgneal@153: $('#member_map_count').html($('#member_map_members')[0].length - 1);
gremmie@1: alert(wasOnMap ? "Your location has been updated!" :
gremmie@1: "You've been added to the map!");
gremmie@1: },
bgneal@150: error: function (xhr, textStatus, ex) {
bgneal@150: alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@150: xhr.responseText);
bgneal@150: }
bgneal@150: });
gremmie@1: });
gremmie@1: }
gremmie@1: return false;
gremmie@1: });
gremmie@1:
gremmie@1: deleteButton.click(function() {
gremmie@1: deleteButton.attr('disabled', 'disabled').val('Deleting...');
bgneal@150: $.ajax({
bgneal@150: url: '/member_map/delete/',
bgneal@150: type: 'POST',
bgneal@150: dataType: 'text',
bgneal@150: success: function(data, textStatus) {
gremmie@1: $('#id_location').val('');
gremmie@1: $('#id_message').val('');
gremmie@1: $("#member_map_members option[value='" + mmapUser.userName + "']").remove();
gremmie@1: $("#member_map_recent option[value='" + mmapUser.userName + "']").remove();
gremmie@1: mmap.map.removeOverlay(mmap.users[mmapUser.userName].marker);
gremmie@1: mmap.users[mmapUser.userName].marker = null;
gremmie@1: mmap.users[mmapUser.userName] = null;
gremmie@1: mmap.userOnMap = false;
gremmie@1: deleteButton.val('Delete');
gremmie@1: submitButton.removeAttr('disabled').val('Add');
gremmie@1: $('#member_map_directions').html(mmap.offMapDir);
bgneal@153: $('#member_map_count').html($('#member_map_members')[0].length - 1);
gremmie@1: alert("You've been removed from the map.");
gremmie@1: },
bgneal@150: error: function (xhr, textStatus, ex) {
bgneal@150: alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@150: xhr.responseText);
bgneal@150: }
bgneal@150: });
gremmie@1: return false;
gremmie@1: });
gremmie@1:
gremmie@1: if (mmap.userOnMap)
gremmie@1: {
gremmie@1: submitButton.val('Update');
gremmie@1: $('#member_map_directions').html(mmap.onMapDir);
gremmie@1: }
gremmie@1: else
gremmie@1: {
gremmie@1: submitButton.val('Add');
gremmie@1: deleteButton.attr('disabled', 'disabled');
gremmie@1: $('#member_map_directions').html(mmap.offMapDir);
gremmie@1: }
gremmie@1: });
gremmie@1: $('#member_map_members').change(mmap.userClick);
gremmie@1: $('#member_map_recent').change(mmap.userClick);
gremmie@1: }
gremmie@1: }
gremmie@1: });