/**
 * Mailing activity log behavior
 */
jQuery.fn.mailingActivityLog = function(s) {
  var settings = jQuery.extend({
    'load_more_url' : null,  
    'entries' : null, 
    'entries_per_load' : 30, 
    'total_entries' : 0
  }, s);
  
  return this.pagedObjectsList({
    'load_more_url' : settings['load_more_url'], 
    'items' : settings['entries'],
    'items_per_load' : settings['entries_per_load'], 
    'total_items' : settings['total_entries'],
    'list_items_are' : 'tr',
    'list_item_attributes' : {
      'class' : 'mailing_activity_log'
    },
    'columns' : {
      'icon' : '',
      'info' : App.lang('Message'),
      'from' : App.lang('From'),   
      'to' : App.lang('To'),
      'created_on' : App.lang('Logged On'),    
      'options' : App.lang('Options')
    }, 
    'on_add_item' : function(item) {
      var log_entry = $(this);
      
      log_entry.append(
        '<td class="icon"></td>' + 
        '<td class="info"></td>' +  
        '<td class="from"></td>' + 
        '<td class="to"></td>' + 
        '<td class="created_on"></td>' + 
        '<td class="options"></td>'
      );

      $('<img>').attr('src', item['icon_url']).appendTo(log_entry.find('td.icon'));

      log_entry.find('td.info').append(item['name'].clean());

      if(item['from']) {
        $('<a></a>').attr('href', item['from']['urls']['view']).text(item['from']['short_display_name']).appendTo(log_entry.find('td.from'));
      } // if

      if(item['to']) {
        $('<a></a>').attr('href', item['to']['urls']['view']).text(item['to']['short_display_name']).appendTo(log_entry.find('td.to'));
      } // if

      log_entry.find('td.created_on').text(item['created_on']['formatted']);

      if(item['has_details']) {
        $('<a href="' + item['urls']['view'] + '" class="view_details" title="' + App.lang('View Details') + '"><img src="' + App.Wireframe.Utils.imageUrl('/icons/12x12/preview.png', 'environment') + '" /></a>').flyout({
          'title' : App.lang('Details')
        }).appendTo(log_entry.find('td.options'));
      } // if
    }
  });
  
};