MyDesktop.Preferences = Ext.extend(Ext.app.Module, {
    
    appType : 'preferences',
    id : 'preferences-win',
    
    // Link/Button actions
    actions : null,
    // Content panel
    cPanel : null,
    // Current component loaded into the content panel
    cComp : null,
    
    init : function(){
        this.launcher = {
            menuText: 'Préférences',
            text: 'Préférences',
            iconCls: 'bogus',
            handler : this.createWindow,
            scope: this
        }
    },

    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('desktop-preferences');
        
        if(!win){
            var winWidth = 650;
            var winHeight = 400;
            
            var west = new Ext.Panel({
                border: false,
                html: '<ul id="pref-nav-panel"> \
                        <li> \
                            <img src="css/images/s.gif" class="icon-pref-quickstart"/> \
                            <a id="viewQuickstart" href="#">Quick Start</a> \
                        </li> \
                        <li> \
                            <img src="css/images/s.gif" class="icon-pref-shortcuts"/> \
                            <a id="viewShortcuts" href="#">Shortcuts</a> \
                        </li> \
                    </ul>',
                id: 'desktop-preferences-nav',
                region: 'west',
                split: false,
                width: 180
            });
            
            //this.cComp = this.initComp('quickstart');
            
            this.cPanel = new Ext.Panel({
                border: false,
                id: 'desktop-preferences-content',
                //items: this.cComp,
                layout: 'fit',
                region: 'center'
            });
            
            win = desktop.createWindow({
                animCollapse: false,
                constrainHeader: true,
                id: 'desktop-preferences',
                height: winHeight,
                iconCls: 'bogus',
                items: [
                    west,
                    this.cPanel
                ],
                layout: 'border',
                shim: false,
                title: 'Preferences',
                width: winWidth
            });
            
            this.initActions();
            //this.cComp.root.expand(true);
            
            // listeners
            var w = west.body;
            w.on('mousedown', this.doAction, this, {delegate:'a'});
            w.on('click', Ext.emptyFn, null, {delegate:'a', preventDefault:true});
            
            win.on('hide', this.onHide, this);
            
            this.viewComp('quickstart');
        }
        
        win.show();
    },
    
    initComp : function(type){
        var ms = this.app.modules;
        var qsIds = this.app.desktopConfig[type];
        var nodes = this.expandNodes(ms, qsIds);
                
        var tree = new Ext.tree.TreePanel({
            autoScroll: true,
            bodyStyle: 'padding:10px',
            border: false,
            buttons: [{
                handler: this.save,
                scope: this,
                text: 'Sauvegarder'
            }],
            id: type,
            lines: false,
            loader: new Ext.tree.TreeLoader(),
            rootVisible: true,
            root: new Ext.tree.AsyncTreeNode({
                text: type == 'quickstart' ? 'Raccourcis' : 'Menu des applications',
                children: nodes
            })
        });
        tree.on('checkchange', this.onCheckChange, this);
        
        return tree;
    },
    
    initActions : function(){
        this.actions = {
            'viewShortcuts' : function(app){
                //app.viewComp('shortcuts');
                alert('Pas encore implémenté!');
            },
            
            'viewQuickstart' : function(app){
                app.viewComp('quickstart');
            }
        }
    },
    
    doAction : function(e, t){
        e.stopEvent();
        this.actions[t.id](this);  // pass this app for scope
    },
    
    viewComp : function(type){
        this.removeCComp();
        
        var ms = this.app.modules;
        var qsIds = this.app.desktopConfig[type];
        var nodes = this.expandNodes(ms, qsIds);
        
        this.cComp = this.initComp(type);
        
        this.cPanel.add(this.cComp);
        this.cPanel.doLayout();
        
        this.cComp.root.expand(true);
    },
    
    inQuickStart : function(id, qsIds){
        for(var i = 0, len = qsIds.length; i < len; i++){
            if(id == qsIds[i]){
                return true;
            }
        }
    },
            
    expandNodes : function(ms, qsIds){
        var nodes = [];
        
        for(var i = 0, len = ms.length; i < len; i++){
            var o = ms[i].launcher ? ms[i].launcher : ms[i];
            if(o.menu){
                /* nodes.push({
                    leaf: false,
                    text: o.text || o.menuText,
                    children: this.expandNodes(o.menu.items, qsIds)
                }); */
            }else{
                nodes.push({
                    checked: this.inQuickStart(ms[i].id, qsIds) ? true : false,
                    iconCls: 'quick-start-plugin',
                    id: ms[i].id,
                    leaf: true,
                    text: o.text || o.menuText
                });
            }
        }
        
        return nodes;
    },
    
    /* getCheckedIds : function(){
        var ids = [], c = 0;
            
        function simplifyNodes(node) {
            var kids = node.childNodes,
                len = kids.length;
                
            for (var i = 0; i < len; i++) {
                if(kids[i].leaf && kids[i].attributes.checked){
                    ids[++c-1] = kids[i].id;
                }
                simplifyNodes(kids[i]);
            }
        }
        
        simplifyNodes(this.cComp.root);
        return ids;
    }, */
    
    onCheckChange : function(node, checked){
        //console.log(node.ownerTree.id);
        var qs = this.app.desktopConfig.quickstart;
        
        if(node.leaf && node.id){
            if(checked){
                this.app.addQuickStartButton(node.id);
                
                // update desktopConfig
                qs.push(node.id);
            }else{
                this.app.removeQuickStartButton(node.id);
                
                // update desktopConfig
                var i = 0;
                while(i < qs.length){
                    if(qs[i] == node.id){
                        qs.splice(i, 1);
                    }else{
                        i++;
                    }
                }
            }
        }
    },
    
    onHide : function(){
        this.removeCComp();
    },
    
    removeCComp : function(){
        if(this.cComp){
            this.cPanel.remove(this.cComp);
            this.cComp = null;
        }
    },
    
    save : function(){
        alert(Ext.encode(this.app.desktopConfig));
    }
});
