/*
 * Desktop configuration
 */
MyDesktop = new Ext.app.App({
    init :function(){
        Ext.QuickTips.init();
    },

    // get modules to initialize (make available to your desktop)
    getModules : function(){
        return [
            new Plugin.LayoutWindow(),
            new MyDesktop.GridWindow(),
            new MyDesktop.TabWindow(),
            new MyDesktop.AccordionWindow(),
            new MyDesktop.subMenu(),
            new MyDesktop.BogusModule(),
            new MyDesktop.Preferences()
        ];
    },
    
    // config for the start menu
    getStartConfig : function(){
        var pref = new MyDesktop.Preferences();
        pref.app = this;
        
        return {
            iconCls: 'user',
            title: 'R&eacute;mi SOUBEYRAND', //get_cookie('memberName'),
            toolItems: [
                pref.launcher
            ,{
                text:'Logout',
                iconCls:'logout',
                //handler:function(){ window.location = "logout.php"; },
                scope:this
            }],
            toolPanelWidth: 110
        };
    },
    
    // get user preferences, modules to load into Start Menu and Quick Start
    getDesktopConfig : function(){
        /* can call server for saved module id's
        Ext.Ajax.request({
            success: function(o){
                var decoded = Ext.decode(o.responseText);
                
                if(decoded.success){
                    this.initDesktopConfig(decoded.config);
                }else{
                    // error
                }
            },
            failure: function(){
                // error
            },
            scope: this,
            url: 'php/DesktopConfig.php'
        }); */
        
        // can hard code the module id's
        var o = {
            'desktopcontextmenu': [
                'preferences-win'
            ],
            'quickstart': [
                'grid-win',
                'tab-win',
                'acc-win',
                'layout-win'
            ],
            'startmenu': [
                'grid-win',
                'tab-win',
                'acc-win',
                'layout-win',
                'bogus-menu'
            ]
        }
        
        this.initDesktopConfig(o);
    }
});



/*
 * Example windows
 */
MyDesktop.GridWindow = Ext.extend(Ext.app.Module, {
    
    appType : 'grid',
    id : 'grid-win',
    
    init : function(){
        this.launcher = {
            text: 'Liste des sites Internet',
            iconCls:'icon-grid',
            handler : this.createWindow,
            scope: this
        }
    },

    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('grid-win');
        
        if(!win){
            var tabWinLauncher = MyDesktop.getModule('tab-win').launcher;
            
            win = desktop.createWindow({
                id: 'grid-win',
                title:'Liste des sites Internet',
                width:740,
                height:480,
                iconCls: 'icon-grid',
                shim:false,
                animCollapse:false,
                constrainHeader:true,

                layout: 'fit',
                items: new Ext.grid.GridPanel({
                    border:false,
                    ds: new Ext.data.Store({
                        reader: new Ext.data.ArrayReader({}, [
                            {name: 'internet'},
                            {name: 'type'}
                        ]),
                        data: Ext.grid.dummyData
                    }),
                    cm: new Ext.grid.ColumnModel([
                        new Ext.grid.RowNumberer(),
                        {header: "Site internet", width: 120, sortable: true, dataIndex: 'internet'},
                        //{header: "Price", width: 70, sortable: true, // renderer: Ext.util.Format.usMoney, 
                        // dataIndex: 'price'},
                        //{header: "Change", width: 70, sortable: true, dataIndex: 'change'},
                        {header: "Type de site", width: 70, sortable: true, dataIndex: 'type'}
                    ]),

//ELREMS
					viewConfig: {
						forceFit:true
					},

                    viewConfig: {
                        forceFit:true
                    },
                    //autoExpandColumn:'company',
    
                    tbar: [{
                        text:'Ajouter un site',
                        tooltip:'Ajouter un site Internet',
                        iconCls:'add'
                    }, '-', {
                        text:'Options',
                        tooltip:'TO DEV',
                        iconCls:'option'
                    },'-',{
                        text:'Supprimer un site',
                        tooltip:'Supprimer le site s&eacute;lectionn&eacute;',
                        iconCls:'remove'
                    },'-',{
                        text: 'Qui suis-je...',//ELREMS
                        handler: tabWinLauncher.handler,
                        scope: tabWinLauncher.scope,
                        iconCls: tabWinLauncher.iconCls
                    }]
                })
            });
        }
        win.show();
    }
});



MyDesktop.TabWindow = Ext.extend(Ext.app.Module, {
    
    appType : 'tabs',
    id : 'tab-win',
    
    init : function(){
        this.launcher = {
            text: 'Pr&eacute;sentation',
            iconCls:'tabs',
            handler : this.createWindow,
            scope: this
        }
    },

    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('tab-win');
        if(!win){
            win = desktop.createWindow({
                id: 'tab-win',
                title:'Pr&eacute;sentation : R&eacute;mi SOUBEYRAND',
                width:740,
                height:480,
                iconCls: 'tabs',
                shim:false,
                animCollapse:false,
                border:false,
                constrainHeader:true,

                layout: 'fit',
                items:
                    new Ext.TabPanel({
                        activeTab:0,

                        items: [{
                            title: 'Pr&eacute;sentation',
                            header:false,
                            html : '<p>R&eacute;mi SOUBEYRAND.</p>',
                            autoLoad: {url: 'presentation.html', callback: this.initSearch, scope: this},
                            border:false
                        },{
                            title: 'Objectifs',
                            header:false,
                            html : '<p>Mon parcours.</p>',
                            autoLoad: {url: 'cv.html', callback: this.initSearch, scope: this},
                            border:false
                        },{
                            title: 'Recommandations',
                            header:false,
                            html : '<p>Recommandations.</p>',
                            autoLoad: {url: 'recommandations.html', callback: this.initSearch, scope: this},
                            border:false
                        },{
                            title: 'Contact',
                            header:false,
                            html : '<p>Comment prendre contact.</p>',
                            autoLoad: {url: 'contact.html', callback: this.initSearch, scope: this},
                            border:false
                        }]
                    })
            });
        }
        win.show();
    }
});



MyDesktop.AccordionWindow = Ext.extend(Ext.app.Module, {
    
    appType : 'accordion',
    id : 'acc-win',
    
    init : function(){
        this.launcher = {
            text: 'Prototype de Messager',
            iconCls:'accordion',
            handler : this.createWindow,
            scope: this
        }
    },

    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('acc-win');
        if(!win){
            win = desktop.createWindow({
                id: 'acc-win',
                title: 'Prototype de Messager',
                width:250,
                height:400,
                iconCls: 'accordion',
                shim:false,
                animCollapse:false,
                constrainHeader:true,

                tbar:[{
                    tooltip:{title:'Rich Tooltips', text:'Let your users know what they can do!'},
                    iconCls:'connect'
                },'-',{
                    tooltip:'Add a new user',
                    iconCls:'user-add'
                },' ',{
                    tooltip:'Remove the selected user',
                    iconCls:'user-delete'
                }],

                layout:'accordion',
                border:false,
                layoutConfig: {
                    animate:false
                },

                items: [
                    new Ext.tree.TreePanel({
                        id:'im-tree',
                        title: 'Utilisateurs en ligne',
                        loader: new Ext.tree.TreeLoader(),
                        rootVisible:false,
                        lines:false,
                        autoScroll:true,
                        tools:[{
                            id:'refresh',
                            on:{
                                click: function(){
                                    var tree = Ext.getCmp('im-tree');
                                    tree.body.mask('Loading', 'x-mask-loading');
                                    tree.root.reload();
                                    tree.root.collapse(true, false);
                                    setTimeout(function(){ // mimic a server call
                                        tree.body.unmask();
                                        tree.root.expand(true, true);
                                    }, 1000);
                                }
                            }
                        }],
                        root: new Ext.tree.AsyncTreeNode({
                            text:'Connect&eacute;s',
                            children:[{
                                text:'Amis',
                                expanded:true,
                                children:[{
                                    text:'Eric',
                                    iconCls:'user',
                                    leaf:true
                                },{
                                    text:'Pierre',
                                    iconCls:'user',
                                    leaf:true
                                },{
                                    text:'Sarah',
                                    iconCls:'user',
                                    leaf:true
                                },{
                                    text:'Olivier',
                                    iconCls:'user',
                                    leaf:true
                                },{
                                    text:'Nadege',
                                    iconCls:'user',
                                    leaf:true
                                },{
                                    text:'Mickael',
                                    iconCls:'user',
                                    leaf:true
                                },{
                                    text:'Sandie',
                                    iconCls:'user',
                                    leaf:true
                                }]
                            },{
                                text:'Famille',
                                expanded:true,
                                children:[{
                                    text:'Val&eacute;rie',
                                    iconCls:'user-girl',
                                    leaf:true
                                },{
                                    text:'Monique',
                                    iconCls:'user-girl',
                                    leaf:true
                                },{
                                    text:'Nicolas',
                                    iconCls:'user-kid',
                                    leaf:true
                                },{
                                    text:'Ludovic',
                                    iconCls:'user-kid',
                                    leaf:true
                                }]
                            }]
                        })
                    }), {
                        title: 'Explications',
                        html:'<p>Cette fenetre montre seulement les possiblit&eacute;es de ExtJS.</p>',
                        autoScroll:true
                    }/*,{
                        title: 'Even More Stuff',
                        html : '<p>Something useful would be in here.</p>'
                    },{
                        title: 'My Stuff',
                        html : '<p>Something useful would be in here.</p>'
                    }*/
                ]
            });
        }
        win.show();
    }
});



MyDesktop.BogusModule = Ext.extend(Ext.app.Module, {

    appType : 'bogus',
    id : 'bogus-win',
    
    init : function(){
        this.launcher = {
            text: 'Pr&eacute;sentation d\'Extjs',
            iconCls: 'bogus',
            handler: this.createWindow,
            scope: this
        }
    },

    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('bogus-win');
        
        if(!win){
            win = desktop.createWindow({
                id: 'bogus-win',
                title: 'Pr&eacute;sentation d\'extjs et de ses possibilit&eacute;s',
                width:640,
                height:480,
                html : '<p>Id&eacute;es.</p>',
                autoLoad: {url: 'extjs.html', callback: this.initSearch, scope: this},
                iconCls: 'bogus',
                shim:false,
                animCollapse:false,
                constrainHeader:true
            });
        }
        
        win.show();
    }
});



MyDesktop.subMenu = Ext.extend(Ext.app.Module, {

    appType : 'menu',
    id : 'bogus-menu',

    // id's of modules to add to this menu,  can't hard code them in directly.
    // desktop needs to initialize them first
    items : [
        'bogus-win'
    ],

    init : function(){
        this.launcher = {
            text: 'EXTJS',
            iconCls: 'bogus',
            handler: function(){
                return false;
            },
            menu: {
                items: []
            }
        }
    }
});



// Array data for the grid
Ext.grid.dummyData = [
    ['<a href="http://extjs.com/" target="blank">extjs</a>','ajax'],

    ['<a href="http://www.store-factory.com/" target="blank">Store Factory</a>','Store Factory'],
    ['<a href="http://www.store-factory.com/mag/fr/page-137094.htm" target="blank">R&eacute;f&eacute;rences des 300 sites store factory</a>','Store Factory'],
    ['<a href="http://www.alacarte-traiteur.com/" target="blank">alacarte-traiteur.com</a>','Store Factory'],

    ['<a href="http://www.pertimm.com/" target="blank">Pertimm.com</a>','Pertimm'],
    ['<a href="http://www.biotech-intelligence.com/" target="blank">Biotech-intelligence.com</a>','Pertimm'],
    ['<a href="http://www.cnrs.fr/rechercher/" target="blank">CNRS</a>','Pertimm'],

    ['<a href="http://www.mediatheque-lecreusot.fr/" target="blank">mediatheque-lecreusot.fr</a>','Decalog portail drupal'],
    ['<a href="http://www.mediatheques-france.ma/" target="blank">mediatheques-france.ma</a>','Decalog portail drupal'],

    ['<a href="http://ziki.soubeyrand.info/" target="blank">ziki</a>','Social web'],
    ['<a href="http://viadeo.soubeyrand.info/" target="blank">viadeo</a>','Social web'],

    ['<a href="http://gliffy.soubeyrand.info/" target="blank">gliffy</a>','Graphique des exp&eacute;riences'],

    ['<a href="http://www.google.Fr" target="blank">google</a>','moteur de recherche']

];
