function rcmail_archive(prop)
{
  if (!rcmail.env.uid && (!rcmail.message_list || !rcmail.message_list.get_selection().length))
    return;

  var uids = rcmail.env.uid ? rcmail.env.uid : rcmail.message_list.get_selection().join(','),
   lock = rcmail.set_busy(true, 'loading');
   rcmail.http_post('plugin.archive', '_uid='+uids+'&_mbox='+urlencode(rcmail.env.mailbox), lock);  
}

function rcmail_archive_contextmenu(prop)
{
  if (!rcmail.env.uid && (!rcmail.message_list || !rcmail.message_list.get_selection().length))
    return;
    
  if(rcmail.message_list.get_selection().length > 0)
    rcmail_archive(prop);
}

// callback for app-onload event
$(document).ready(function() {
  if (window.rcmail) {
    if(rcmail.env.skin != 'larry'){
      $('.archivefolder').text('');
    }
    rcmail.addEventListener('init', function(evt) {
      if(rcmail.env.archive_folder)
        rcmail.add_onload('rcmail_archive_init()');
      // register command (directly enable in message view mode)
      rcmail.register_command('plugin.archive', rcmail_archive, (rcmail.env.uid && rcmail.env.mailbox != rcmail.env.archive_folder));

      // add event-listener to message list
      if (rcmail.message_list)
        rcmail.message_list.addEventListener('select', function(list){
          rcmail.enable_command('plugin.archive', (list.get_selection().length > 0 && rcmail.env.mailbox != rcmail.env.archive_folder));
        });
      rcmail_archive_icon();
    });
  }
});

function rcmail_archive_icon()
{
  // set css style for archive folder
  var li;
  if (rcmail.env.archive_folder && rcmail.env.archive_folder_icon
    && (li = rcmail.get_folder_li(rcmail.env.archive_folder, '', true))
  ){
    if(rcmail.env.skin != 'larry')
      $(li).css('background-image', 'url(' + rcmail.env.archive_folder_icon + ')');
    else
      $(li).addClass('archive');
    $(li).insertAfter('#mailboxlist .inbox');
    li = $('._archive');
    $(li.get(0)).insertBefore('#rcmContextMenu .drafts');
  }
}

function rcmail_archive_init()
{
	if (window.rcm_contextmenu_register_command){
		//command, callback, label, pos, sep, multi, newSub, menu	
    rcm_contextmenu_register_command('archive', 'rcmail_archive_contextmenu', rcmail.gettext('archivefolder.buttontitle'), 'delete', null, true);
  }
}