2011-03-17 20:53:05 +00:00
|
|
|
$(function() {
|
2011-03-18 02:09:59 +00:00
|
|
|
var center = new google.maps.LatLng(42.358431, -71.059773);
|
|
|
|
var zoomLevel = 15;
|
2011-03-18 02:33:20 +00:00
|
|
|
var mapOptions = {
|
2011-03-18 02:09:59 +00:00
|
|
|
center: center,
|
2011-03-17 20:53:05 +00:00
|
|
|
mapTypeControl: false,
|
2011-03-17 23:37:15 +00:00
|
|
|
mapTypeId: google.maps.MapTypeId.ROADMAP,
|
|
|
|
panControl: false,
|
2011-03-18 02:09:59 +00:00
|
|
|
zoom: zoomLevel
|
2011-03-17 20:53:05 +00:00
|
|
|
};
|
2011-03-18 02:33:20 +00:00
|
|
|
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
|
2011-03-17 20:53:05 +00:00
|
|
|
var activeHydrantId;
|
|
|
|
var activeMarker;
|
|
|
|
var activeInfoWindow;
|
2011-03-17 21:40:47 +00:00
|
|
|
var hydrantIds = [];
|
2011-03-17 20:53:05 +00:00
|
|
|
function addMarker(hydrantId, point, color) {
|
2011-03-18 02:09:59 +00:00
|
|
|
if($.inArray(hydrantId, hydrantIds) != -1) {
|
|
|
|
return;
|
|
|
|
}
|
2011-03-17 20:53:05 +00:00
|
|
|
var image = new google.maps.MarkerImage(color,
|
|
|
|
new google.maps.Size(27.0, 37.0),
|
|
|
|
new google.maps.Point(0, 0),
|
|
|
|
new google.maps.Point(13.0, 18.0)
|
|
|
|
);
|
|
|
|
var shadow = new google.maps.MarkerImage('/images/markers/shadow.png',
|
|
|
|
new google.maps.Size(46.0, 37.0),
|
|
|
|
new google.maps.Point(0, 0),
|
|
|
|
new google.maps.Point(13.0, 18.0)
|
|
|
|
);
|
|
|
|
var marker = new google.maps.Marker({
|
2011-03-17 21:40:47 +00:00
|
|
|
animation: google.maps.Animation.DROP,
|
2011-03-17 20:53:05 +00:00
|
|
|
icon: image,
|
2011-03-17 21:40:47 +00:00
|
|
|
map: map,
|
|
|
|
position: point,
|
2011-03-17 20:53:05 +00:00
|
|
|
shadow: shadow
|
|
|
|
});
|
|
|
|
google.maps.event.addListener(marker, 'click', function() {
|
|
|
|
if(activeInfoWindow) {
|
|
|
|
activeInfoWindow.close();
|
|
|
|
}
|
2011-03-18 02:33:20 +00:00
|
|
|
$.get('<%= contents_path %>', {
|
2011-03-17 20:53:05 +00:00
|
|
|
'hydrant_id': hydrantId
|
|
|
|
}, function(data) {
|
|
|
|
var infoWindow = new google.maps.InfoWindow({
|
|
|
|
content: data,
|
|
|
|
maxWidth: 350
|
|
|
|
});
|
|
|
|
infoWindow.open(map, marker);
|
|
|
|
activeHydrantId = hydrantId;
|
|
|
|
activeMarker = marker;
|
|
|
|
activeInfoWindow = infoWindow;
|
|
|
|
});
|
|
|
|
});
|
2011-03-17 21:40:47 +00:00
|
|
|
hydrantIds.push(hydrantId);
|
2011-03-17 20:53:05 +00:00
|
|
|
}
|
2011-03-17 23:37:15 +00:00
|
|
|
function addMarkersAround(lat, lng) {
|
|
|
|
$.get('<%= hydrants_path :format => "json" %>', {
|
2011-03-18 02:33:20 +00:00
|
|
|
'commit': $('#address_submit').val(),
|
2011-03-17 23:37:15 +00:00
|
|
|
'utf8': '✓',
|
2011-03-18 02:33:20 +00:00
|
|
|
'authenticity_token': $('#address_form input[name="authenticity_token"]').val(),
|
2011-03-17 23:37:15 +00:00
|
|
|
'lat': lat,
|
|
|
|
'lng': lng
|
|
|
|
}, function(data) {
|
|
|
|
if(data.errors) {
|
|
|
|
$('#address_label').addClass('error', 500);
|
|
|
|
$('#address').addClass('error', 500);
|
|
|
|
$('#address').focus();
|
|
|
|
} else {
|
2011-03-18 02:09:59 +00:00
|
|
|
var northMost;
|
|
|
|
var eastMost;
|
|
|
|
var southMost;
|
|
|
|
var westMost;
|
2011-03-17 23:37:15 +00:00
|
|
|
var i = 0;
|
|
|
|
$(data).each(function(index, hydrant) {
|
|
|
|
hydrant = hydrant.hydrant;
|
2011-03-18 02:09:59 +00:00
|
|
|
if(!northMost || northMost > hydrant.lat) {
|
|
|
|
northMost = hydrant.lat;
|
|
|
|
}
|
|
|
|
if(!eastMost || eastMost > hydrant.lng) {
|
|
|
|
eastMost = hydrant.lng;
|
|
|
|
}
|
|
|
|
if(!southMost || southMost < hydrant.lat) {
|
|
|
|
southMost = hydrant.lat;
|
|
|
|
}
|
|
|
|
if(!westMost || westMost < hydrant.lng) {
|
|
|
|
westMost = hydrant.lng;
|
2011-03-17 23:37:15 +00:00
|
|
|
}
|
|
|
|
setTimeout(function() {
|
|
|
|
point = new google.maps.LatLng(hydrant.lat, hydrant.lng);
|
|
|
|
color = '/images/markers/' + (hydrant.user_id ? 'green' : 'red') + '.png';
|
|
|
|
addMarker(hydrant.id, point, color);
|
|
|
|
}, i * 100);
|
2011-03-18 02:09:59 +00:00
|
|
|
if($.inArray(hydrant.id, hydrantIds) == -1) {
|
|
|
|
i += 1;
|
|
|
|
}
|
2011-03-17 23:37:15 +00:00
|
|
|
});
|
2011-03-18 02:09:59 +00:00
|
|
|
southWest = new google.maps.LatLng(southMost, westMost);
|
|
|
|
northEast = new google.maps.LatLng(northMost, eastMost);
|
|
|
|
bounds = new google.maps.LatLngBounds(southWest, northEast);
|
|
|
|
map.fitBounds(bounds);
|
2011-03-17 23:37:15 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
google.maps.event.addListener(map, 'dragend', function() {
|
|
|
|
center = map.getCenter();
|
|
|
|
addMarkersAround(center.lat(), center.lng());
|
|
|
|
});
|
2011-03-18 02:33:20 +00:00
|
|
|
$('#address_form').submit(function() {
|
2011-03-17 20:53:05 +00:00
|
|
|
if($('#address').val() === '') {
|
|
|
|
$('#address_label').addClass('error', 500);
|
|
|
|
$('#address').addClass('error', 500);
|
2011-03-17 21:15:42 +00:00
|
|
|
$('#address').focus();
|
2011-03-17 20:53:05 +00:00
|
|
|
} else {
|
|
|
|
$.get('<%= address_path :format => "json" %>', {
|
2011-03-18 02:33:20 +00:00
|
|
|
'commit': $('#address_submit').val(),
|
2011-03-17 20:53:05 +00:00
|
|
|
'utf8': '✓',
|
2011-03-18 02:33:20 +00:00
|
|
|
'authenticity_token': $('#address_form input[name="authenticity_token"]').val(),
|
2011-03-17 20:53:05 +00:00
|
|
|
'city_state': $('#city_state').val(),
|
|
|
|
'address': $('#address').val()
|
|
|
|
}, function(data) {
|
|
|
|
if(data.errors) {
|
|
|
|
$('#address_label').addClass('error', 500);
|
|
|
|
$('#address').addClass('error', 500);
|
2011-03-17 21:15:42 +00:00
|
|
|
$('#address').focus();
|
2011-03-17 20:53:05 +00:00
|
|
|
} else {
|
2011-03-17 23:37:15 +00:00
|
|
|
addMarkersAround(data[0], data[1]);
|
2011-03-17 20:53:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
$('#combo_form input[type="radio"]').live('click', function() {
|
|
|
|
var self = $(this);
|
|
|
|
if('new' == self.val()) {
|
|
|
|
$('#user_forgot_password_fields').slideUp();
|
2011-03-17 21:15:42 +00:00
|
|
|
$('#user_sign_in_fields').slideUp();
|
|
|
|
$('#user_sign_up_fields').slideDown();
|
|
|
|
$('#combo_form').data('state', 'user_sign_up');
|
2011-03-17 20:53:05 +00:00
|
|
|
} else if('existing' == self.val()) {
|
2011-03-17 21:15:42 +00:00
|
|
|
$('#user_sign_up_fields').slideUp();
|
|
|
|
$('#user_sign_in_fields').slideDown(function() {
|
|
|
|
$('#combo_form').data('state', 'user_sign_in');
|
2011-03-17 20:53:05 +00:00
|
|
|
$('#user_forgot_password_link').click(function() {
|
2011-03-17 21:15:42 +00:00
|
|
|
$('#user_sign_in_fields').slideUp();
|
2011-03-17 20:53:05 +00:00
|
|
|
$('#user_forgot_password_fields').slideDown(function() {
|
|
|
|
$('#user_remembered_password').click(function() {
|
|
|
|
$('#user_forgot_password_fields').slideUp();
|
2011-03-17 21:15:42 +00:00
|
|
|
$('#user_sign_in_fields').slideDown();
|
|
|
|
$('#combo_form').data('state', 'user_sign_in');
|
2011-03-17 20:53:05 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
$('#combo_form').data('state', 'user_forgot_password');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$('#combo_form').live('submit', function() {
|
|
|
|
var errors = []
|
|
|
|
if(!/[\w\.%\+\]+@[\w\]+\.+[\w]{2,}/.test($('#user_email').val())) {
|
|
|
|
errors.push($('#user_email'));
|
|
|
|
$('#user_email_label').addClass('error', 500);
|
|
|
|
$('#user_email').addClass('error', 500);
|
|
|
|
} else {
|
|
|
|
$('#user_email_label').removeClass('error');
|
|
|
|
$('#user_email').removeClass('error');
|
|
|
|
}
|
2011-03-17 21:15:42 +00:00
|
|
|
if(!$(this).data('state') || $(this).data('state') === 'user_sign_up') {
|
2011-03-17 20:53:05 +00:00
|
|
|
if($('#user_name').val() === '') {
|
|
|
|
errors.push($('#user_name'));
|
|
|
|
$('#user_name_label').addClass('error', 500);
|
|
|
|
$('#user_name').addClass('error', 500);
|
|
|
|
} else {
|
|
|
|
$('#user_name_label').removeClass('error');
|
|
|
|
$('#user_name').removeClass('error');
|
|
|
|
}
|
|
|
|
if($('#user_password_confirmation').val().length < 6 || $('#user_password_confirmation').val().length > 20) {
|
|
|
|
errors.push($('#user_password_confirmation'));
|
|
|
|
$('#user_password_confirmation_label').addClass('error', 500);
|
|
|
|
$('#user_password_confirmation').addClass('error', 500);
|
|
|
|
} else {
|
|
|
|
$('#user_password_confirmation_label').removeClass('error');
|
|
|
|
$('#user_password_confirmation').removeClass('error');
|
|
|
|
}
|
|
|
|
if(errors.length > 0) {
|
|
|
|
errors[0].focus();
|
|
|
|
} else {
|
|
|
|
$.post('<%= user_registration_path :format => "json" %>', {
|
2011-03-17 21:15:42 +00:00
|
|
|
'commit': $('#user_sign_up_submit').val(),
|
2011-03-17 20:53:05 +00:00
|
|
|
'utf8': '✓',
|
2011-03-18 02:50:47 +00:00
|
|
|
'authenticity_token': $('#combo_form input[name="authenticity_token"]').val(),
|
2011-03-17 20:53:05 +00:00
|
|
|
'user': {
|
|
|
|
'email': $('#user_email').val(),
|
|
|
|
'name': $('#user_name').val(),
|
|
|
|
'organization': $('#user_organization').val(),
|
|
|
|
'voice_number': $('#user_voice_number').val(),
|
|
|
|
'sms_number': $('#user_sms_number').val(),
|
|
|
|
'password': $('#user_password_confirmation').val(),
|
|
|
|
'password_confirmation': $('#user_password_confirmation').val()
|
|
|
|
}
|
|
|
|
}, function(data) {
|
|
|
|
if(data.errors) {
|
|
|
|
if(data.errors.email) {
|
|
|
|
errors.push($('#user_email'));
|
|
|
|
$('#user_email_label').addClass('error', 500);
|
|
|
|
$('#user_email').addClass('error', 500);
|
|
|
|
}
|
|
|
|
if(data.errors.name) {
|
|
|
|
errors.push($('#user_name'));
|
|
|
|
$('#user_name_label').addClass('error', 500);
|
|
|
|
$('#user_name').addClass('error', 500);
|
|
|
|
}
|
|
|
|
if(data.errors.organization) {
|
|
|
|
errors.push($('#user_organization'));
|
|
|
|
$('#user_organization_label').addClass('error', 500);
|
|
|
|
$('#user_organization').addClass('error', 500);
|
|
|
|
}
|
|
|
|
if(data.errors.voice_number) {
|
|
|
|
errors.push($('#user_voice_number'));
|
|
|
|
$('#user_voice_number_label').addClass('error', 500);
|
|
|
|
$('#user_voice_number').addClass('error', 500);
|
|
|
|
}
|
|
|
|
if(data.errors.sms_number) {
|
|
|
|
errors.push($('#user_sms_number'));
|
|
|
|
$('#user_sms_number_label').addClass('error', 500);
|
|
|
|
$('#user_sms_number').addClass('error', 500);
|
|
|
|
}
|
|
|
|
if(data.errors.password) {
|
|
|
|
errors.push($('#user_password_confirmation'));
|
|
|
|
$('#user_password_confirmation_label').addClass('error', 500);
|
|
|
|
$('#user_password_confirmation').addClass('error', 500);
|
|
|
|
}
|
|
|
|
errors[0].focus();
|
|
|
|
} else {
|
2011-03-18 02:33:20 +00:00
|
|
|
$.get('<%= contents_path %>', {
|
2011-03-17 20:53:05 +00:00
|
|
|
'hydrant_id': activeHydrantId
|
|
|
|
}, function(data) {
|
|
|
|
activeInfoWindow.setContent(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-03-17 21:15:42 +00:00
|
|
|
} else if($(this).data('state') === 'user_sign_in') {
|
2011-03-17 20:53:05 +00:00
|
|
|
if($('#user_password').val().length < 6 || $('#user_password').val().length > 20) {
|
|
|
|
errors.push($('#user_password'));
|
|
|
|
$('#user_password_label').addClass('error', 500);
|
|
|
|
$('#user_password').addClass('error', 500);
|
|
|
|
} else {
|
|
|
|
$('#user_password_label').removeClass('error');
|
|
|
|
$('#user_password').removeClass('error');
|
|
|
|
}
|
|
|
|
if(errors.length > 0) {
|
|
|
|
errors[0].focus();
|
|
|
|
} else {
|
|
|
|
$.post('<%= user_session_path :format => "json" %>', {
|
2011-03-17 21:15:42 +00:00
|
|
|
'commit': $('#user_sign_in_submit').val(),
|
2011-03-17 20:53:05 +00:00
|
|
|
'utf8': '✓',
|
2011-03-18 02:50:47 +00:00
|
|
|
'authenticity_token': $('#combo_form input[name="authenticity_token"]').val(),
|
2011-03-17 20:53:05 +00:00
|
|
|
'user': {
|
|
|
|
'email': $('#user_email').val(),
|
|
|
|
'password': $('#user_password').val(),
|
|
|
|
'remember_me': $('#user_remember_me').val()
|
|
|
|
}
|
|
|
|
}, function(data) {
|
|
|
|
if(data.errors) {
|
|
|
|
$('#user_password_label').addClass('error', 500);
|
|
|
|
$('#user_password').addClass('error', 500);
|
2011-03-17 21:15:42 +00:00
|
|
|
$('#user_password').focus();
|
2011-03-17 20:53:05 +00:00
|
|
|
} else {
|
2011-03-18 02:33:20 +00:00
|
|
|
$.get('<%= contents_path %>', {
|
2011-03-17 20:53:05 +00:00
|
|
|
'hydrant_id': activeHydrantId
|
|
|
|
}, function(data) {
|
|
|
|
activeInfoWindow.setContent(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if($(this).data('state') === 'user_forgot_password') {
|
|
|
|
if(errors.length > 0) {
|
|
|
|
errors[0].focus();
|
|
|
|
} else {
|
|
|
|
$.post('<%= user_password_path :format => "json" %>', {
|
|
|
|
'commit': $('#user_forgot_password_submit').val(),
|
|
|
|
'utf8': '✓',
|
2011-03-18 02:50:47 +00:00
|
|
|
'authenticity_token': $('#combo_form input[name="authenticity_token"]').val(),
|
2011-03-17 20:53:05 +00:00
|
|
|
'user': {
|
|
|
|
'email': $('#user_email').val()
|
|
|
|
}
|
|
|
|
}, function(data) {
|
|
|
|
if(data.errors) {
|
|
|
|
$('#user_email_label').addClass('error', 500);
|
|
|
|
$('#user_email').addClass('error', 500);
|
2011-03-17 21:15:42 +00:00
|
|
|
$('#user_email').focus();
|
2011-03-17 20:53:05 +00:00
|
|
|
} else {
|
|
|
|
$('#user_forgot_password_fields').slideUp();
|
2011-03-17 21:15:42 +00:00
|
|
|
$('#user_sign_in_fields').slideDown();
|
2011-03-17 20:53:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2011-03-18 02:50:47 +00:00
|
|
|
$('#sign_out_form').live('submit', function() {
|
|
|
|
$.get('<%= destroy_user_session_path :format => "json" %>', {
|
|
|
|
'commit': $('#sign_out_form_submit').val(),
|
|
|
|
'utf8': '✓',
|
|
|
|
'authenticity_token': $('#sign_out_form input[name="authenticity_token"]').val()
|
|
|
|
}, function(data) {
|
|
|
|
$.get('<%= contents_path %>', {
|
|
|
|
'hydrant_id': activeHydrantId
|
|
|
|
}, function(data) {
|
|
|
|
activeInfoWindow.setContent(data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
});
|
2011-03-17 20:53:05 +00:00
|
|
|
$('#adoption_form').live('submit', function() {
|
|
|
|
$.post('<%= hydrant_path :format => "json" %>', {
|
|
|
|
'id': $('#hydrant_id').val(),
|
|
|
|
'commit': $('#adoption_form_submit').val(),
|
|
|
|
'utf8': '✓',
|
2011-03-22 08:45:28 +00:00
|
|
|
'authenticity_token': $('#adoption_form input[name="authenticity_token"]').val(),
|
2011-03-17 20:53:05 +00:00
|
|
|
'_method': 'put',
|
|
|
|
'hydrant': {
|
|
|
|
'user_id': $('#hydrant_user_id').val(),
|
|
|
|
'name': $('#hydrant_name').val()
|
|
|
|
}
|
|
|
|
}, function(data) {
|
2011-03-18 02:33:20 +00:00
|
|
|
$.get('<%= contents_path %>', {
|
2011-03-17 20:53:05 +00:00
|
|
|
'hydrant_id': activeHydrantId
|
|
|
|
}, function(data) {
|
|
|
|
activeInfoWindow.setContent(data);
|
|
|
|
image = new google.maps.MarkerImage('/images/markers/green.png',
|
|
|
|
new google.maps.Size(27.0, 37.0),
|
|
|
|
new google.maps.Point(0, 0),
|
|
|
|
new google.maps.Point(13.0, 18.0)
|
|
|
|
);
|
|
|
|
activeMarker.setIcon(image);
|
|
|
|
activeMarker.setAnimation(google.maps.Animation.BOUNCE);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
});
|
2011-03-22 08:45:44 +00:00
|
|
|
$('#abandon_form').live('submit', function() {
|
|
|
|
var answer = window.confirm("Are you sure you want to abandon this hydrant?")
|
|
|
|
if(answer) {
|
|
|
|
$.post('<%= hydrant_path :format => "json" %>', {
|
|
|
|
'id': $('#hydrant_id').val(),
|
|
|
|
'commit': $('#abandon_form_submit').val(),
|
|
|
|
'utf8': '✓',
|
|
|
|
'authenticity_token': $('#abandon_form input[name="authenticity_token"]').val(),
|
|
|
|
'_method': 'put',
|
|
|
|
'hydrant': {
|
|
|
|
'user_id': $('#hydrant_user_id').val(),
|
|
|
|
'name': $('#hydrant_name').val()
|
|
|
|
}
|
|
|
|
}, function(data) {
|
|
|
|
$.get('<%= contents_path %>', {
|
|
|
|
'hydrant_id': activeHydrantId
|
|
|
|
}, function(data) {
|
|
|
|
activeInfoWindow.setContent(data);
|
|
|
|
image = new google.maps.MarkerImage('/images/markers/red.png',
|
|
|
|
new google.maps.Size(27.0, 37.0),
|
|
|
|
new google.maps.Point(0, 0),
|
|
|
|
new google.maps.Point(13.0, 18.0)
|
|
|
|
);
|
|
|
|
activeMarker.setIcon(image);
|
|
|
|
activeMarker.setAnimation(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2011-03-22 08:45:28 +00:00
|
|
|
$('#steal_form').live('submit', function() {
|
|
|
|
var answer = window.confirm("Are you sure you want to steal this hydrant?")
|
|
|
|
if(answer) {
|
|
|
|
$.post('<%= hydrant_path :format => "json" %>', {
|
|
|
|
'id': $('#hydrant_id').val(),
|
|
|
|
'commit': $('#steal_form_submit').val(),
|
|
|
|
'utf8': '✓',
|
|
|
|
'authenticity_token': $('#steal_form input[name="authenticity_token"]').val(),
|
|
|
|
'_method': 'put',
|
|
|
|
'hydrant': {
|
|
|
|
'user_id': $('#hydrant_user_id').val(),
|
|
|
|
'name': $('#hydrant_name').val()
|
|
|
|
}
|
|
|
|
}, function(data) {
|
|
|
|
$.get('<%= contents_path %>', {
|
|
|
|
'hydrant_id': activeHydrantId
|
|
|
|
}, function(data) {
|
|
|
|
activeInfoWindow.setContent(data);
|
|
|
|
image = new google.maps.MarkerImage('/images/markers/red.png',
|
|
|
|
new google.maps.Size(27.0, 37.0),
|
|
|
|
new google.maps.Point(0, 0),
|
|
|
|
new google.maps.Point(13.0, 18.0)
|
|
|
|
);
|
|
|
|
activeMarker.setIcon(image);
|
|
|
|
activeMarker.setAnimation(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2011-03-17 20:53:05 +00:00
|
|
|
});
|