comparison media/js/membermap.js @ 1:dbd703f7d63a

Initial import of sg101 stuff from private repository.
author gremmie
date Mon, 06 Apr 2009 02:43:12 +0000
parents
children c515b7401078
comparison
equal deleted inserted replaced
0:900ba3c7b765 1:dbd703f7d63a
1 var mmap = {
2 map: null,
3 geocoder: null,
4 users: Object,
5 userOnMap: false,
6 userClick: function() {
7 var name = $('option:selected', this).text();
8 if (name != mmap.selectText)
9 {
10 mmap.clickUser(name);
11 }
12 },
13 clickUser: function(name) {
14 pt = new GLatLng(mmap.users[name].lat, mmap.users[name].lon);
15 mmap.map.setCenter(pt);
16 mmap.users[name].marker.openInfoWindowHtml(mmap.users[name].message);
17 },
18 clear: function() {
19 mmap.users.length = 0;
20 },
21 resizeUserList: function() {
22 var sel = $('#member_map_members');
23 sel[0].size = Math.min(29, sel[0].length);
24 $('#member_map_count').html(sel[0].length);
25 },
26 selectText: "(select)",
27 onMapDir: 'You have previously added yourself to the member map. Your information appears below. You may change ' +
28 'the information if you wish. To delete yourself from the map, click the Delete button.',
29 offMapDir: 'Your location is not on the map. If you would like to appear on the map, please fill out the form below ' +
30 'and click the Submit button.'
31 };
32 $(document).ready(function() {
33 $('#id_message').markItUp(mySettings);
34 if (GBrowserIsCompatible())
35 {
36 $(window).unload(GUnload);
37 mmap.map = new GMap2($('#member_map_map')[0]);
38 mmap.map.setCenter(new GLatLng(15.0, -30.0), 2);
39 mmap.map.enableScrollWheelZoom();
40 mmap.map.addControl(new GLargeMapControl());
41 mmap.map.addControl(new GMapTypeControl());
42 mmap.geocoder = new GClientGeocoder();
43
44 if (mmapUser.userName)
45 {
46 $.getJSON('/member_map/query/',
47 function(data) {
48 mmap.map.clearOverlays();
49 var sel = $('#member_map_members');
50 sel[0].length = 0;
51 mmap.clear();
52 $.each(data.users, function(i, item) {
53 sel.append($('<option />').html(item.name));
54 var marker = new GMarker(new GLatLng(item.lat, item.lon));
55 marker.bindInfoWindowHtml(item.message);
56 mmap.map.addOverlay(marker);
57 mmap.users[item.name] = item;
58 mmap.users[item.name].marker = marker;
59 if (mmapUser.userName == item.name)
60 {
61 mmap.userOnMap = true;
62 }
63 });
64 sel[0].size = Math.min(29, data.users.length);
65 $('#member_map_count').html(data.users.length);
66
67 sel = $('#member_map_recent');
68 sel[0].length = 0;
69 sel.append($('<option />').html(mmap.selectText));
70 $.each(data.recent, function(i, item) {
71 sel.append($('<option />').html(item));
72 });
73 var submitButton = $('#member_map_submit');
74 var deleteButton = $('#member_map_delete');
75
76 submitButton.click(function() {
77 if (mmap.geocoder)
78 {
79 $(this).attr('disabled', 'disabled').val('Updating Map...');
80 var address = $('#id_location').val();
81 mmap.geocoder.getLatLng(address,
82 function(point) {
83 if (!point)
84 {
85 alert(address + ' could not be found on Google Maps.');
86 return;
87 }
88 $.post('/member_map/add/', {
89 loc : address,
90 lat : point.lat(),
91 lon : point.lng(),
92 msg : $('#id_message').val()
93 },
94 function(data, textStatus) {
95 var wasOnMap = mmap.userOnMap;
96 if (mmap.userOnMap)
97 {
98 mmap.map.removeOverlay(mmap.users[mmapUser.userName].marker);
99 }
100 else
101 {
102 $('#member_map_members').append($('<option />').html(data.name));
103 mmap.userOnMap = true;
104 deleteButton.removeAttr('disabled').val('Delete');
105 }
106 var marker = new GMarker(new GLatLng(data.lat, data.lon));
107 marker.bindInfoWindowHtml(data.message);
108 mmap.map.addOverlay(marker);
109 mmap.users[mmapUser.userName] = data;
110 mmap.users[mmapUser.userName].marker = marker;
111 mmap.clickUser(mmapUser.userName);
112 submitButton.removeAttr('disabled').val('Update');
113 $('#member_map_directions').html(mmap.onMapDir);
114 mmap.resizeUserList();
115 alert(wasOnMap ? "Your location has been updated!" :
116 "You've been added to the map!");
117 },
118 'json');
119 });
120 }
121 return false;
122 });
123
124 deleteButton.click(function() {
125 deleteButton.attr('disabled', 'disabled').val('Deleting...');
126 $.post('/member_map/delete/', function(data, textStatus) {
127 $('#id_location').val('');
128 $('#id_message').val('');
129 $("#member_map_members option[value='" + mmapUser.userName + "']").remove();
130 $("#member_map_recent option[value='" + mmapUser.userName + "']").remove();
131 mmap.map.removeOverlay(mmap.users[mmapUser.userName].marker);
132 mmap.users[mmapUser.userName].marker = null;
133 mmap.users[mmapUser.userName] = null;
134 mmap.userOnMap = false;
135 deleteButton.val('Delete');
136 submitButton.removeAttr('disabled').val('Add');
137 $('#member_map_directions').html(mmap.offMapDir);
138 mmap.resizeUserList();
139 alert("You've been removed from the map.");
140 },
141 'text');
142 return false;
143 });
144
145 if (mmap.userOnMap)
146 {
147 submitButton.val('Update');
148 $('#member_map_directions').html(mmap.onMapDir);
149 }
150 else
151 {
152 submitButton.val('Add');
153 deleteButton.attr('disabled', 'disabled');
154 $('#member_map_directions').html(mmap.offMapDir);
155 }
156 });
157 $('#member_map_members').change(mmap.userClick);
158 $('#member_map_recent').change(mmap.userClick);
159 }
160 }
161 });