/**
 * Prepare and initialize system notifications homescreen widget
 */
jQuery.fn.remindersHomescreenWidget = function(s) {
  var settings = jQuery.extend({

  }, s);

  return this.each(function() {

    var wrapper = $(this);

    wrapper.find('a.reminder_dismiss').asyncLink({
      'type'            : 'post',
      'success_message' : App.lang('Reminder dismissed'),
      'indicator_url'   : App.Wireframe.Utils.indicatorUrl('small'),
      'before'          : function () {
        $(this).addClass('processing');
      },
      'complete'        : function () {
        $(this).removeClass('processing');
      },
      'success'         : function (reminder) {
        App.Wireframe.Events.trigger('reminder_deleted', [reminder]);
      }
    });

    // handle deletion of reminders
    App.Wireframe.Events.bind('reminder_deleted.single', function (event, reminder) {
      var reminder_row = wrapper.find('li[reminder_id="' + reminder.id + '"]');
      if (reminder_row.length) {
        reminder_row.remove();
      } // if

      if (!wrapper.find('li.reminder').length) {
        wrapper.parents('.homescreen_widget:first').remove();
      } //if
    });

    // if we don't have reminders initially, remove the widget from the dashboard
    if (!wrapper.find('li.reminder').length) {
      wrapper.parents('.homescreen_widget:first').remove();
    } //if
  });

};