/*
 * Ext JS Library 1.1
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 *
 * http://www.extjs.com/license
 */
Ext.BLANK_IMAGE_URL = blank_image;

var ds;
var paging;

Ext.onReady(function()
{

    if (is_tournament == true) 
    {
        // create the Data Store
        ds = new Ext.data.Store({
            // load using HTTP
            url: xml_location,
            
            // the return will be XML, so lets set up a reader
            reader: new Ext.data.XmlReader({
                // records will have an "Item" tag
                record: 'tournament',
                id: 'tournamentid',
                totalRecords: 'total'
            }, [            // set up the fields mapping into the xml doc
            // The first needs mapping, the others are very basic
            'tournamentname', 'divisions', 'players', 'stake', 'jackpot', 'enddate', 'playbutton'])
        });
        
        // default sort order
        //ds.setDefaultSort('tournamentname', 'asc');
        
        var cm = new Ext.grid.ColumnModel([{
            header: "Categorie",
            width: 150,
            dataIndex: 'tournamentname',
            resizable: false
        }, {
            header: division_name_multiple,
            width: 170,
            dataIndex: 'divisions',
            resizable: false
        }, {
            header: "Aantal deelnames",
            width: 160,
            dataIndex: 'players',
            resizable: false
        }, {
            header: "Inzet",
            width: 120,
            dataIndex: 'stake',
            resizable: false
        }, {
            header: "Jackpot",
            width: 120,
            dataIndex: 'jackpot',
            resizable: false
        }, {
            header: "Einddatum",
            width: 130,
            dataIndex: 'enddate',
            resizable: false
        }, {
            header: "&nbsp;",
            width: 100,
            dataIndex: 'playbutton',
            resizable: false,
            sortable: false
        }]);
        cm.defaultSortable = true;
    }
    else 
        if (is_quizchamps == true) 
        {
            // create the Data Store
            ds = new Ext.data.Store({
                // load using HTTP
                url: xml_location,
                
                // the return will be XML, so lets set up a reader
                reader: new Ext.data.XmlReader({
                    // records will have an "Item" tag
                    record: 'game',
                    id: 'gameid',
                    totalRecords: 'total'
                }, [            // set up the fields mapping into the xml doc
                // The first needs mapping, the others are very basic
                'gamename', 'divisions', 'players', 'waitingroom', 'stake', 'playbutton'])
            });
            
            // default sort order
            //ds.setDefaultSort('players', 'desc');
            //950px
            var cm = new Ext.grid.ColumnModel([{
                header: "Quizkamer",
                width: 186,
                dataIndex: 'gamename',
                resizable: false
            }, {
                header: division_name_multiple,
                width: 156,
                dataIndex: 'divisions',
                resizable: false
            }, {
                header: "Spelers (min.)",
                width: 122,
                dataIndex: 'players',
                resizable: false
            }, {
                header: "&nbsp;In de wachtkamer",
                width: 266,
                dataIndex: 'waitingroom',
                resizable: false
            }, {
                header: "&nbsp;Inzet",
                width: 120,
                dataIndex: 'stake',
                resizable: false
            }, {
                header: "&nbsp;",
                width: 100,
                dataIndex: 'playbutton',
                resizable: false,
                sortable: false
            }]);
            cm.defaultSortable = true;
        }
        else 
        {
            // create the Data Store
            ds = new Ext.data.Store({
                // load using HTTP
                url: xml_location,
                
                // the return will be XML, so lets set up a reader
                reader: new Ext.data.XmlReader({
                    // records will have an "Item" tag
                    record: 'game',
                    id: 'gameid',
                    totalRecords: 'total'
                }, [            // set up the fields mapping into the xml doc
                // The first needs mapping, the others are very basic
                'gamename', 'divisions', 'players', 'waitingroom', 'stake', 'playbutton'])
            });
            
            // default sort order
            //ds.setDefaultSort('players', 'desc');
            //950px
            var cm = null
            
            if (enable_divisions) 
            {
                cm = new Ext.grid.ColumnModel([{
                    header: "Stadion",
                    width: 216,
                    dataIndex: 'gamename',
                    resizable: false
                }, {
                    header: division_name_multiple,
                    width: 146,
                    dataIndex: 'divisions',
                    resizable: false
                }, {
                    header: "Spelers (min.)",
                    width: 112,
                    dataIndex: 'players',
                    resizable: false
                }, {
                    header: "In de kleedkamer",
                    width: 276,
                    dataIndex: 'waitingroom',
                    resizable: false
                }, {
                    header: "Inzet",
                    width: 100,
                    dataIndex: 'stake',
                    resizable: false
                }, {
                    header: "&nbsp;",
                    width: 100,
                    dataIndex: 'playbutton',
                    resizable: false,
                    sortable: false
                }]);
            }
            else 
            {
                cm = new Ext.grid.ColumnModel([{
                    header: "Stadion",
                    width: 216,
                    dataIndex: 'gamename',
                    resizable: false
                }, {
                    header: "Spelers (min.)",
                    width: 50,
                    dataIndex: 'players',
                    resizable: false
                }, {
                    header: "In de kleedkamer",
                    width: 276,
                    dataIndex: 'waitingroom',
                    resizable: false
                }, {
                    header: "Inzet",
                    width: 100,
                    dataIndex: 'stake',
                    resizable: false
                }, {
                    header: "&nbsp;",
                    width: 100,
                    dataIndex: 'playbutton',
                    resizable: false,
                    sortable: false
                }]);
            }
            cm.defaultSortable = true;
        }
    
    if (typeof(skip_paging) != 'undefined') 
    {
        paging = false;
    }
    else 
    {
        paging = new Ext.PagingToolbar({
            pageSize: game_count,
            store: ds,
            displayInfo: true,
            beforePageText: 'Pagina',
            afterPageText: "van {0}",
            displayMsg: 'Game {0} - {1} van {2}',
            emptyMsg: "Geen games"
        })
    }
    
    // create the grid
    var grid = new Ext.grid.GridPanel({
        ds: ds,
        cm: cm,
        renderTo: 'plac-lobby',
        enableColumnHide: false,
        enableColumnMove: false,
        autoExpandColumn: 1,
        autoHeight: true,
        width: 936,
        bbar: paging
    });
    
    if (is_asr) 
    {
        grid.setWidth(805);
    }
    
    grid.render();
    
	ds.on('load', function(){grid.render(); grid.doLayout();});
    function do_it()
    {
        if (typeof(skip_paging) != 'undefined') 
        {
            ds.reload({
                args: [{
                    params: false,
                    add: false
                }]
            });
        }
        else 
        {
            ds.reload({
                args: [{
                    params: {
                        start: paging.getPageData().activePage,
                        limit: game_count
                    },
                    add: false
                }]
            });
        }
    }
    
    
    var refresher = {
        run: do_it,
        scope: ds,
        interval: game_refresh * 1000
    };
    Ext.TaskMgr.start(refresher);
    
    
});

