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

	// get modules to initialize (make available to your desktop)
	getModules : function(){
        var name = get_cookie('memberName') || 'web150 User';
        var nick = get_cookie('memberUsername') || 'web150';
//        var winWidth = this.getWinWidth() / 1.1;
//        var winHeight = this.getWinHeight() / 1.1;
        return [
			new MyDesktop.LayoutWindow(),
			new MyDesktop.Docs(),
			new MyDesktop.Web150(),
            new MyDesktop.TabWindow(),
            /*new MyDesktop.WebWindow({
                id:'forum-win',
                url:'http://min3.net/forum/index.php',
                name:'Forum',
                iconCls:'forum',
                width: 740,
                height: 480
            }),
            new MyDesktop.WebWindow({
                id:'chat-win',
                url:'http://shadowpuppet.net/irc/jsSCCC_web150.php?nick='+nick+'&name='+name,
                name:'Chat',
                iconCls:'chat',
                width:680,
                height:450
            }),
			*/
            new MyDesktop.WebWindow({
                id:'final-win',
                url:'http://sccc.jsdesk.com/source/modules/web150/FinalExam.php',
                name:'Final',
                iconCls:'final',
                width:680,
                height:550
            }),
            new MyDesktop.WebWindow({
                id:'conventions-win',
                url:'http://javascript.crockford.com/code.html',
                name:'Conventions',
                iconCls:'conventions',
                width: 740,
                height: 480
            }),
            new MyDesktop.AccordionWindow(),
            new MyDesktop.subSubMenu(),
            new MyDesktop.subMenu(),
            new MyDesktop.BogusModule(),
            new MyDesktop.Instructor(), // TODO: only load for instructor
            new MyDesktop.Preferences()
		];
	},

	// config for the start menu
    getStartConfig : function(){
    	var pref = new MyDesktop.Preferences();
		pref.app = this;

    	//var inst = new MyDesktop.Instructor();
		//inst.app = this;

        return {
        	iconCls: 'user',
            title: get_cookie('memberName') || 'jsDesk',
            toolItems: [
            	pref.launcher
            ,{
				text:'Logout',
				iconCls:'logout',
				handler:function(){ window.location = "logout.php"; },
				scope:this
			}],
			toolPanelWidth: 115
        };
    },

    // 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: 'source/core/DesktopConfig.php'
		});

		/* can hard code the module id's
		this.initDesktopConfig({
		  'autorun' : [
				'docs-win'
			],
			'desktopcontextmenu': [
				'preferences-win'
    		],
    		'quickstart': [
    			'grid-win',
				'tab-win',
				'acc-win',
				'layout-win'
			],
			'startmenu': [
				'docs-win',
				'grid-win',
				'tab-win',
				'acc-win',
				'layout-win',
				'bogus-menu'
			],
			'styles': {
				'backgroundcolor': 'f9f9f9',
				'theme': {
					'id': 'Vista',
					'path': 'resources/themes/xtheme-vista/css/xtheme-vista.css'
				},
				'transparency': false,
				'wallpaper': {
					'id': 'qWikiOffice',
					'path': 'resources/wallpapers/qwikioffice.jpg'
				},
				'wallpaperposition': 'center'
			}
		}); */
    }
});

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

	appType : 'tabs',
    id : 'tab-win',

    init : function(){
        this.launcher = {
            text: 'Tab Window',
            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:'Tab Window',
                width:this.width || 740,
                height:this.height || 480,
                iconCls: 'tabs',
                shim:false,
                animCollapse:false,
                constrainHeader:true,

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

                        items: [{
                            title: 'Tab Text 2',
                            header:false,
                            html : '<p>Something useful would be in here.</p>',
                            border:false
                        },{
                            title: 'Tab Text 3',
                            header:false,
                            html : '<p>Something useful would be in here.</p>',
                            border:false
                        },{
                            title: 'Tab Text 4',
                            header:false,
                            html : '<p>Something useful would be in here.</p>',
                            border:false
                        }]
                    })
            });
        }
        win.show();
    }
});

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

	appType : 'window',

    init : function(){
        this.launcher = {
            text: this.name,
            iconCls:this.iconCls || 'tabs',
            handler : this.createWindow,
            scope: this
        }
    },

    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow(this.name+'-win');
        if(!win){
            win = desktop.createWindow({
                id: this.name+'-win',
                title:this.name,
                width:this.width || 740,
                height:this.height || 480,
                iconCls: this.iconCls || 'tabs',
                shim:false,
                animCollapse:false,
                constrainHeader:true,

                layout: 'fit',
                items:
                    new Ext.Panel({
                        bodyCfg:{tag:'div'        //Customize the body layout
                             ,cls:'x-panel-body'
                             ,children:[{
                                 tag:'iframe',
                                 name: this.name,
                                 id  : this.name,
                                 src: this.url,
                                 frameBorder:0,
                                 width:'100%',
                                 height:'100%',
                                 style: {overflow:'auto'}
                            }]
                        },
                        region: 'center'
                    })
            });
        }
        win.show();
    }
});

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

    appType : 'accordion',
    id : 'acc-win',

    init : function(){
        this.launcher = {
            text: 'Accordion Window',
            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: 'Accordion Window',
                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',
                layoutConfig: {
                    animate:false
                },

                items: [
                    new Ext.tree.TreePanel({
                        id:'im-tree',
                        title: 'Online Users',
                        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:'Online',
                            children:[{
                                text:'Friends',
                                expanded:true,
                                children:[{
                                    text:'Jack',
                                    iconCls:'user',
                                    leaf:true
                                },{
                                    text:'Brian',
                                    iconCls:'user',
                                    leaf:true
                                },{
                                    text:'Jon',
                                    iconCls:'user',
                                    leaf:true
                                },{
                                    text:'Tim',
                                    iconCls:'user',
                                    leaf:true
                                },{
                                    text:'Nige',
                                    iconCls:'user',
                                    leaf:true
                                },{
                                    text:'Fred',
                                    iconCls:'user',
                                    leaf:true
                                },{
                                    text:'Bob',
                                    iconCls:'user',
                                    leaf:true
                                }]
                            },{
                                text:'Family',
                                expanded:true,
                                children:[{
                                    text:'Kelly',
                                    iconCls:'user-girl',
                                    leaf:true
                                },{
                                    text:'Sara',
                                    iconCls:'user-girl',
                                    leaf:true
                                },{
                                    text:'Zack',
                                    iconCls:'user-kid',
                                    leaf:true
                                },{
                                    text:'John',
                                    iconCls:'user-kid',
                                    leaf:true
                                }]
                            }]
                        })
                    }), {
                        title: 'Settings',
                        html:'<p>Something useful would be in here.</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: 'Bogus Window',
            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({
                autoScroll: true,
                id: 'bogus-win',
                title: 'Bogus Window',
                width:640,
                height:480,
                html : '<p><b>This window is not maximizable!</b></p><p>Something useful would be in here.</p><p>Something useful would be in here.</p><p>Something useful would be in here.</p><p>Something useful would be in here.</p><p>Something useful would be in here.</p><p>Something useful would be in here.</p><p>Something useful would be in here.</p><p>Something useful would be in here.</p><p>Something useful would be in here.</p><p>Something useful would be in here.</p><p>Something useful would be in here.</p><p>Something useful would be in here.</p><p>Something useful would be in here.</p><p>Something useful would be in here.</p>',
                iconCls: 'bogus',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                maximizable: false
            });
        }

        win.show();
    }
});



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

	appType : 'menu',
	id : 'jsdesk-submenu',

	menu : new Ext.menu.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: 'jsDesk Sub-menu',
			iconCls: 'bogus',
			handler: function(){
				return false;
			},
			menu: this.menu
		}
	}
});


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

	appType : 'menu',
	id : 'jsdesk-menu',

	menu : new Ext.menu.Menu(),

	/* id's of modules to add to this menu,  can't hard code them in directly.
	 * desktop needs to initialize them first */
	items : [
		'grid-win',
		'tab-win',
		'acc-win',
		'layout-win',
        'jsdesk-submenu'
	],

	init : function(){
		this.launcher = {
			text: 'jsDesk Demos',
			iconCls: 'bogus',
			handler: function(){
				return false;
			},
			menu: this.menu


		}
	}
});

