//Uwd.System.Startmenu

Uwd.System.Startmenu = function(config){
	Uwd.System.Startmenu.superclass.constructor.call(this, config);
};

Ext.extend(Uwd.System.Startmenu, Ext.util.Observable, {
	 //Ext.ux.StartMenu
	 
	 initStartmenu : function() {
		var system = window.system;
		this.desktop = system.getDesktop();
		
		var userName = system.user.getName();
		// Namen aller erstellten Programm Menus
		var programGroups = [];
		// Die erstellten Programm Menu Objekte in einem Array
		var programMenus = [];
		// Das komplette Programm Menu dem das Array mit Programm Menus angehängt wird.
		var programMenu = new Ext.menu.Menu();
		// Haupt Startmenu
		var mainMenu = new  Uwd.System.SystemMenu({
			height: 370,
			width: 400,
			toolWidth: 150,
			border: 0,
			shadow: true,
			shim: false,
			title: userName,
			iconCls: 'icon-startmenu'

		});
		// Login und Restart Buttons als Tools auf die Rechte Seite
		
		// Programmmenus bilden
		Ext.each(Uwd.System.ProgramStack.getStack(), function(module){
			if (module.getIsTool() === true) {
				
				mainMenu.addTool({
					id: 'start-' + module.getId() + '-' + module.getName(),
					iconCls: module.getIconClass(), // icon and text class
					text: module.getName(),
					handler: function() {
						module.openWindow();
					}.createDelegate(this),
					scope: this
				});
			}
		}.createDelegate(this));
		
		
		mainMenu.addTool({
			id: 'start_restart',
			iconCls: 'icon_cm_restart', // icon and text class
			text: Uwd.System.SystemLanguage.reboot,
			handler: function() {
				var restartApp = new Uwd.System.Restart();
			},
			scope: this
		});
		
		mainMenu.addTool({
			id: 'start_quit',
			iconCls: 'icon_cm_logout', // icon and text class
			text: Uwd.System.SystemLanguage.logout,
			handler: function() {
				var logoutApp = new Uwd.System.Logout();
			},
			scope: this
		});


		// Programmmenus bilden
		Ext.each(Uwd.System.ProgramStack.getStack(), function(module){
			module.loadIconCss();
			// Verzeichnis der Programmgrupe hinzufügen falls dieses noch nicht existiert
			var programGroup = module.getProgramFolder();
			if (Ext.isEmpty(programGroup)) {
				programGroup = 'undefined';
			}
			if (!programGroups.contains(programGroup)) {
				programGroups.push(programGroup);
				programMenus[programGroup] = new Ext.menu.Menu();
				programMenu.add({
					text: programGroup,
					iconCls: 'startmenu-folder-icon', // icon and text class
					menu: programMenus[programGroup]
				});
			}

			var btn = programMenus[programGroup].add({
				iconCls: module.getIconClass(),
				text: module.getName(),
				scope: this,
				handler: function() {
					module.openWindow();
				}.createDelegate(this)
			});
		}.createDelegate(this));



		mainMenu.addMenu({
			id: 'tb_programs',
			iconCls: 'icon_tb_applications', // icon and text class
			text: Uwd.System.SystemLanguage.programs,
			menu: programMenu,
			scope: this
		 });
		//mainMenu.add('-');
			
		var startBtn = new Ext.Button({
			cls: 'x-btn-text-icon icon_tb_start', // icon and text class
			text: '<b>' + Uwd.System.SystemLanguage.start + ' &uarr;</b>',
			renderTo:'x-toolbar',
			handler: function() {
				if (mainMenu.hidden) {
					mainMenu.show();
				} else {
					mainMenu.hide();	
				}
			}.createDelegate(this),
			//menu: mainMenu,
			width: 70,
			template: new Ext.Template(
				'<table cellspacing="0" class="x-btn {3}"><tbody><tr>',
				'<td class="ux-taskbutton-left"><i>&#160;</i></td>',
				'<td class="ux-taskbutton-center"><em class="{5} unselectable="on">',
					'<button class="x-btn-text {2}" type="{1}" style="height:28px;">{0}</button>',
				'</em></td>',
				'<td class="ux-taskbutton-right"><i>&#160;</i></td>',
				"</tr></tbody></table>")   
		});
		Ext.get('x-windows').removeClass('x-hidden');
	 }
});
