mailer = {};
mailer.Invoked = false;

mailer.ViewQueuedEmails = function(){
    // Prevent this from being called twice
    if(document.getElementById('pendingitems') == null || pendingitems.Invoked){
        return;
    }
    
    mailer.gridstore = new Ext.data.JsonStore({
        id:             "mailer",
        root:           'data',
        totalProperty:  'totalcount',
        idProperty:     'ticketid',
        remoteSort:     true,
        fields:         [
            'customerid',
			'subject',
            'to',
			'bcc',
			'body',
			'attachment'
        ],
        proxy:          new Ext.data.HttpProxy({
            url:  'index.php?fuse=admin&action=getQueuedEmailList'
        })
    });
    
    mailer.renderName = function(value, p, record){
        return String.format('<a href="index.php?fuse=clients&amp;view=Admin_Profile_Contact&amp;frmClientID={0}">{1}</a>', record.data.customerid, record.data.customer);
    }
	
	mailer.grid = new Ext.grid.GridPanel({
        el:               'pendingitems',
        autoHeight:       true,
        store:            mailer.gridstore,
        trackMouseOver:   true,
        disableSelection: true,
        loadMask:         true,
        columns:          [
			{
                id:         'subject',
                header:     "Subject",
                width:      75,
                dataIndex:  "subject"
            },
            {
                id:         'to',
                header:     "To",
                width:      30,
                dataIndex:  "to"
            },
			{
                id:         'bcc',
                header:     "BCC",
                width:      30,
                dataIndex:  "bcc"
            },		
			{
			    id:         'body',
                header:     "Body",
                width:      50,
                dataIndex:  "body"
			},		
			{
			    id:         'attachment',
                header:     "Attachments",
                width:      20,
                dataIndex:  "attachment"
			}
        ],
        stripeRows:       true,
        viewConfig:       {
            forceFit:       true,
            enableRowBody:  true,
            emptyText:      lang("There are currently no e-mails in the queue."),
            scrollOffset:   1
        }
    });

    mailer.grid.render();
    mailer.gridstore.load({params:{start:0, limit:0}});
    mailer.Invoked = true;
}