/**
 * ImportMessages plugin script
 */

var importmessages_key_press;
var importmessages_key_down;

rcube_webmail.prototype.importmessages_send_upload = function() {
	rcube_find_object('importmessages_form').submit();
	rcmail.message_list.clear();
	rcmail.set_busy(true, 'loading');
	rcmail.importmessages_hide_upload();
};

rcube_webmail.prototype.importmessages_hide_upload = function() {
	$('#importmessageoverlay').dialog('close');
	rcmail.message_list.key_press = importmessages_key_press;
	rcmail.message_list.key_down = importmessages_key_down;
};

$(document).ready(function() {
	if (window.rcmail) {
		rcmail.addEventListener('init', function(evt) {
			// register command (directly enable in message view mode)
			rcmail.register_command('plugin.importmessages.showupload', function() {
				importmessages_key_press = rcmail.message_list.key_press;
				importmessages_key_down = rcmail.message_list.key_down;
				rcmail.message_list.key_press = function() {};
				rcmail.message_list.key_down = function() {};
				rcube_find_object('importmessages_mbox').value = rcmail.env.mailbox;

				$('#importmessageoverlay').dialog({ width: 512, resizable: false, modal: true });
			}, true);

			if (rcmail.env.importmessages_msg)
				rcmail.display_message(rcmail.env.importmessages_msg, rcmail.env.importmessages_type);
		});
	}
});