tickettypes = {};

Ext.onReady(function(){

    tickettypes.gridstore = new Ext.data.JsonStore({
            id: "gridstore",
            root: 'groups',
            totalProperty: 'totalcount',
            idProperty: 'ticketTypeId',
            remoteSort: true,
            baseParams: {
            filter: ""

        },
        fields: [
                'ticketTypeId','ticketTypeName', 'ticketTypeDescription','ticketTypeEnabled','ticketTypeDefaultAssignee','numTickets','canEditTicketTypes','canDeleteTicketType'
        ],
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
                url: 'index.php?fuse=support&action=GetTicketTypeList'
        })
    });
    tickettypes.gridstore.setDefaultSort('id', 'asc');
    function renderName(value, p, record){
        name = record.data.ticketTypeName;
        if(tickettypes.gridstore.reader.jsonData.canEditTicketTypes){
            name = '<a href="javascript:editTicketType('+record.data.ticketTypeId+','+record.data.canDeleteTicketType+');" id="'+record.data.ticketTypeId+'" class="editbillingtype" qtip="'+lang('Click to edit this ticket type')+'">'+record.data.ticketTypeName+'</a>';
        }

        return name;
    }
    
    tickettypes.grid = new Ext.grid.GridPanel({
        id:'tickettype-grid',
        el:'tickettype-grid',
        autoHeight:true,
        store: tickettypes.gridstore,
        /*trackMouseOver:true,
        disableSelection:true,*/
        loadMask: true,  
        

        // grid columns
        columns:[/*sm,*/{
            id: 'ticketTypeId', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            header: lang("Id"),
			dataIndex : "ticketTypeId",
			hidden:true,
            sortable: false,
			align:"left"
        },{
            id: 'ticketTypeName', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            header: lang("Name"),
            width: 35,
            renderer : renderName,
    		dataIndex : "ticketTypeName",
            sortable: false
        },{
            id: 'ticketTypeDescription', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            header: lang("Description"),
            width: 90,
    		dataIndex : "ticketTypeDescription",
            sortable: false
        },{
            id: 'ticketTypeDefaultAssignee', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            header: lang("Default Assignee"),
            width: 35,
    		dataIndex : "ticketTypeDefaultAssignee",
            sortable: false
        },{
            id: 'ticketTypeEnabled', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            header: lang("Enabled"),
            width: 35,
            dataIndex : "ticketTypeEnabled",
            sortable: false
        },{
            id: 'numTickets', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            header: lang("Tickets"),
            width: 40,
            dataIndex : "numTickets",
            sortable: false
        }
        ],
        enableDragDrop    : true,
        ddGroup     : 'myGridDD',
        dragConfig : {
            containerScroll : true

        },
	stripeRows: true,
        viewConfig: {
            forceFit:true,
            enableRowBody:true,
            emptyText: lang("There are no groups available to view at this time"),
	    scrollOffset:1
        }

    });
    // option button code
    function toggleButtons(count){
        if(count == undefined){
            count = sm.getCount();
        }
        if(count>0){
            deletebutton.setDisabled(false);
        }else{
            deletebutton.setDisabled(true);
        }
    }

    function GetSelectedRowIds(){
        var arrayofids = new Array();
        rows = sm.getSelections();
	    for(x=0;x<rows.length;x++){
	       arrayofids[x] = rows[x].data.ticketTypeId;
	    }
	    return arrayofids;
    }

    // render it
    tickettypes.grid.render();
    tickettypes.gridstore.setBaseParam('limit', 50);
    tickettypes.gridstore.load({params:{start:0}});

    tickettypes.gridstore.on('load',function(){
        var noticeOrder = document.getElementById('noticeOrder');
        if(tickettypes.gridstore.reader.jsonData.canEditTicketTypes){
            noticeOrder.style.display = "block";
       }else{
            noticeOrder.style.display = "none";
       }
    });
});