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