var tickettypes = {};
$(document).ready(function() {

    //name renderer
    tickettypes.renderName = function(text,row,el){
        var name = row.ticketTypeName;
        if(canedittickettypes){
            name = '<a href="javascript:editTicketType('+row.ticketTypeId+','+row.canDeleteTicketType+','+row.ticketTypeSystemId+');" id="'+row.ticketTypeId+'" class="editbillingtype" title="'+lang('Click to edit this ticket type')+'">'+row.ticketTypeName+'</a>';
        }			
        return name;
    };

    //expander renderer non delayed renderer as we get the data on initial request
    tickettypes.renderExpander = function(text,row,el){
    var expanderBody = "";
    if (row.ticketTypeSystemId>0){
        expanderBody += "<b style='color:red;'>System Type - may not be deleted.</b><br/><br/>";               
    }
    if (row.ticketTypeDescription=="") row.ticketTypeDescription = lang("None");
        expanderBody += "<b>Description</b>: "+ row.ticketTypeDescription+" </b>";
        expanderBody += "<br/><b>Default Assignee</b>: " + row.ticketTypeDefaultAssignee;
        return expanderBody;
    }
    
    
    tickettypes.grid = new RichHTML.grid({
        el:'tickettypes-grid',
        width: "100%",		
        url: 'index.php?fuse=support&action=GetTicketTypeList',
        root: 'groups',
        totalProperty : 'totalcount',			
        baseParams: {
            sessionHash: gHash,
            includesystem:0,
            limit: 150,
            filter: ""
        },
        columns: [{
            text: "",
            //dataIndex: "billpmtref",
            xtype: "expander",
            escapeHTML: true,
            renderer:tickettypes.renderExpander
        },{          
            text:     	"",            
            xtype:      "drag"            
        },{
            id: 'ticketTypeId', 
            dataIndex : "ticketTypeId",
            hidden:true
        },{
            text: lang("Name"),
            align: 'left',
            renderer: tickettypes.renderName,
            dataIndex : "ticketTypeName"
        },{
            text: lang("Enabled"),
            width: 80,
            align: 'center',
            dataIndex : "ticketTypeEnabled"
        },{
            text: lang("Tickets"),
            width: 80,
            align: 'center',
            dataIndex : "numTickets"
        }]
    });

    tickettypes.grid.render();
	
    //lets add handler to checkbox for including system ticket types
    $('#includesystemtypes').click(function(){	
        var includesystem = 0;
        if ($(this).is (':checked')) {			
            includesystem = 1;
        }
        tickettypes.grid.reload({params:{"start":0,"includesystem":includesystem}});		
    });

    $(tickettypes.grid).bind({	
        "drop": function (event, data) {
            $.ajax({
                url: 'index.php?fuse=support&action=UpdateTicketTypesOrder',
                dataType: 'json',
                type: 'POST',
                data: {sessionHash: gHash,ids:tickettypes.grid.getRowValues('ticketTypeId')},
                success: function(data) {}
            });	
        }
    });
});