Ext.namespace('Modules');
Ext.namespace('QoDesk');
Modules.Preferences = Ext.extend(Ext.app.Module, {
	moduleType : 'Core',
	moduleId : 'Preferences',
	menuPath : 'ToolMenu/Control Panel',
	launcher : {
		iconCls: 'Preferences-icon16x16',
		shortcutIconCls: 'Preferences-icon48x48',
		text: 'Preferences',
		tooltip: '<b>Preferences</b><br />A system application.  Allows users to set, and save their desktop preferences to the database.'
	}
});
Modules.Module = Ext.extend(Ext.app.Module, {
	moduleType : 'Core',
	moduleId : 'Module',
	menuPath : 'ToolMenu',
	launcher : {
		iconCls: 'Module-icon16x16',
		shortcutIconCls: 'Module-icon48x48',
		text: 'Module',
		tooltip: '<b>Module</b><br />Modules management'
	}
});
Modules.Explorer = Ext.extend(Ext.app.Module, {
	moduleType : 'Core',
	moduleId : 'Explorer',
	menuPath : 'StartMenu',
	launcher : {
		iconCls: 'Explorer-icon16x16',
		shortcutIconCls: 'Explorer-icon48x48',
		text: 'Explorer',
		tooltip: '<b>Explorer</b><br />File manager'
	}
});
Modules.GoogleGadgets = Ext.extend(Ext.app.Module, {
	moduleType : 'Other',
	moduleId : 'GoogleGadgets',
	menuPath : 'StartMenu',
	launcher : {
		iconCls: 'GoogleGadgets-icon16x16',
		shortcutIconCls: 'GoogleGadgets-icon48x48',
		text: 'Google Gadgets',
		tooltip: '<b>Google Gadgets</b><br />'
	}
});
Modules.WelcomeCenter = Ext.extend(Ext.app.Module, {
	moduleType : 'Core',
	moduleId : 'WelcomeCenter',
	menuPath : 'ToolMenu/Control Panel',
	launcher : {
		iconCls: 'WelcomeCenter-icon16x16',
		shortcutIconCls: 'WelcomeCenter-icon48x48',
		text: 'Welcome Center',
		tooltip: '<b>Welcome Center</b><br />'
	}
});
Modules.Sidebar = Ext.extend(Ext.app.Module, {
	moduleType : 'Core',
	moduleId : 'Sidebar',
	menuPath : '',
	launcher : {
		iconCls: 'Sidebar-icon16x16',
		shortcutIconCls: 'Sidebar-icon48x48',
		text: 'Sidebar',
		tooltip: '<b>Sidebar</b><br />Sidebar'
	}
});

/*
 * qWikiOffice Desktop 0.8.1
 * Copyright(c) 2007-2008, Integrated Technologies, Inc.
 * licensing@qwikioffice.com
 * 
 * http://www.qwikioffice.com/license
 *
 * NOTE:
 * This code is based on code from the original Ext JS desktop demo.
 * I have made many modifications/additions.
 *
 * The Ext JS licensing can be viewed here:
 *
 * Ext JS Library 2.0 Beta 2
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 *
 */

QoDesk.App = new Ext.app.App({
	
	init : function(){
		Ext.QuickTips.init();
	},
	
	/**
     * Returns the privileges object.
     *
     * Example
     *
     * {
     *		saveAppearence: [
     *			'qo-preferences'
     *		],
     *		saveAutorun: [
     *			'qo-preferences'
     *		]
     *		...
     * }
     */
	getPrivileges : function(){
	    return {"loadModule":["Preferences"],"saveAppearance":["Preferences"],"saveAutorun":["Preferences"],"saveBackground":["Preferences"],"saveQuickstart":["Preferences"],"saveShortcut":["Preferences"],"viewThemes":["Preferences"],"viewWallpapers":["Preferences"]};
	},
	
	/**
	 * Returns an array of the module instances.
	 *
	 * Example:
	 * 
	 * [
	 * 		new QoDesk.GridWindow(),
	 *		...
	 * ]
	 * 
	 */
    getModules : function(){
    	return [ new Modules.Preferences(),new Modules.Module(),new Modules.Explorer(),new Modules.GoogleGadgets(),new Modules.WelcomeCenter(),new Modules.Sidebar() ];
    },
    
    /**
	 * Returns the launchers object.
	 * Contains the moduleId's of the modules to add to each launcher.
	 * 
	 * Example:
	 *
	 * {
	 *		autorun: [
	 *		
	 *		],
	 *		contextmenu: [
	 *			'qo-preferences'
	 *		],
	 *		quickstart: [
	 *			'qo-preferences'
	 *		],
	 *		shortcut: [
	 *			'qo-preferences'
	 *		]
	 *	}
	 */
    getLaunchers : function(){
    	return {"autorun":["WelcomeCenter","Sidebar"],"contextmenu":["Preferences"],"quickstart":["Preferences"],"shortcut":["Module","Explorer","GoogleGadgets","Preferences"]};
    },
	
    /**
	 * Returns the Styles object.
	 *
	 * Example
	 *
	 * {
	 *		backgroundcolor: '575757',
	 *		fontcolor: 'FFFFFF',
	 *		transparency: 100,
	 *		theme: {
	 *			id: 2,
	 *			name: 'Vista Black',
	 *			pathtofile: 'resources/themes/xtheme-vistablack/css/xtheme-vistablack.css'
	 *		},
	 *		wallpaper: {
	 *			id: 10,
	 *			name: 'Blue Swirl',
	 *			pathtofile: 'resources/wallpapers/blue-swirl.jpg'
	 *		},
	 *		wallpaperposition: 'tile'
	 *	}
	 */
	getStyles : function(){
		return {"backgroundcolor":"000000","fontcolor":"FFFFFF","transparency":"90","theme":{"id":"2","name":"Vista Black","pathtofile":"resources\/themes\/xtheme-vistablack\/css\/xtheme-vistablack.css"},"wallpaper":{"id":"1","name":"Vista","pathtofile":"resources\/wallpapers\/vista.jpg"},"wallpaperposition":"center"};
	},
	
	/**
	 * Returns the Start Menu's logout button configuration
	 */
	getLogoutConfig : function(){
		return {
					text: 'Login',
			iconCls: 'login',
			handler: function(){
				var form = new Ext.FormPanel({
						id: 'login-firm',
						labelWidth: 80,
						defaultType: 'textfield',
						bodyStyle:'padding:10px',
						baseParams: {module: 'login'},

       	        items: [{
       	            fieldLabel: 'Login',
       	            name: 'user',
			allowBlank:false,
//       	            emptyText:'Enter login',
       	            anchor:'100%'
       	        },{
       	            fieldLabel: 'Password',
       	            name: 'pass',
//       	            emptyText:'Enter password',
       	            anchor:'100%'
       	        },{
       	            fieldLabel: 'Group',
       	            name: 'group',
		hideLabel: true,
		hidden: true,
       	            anchor:'100%'
       	        }],
					buttons: [{
						text: 'Login',
        					handler: function() {
                if(form.getForm().isValid()){
                    form.getEl().mask();
                    form.getForm().submit({
			url:'/system/login/login.php',
                        success: function(form, action){
//                            form.getEl().unmask();
				if ('undefined' != typeof(action.result.sessionId) && "" != action.result.sessionId) {
								// get the path
								var path = window.location.pathname,
								path = path.substring(0, path.lastIndexOf('/') + 1);
									
								// set the cookie
								set_cookie('sessionId', action.result.sessionId, '', path, '', '' );
								
								// redirect the window
								window.location = path;
				} else {
					form.setValues({group: action.result.groups[0].id});
form.submit({
			url:'/system/login/login.php',
                        success: function(form, action){
								// get the path
								var path = window.location.pathname,
								path = path.substring(0, path.lastIndexOf('/') + 1);
									
								// set the cookie
								set_cookie('sessionId', action.result.sessionId, '', path, '', '' );
								
								// redirect the window
								window.location = path;
                        }, 
                        failure: function(){
alert('asdasd');
                            form.getEl().unmask();
                        }
                    });
				}
                        }, 
                        failure: function(){
                            form.getEl().unmask();
                        }
                    });
                }
//	       	    					win.close();
			            		}
					},{
						text: 'Cancel',
		        			scope: this,
        					handler: function() {
		    					win.close();
			            		}
					}]
       	    });
			    	var win = new Ext.Window({
					manager: this.getDesktop().getManager(),
//		        		id: this.moduleId + '-win',
		        		title: 'Login',
					modal: true,
					resizable: false,
					draggable: false,
					width:360,
					height:200,
					closeAction: 'destroy',
					iconCls: 'login',
					layout: 'fit',
					autoScroll: true,
					items: form
				});

				win.show();
//				window.location = "login.html";
			},
					scope: this
		};
	},
	
	/**
	 * Returns the Start Menu configuration
	 */
	getStartConfig : function(){
		return {
			iconCls: 'user',
			title: 'Гость',
			height: 350,
			width: 350,
			toolPanelWidth: 160
		};
	},
    
	/**
	 * Function that handles sorting of the Start Menu
	 * Return true to swap a and b
	 */
	startMenuSortFn : function(a, b){
	    // Sort in ASC alphabetical order
		// if( b.text < a.text ){
		//		return true;
		// }
		
		// Sort in ASC alphabetical order with menus at the bottom
		// if( (b.text < a.text) && !b.menu ){
		//		return true;
		// }
		
		// Sort in ASC alphabetical order with menus at the top
		if( ( ( b.menu && a.menu ) && ( b.text < a.text ) ) || ( b.menu && !a.menu ) || ( (b.text < a.text) && !a.menu ) ){
			return true;
		}
		
		return false;
	}
});
