window.addEvent('domready', function() {
  if ($('notify')) {
    $('notify').addEvent('change', updateNotification);
    if (window.location.search &&
        window.location.search.match(/notify=([01])/)) {
      var notify = window.location.search.match(/notify=([01])/)[1];
      if (notify == '0') {
        $('notify').checked = false;
      } else {
        $('notify').checked = true;
      }
      updateNotification();
    }
  }
});

function updateNotification() {
  new XHR({
    onSuccess: function() {
      $('notify-response').setHTML('Your notification preference has been saved.');
      flash('notify-response');
    }
  }).send('/notifications', 'notify=' + ($('notify').checked ? 1 : 0));
}
