Ext.BLANK_IMAGE_URL = Ext.urlScriptDir + '/ext/resources/images/default/s.gif';

// Custom function for writing to the FireBug console
log = function(data){
	try {
		console.log(data);
	} catch(e) {
  		console = { log: Ext.log };
	}
};

// Executes after a successful login
var MainScreen = function(){
	// Enable tooltip functionality
   		Ext.QuickTips.init();
	// Specify where form validation tooltips go
    	Ext.form.Field.prototype.msgTarget = 'side';
	// Define the main regions (from start.js)
    	var mainPanel = new MainPanel();
    	Ext.mainPanelVar = mainPanel;
	// Opens either an HTML tabPanel fragment, or loads a component
	    function onItemClick(item){
	    	if(item.comp){
	    		rpmCom.loadComp(item.comp, item.tab, mainPanel);
	    	}
	    	else{
				mainPanel.loadClass('output/'+item.page, item.tab, '');
	    	}
	    }
	// Returns the user to the Home tab (welcome.html)
	    function onHomeClick(item){
	    	Ext.info.msg('Navigating', 'Taking you Home...', 1.50);
	    	mainPanel.setActiveTab(0);
	    }
	// Clears some various loading masks
	    function onClearMask(item){
	    	if(Ext.getCmp('meetingRoomPanel')){
	    		// Remove the loading mask
				Ext.getCmp('meetingRoomPanel').getEl().unmask();
	    	}
	    	if(Ext.getCmp('tourPanel')){
	    		// Remove the loading mask
				Ext.getCmp('tourPanel').getEl().unmask();
	    	}
	    }
	// Global keyListeners
	    var keyMapHome = new Ext.KeyMap(document,{
	        key: 36, // CTRL + HOME
	        ctrl: true,
	        fn: onHomeClick,
	        scope: document
	    });

	    var keyMapRemoveMask = new Ext.KeyMap(document,{
	        key: 46, // CTRL + DELETE
	        ctrl: true,
	        fn: onClearMask,
	        scope: document
	    });

    // Prebuild menus passed by reference later
	    var dateMenu = new Ext.menu.DateMenu({
	        handler : function(dp, date){
	            Ext.info.msg('Date Selected', 'You chose {0}.', date.format('M j, Y'));
	        }
	    });

	    var colorMenu = new Ext.menu.ColorMenu({
	        handler : function(cm, color){
	            Ext.info.msg('Color Selected', 'You chose {0}.', color);
	        }
	    });

	    
    /**
     * Assign top level menu structure
     */
	    
	    var menuShow = new Ext.menu.Menu({ });

	    var menuResource = new Ext.menu.Menu({ });

	    var menuTour = new Ext.menu.Menu({ });

	    var menuDemo = new Ext.menu.Menu({ });

	    var menuUser = new Ext.menu.Menu({ });

	    var menuCalendar = new Ext.menu.Menu({ });
	    
	    var menuAdmin = new Ext.menu.Menu({ });

	    var menuReports = new Ext.menu.Menu({ });

	    var menuHelp = new Ext.menu.Menu({ });
	    
	    var menuHome = new Ext.menu.Menu({ });
	    

	/**
	 * ADMIN MENU SYSTEM
	 */
	    if(Ext.user_level == 2){
	    	
	    /**
	     * SHOW MENU
	     */	
		    menuShow.add({
		    	text: 'Create New / Modify Existing',
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/add.png',
		    	handler: function(){
					var tab = mainPanel.getComponent('docs-Edit Show');
		    		if(tab){
		    			mainPanel.setActiveTab(tab);
		    		}
		    		else{
		    			Ext.show.editShowDetails(mainPanel);
		    		}
		    	}
		    });
		    menuShow.addSeparator();
		    menuShow.add({
		    	text: 'Manage...',
		    	menu: {
		    		items: [
		    		{
		    			text: 'Registered User Details',
		    			icon: Ext.urlScriptDir + Ext.urlIconDir + '/user.png',
		    			handler: function(){
							var tab = mainPanel.getComponent('docs-Edit Registered Users');
				    		if(tab){
				    			mainPanel.setActiveTab(tab);
				    		}
				    		else{
				    			Ext.show.editRegisteredUsers(mainPanel);
				    		}
		    			}
		    		},
		    		{
		    			text: 'Registered User Availability',
		    			icon: Ext.urlScriptDir + Ext.urlIconDir + '/user_comment.png',
		    			handler: function(){
		    				Ext.editAvailability(mainPanel);
		    			}
		    		}
		    	]}
		    });
		    
		    
		/**
		 * RESOURCE MENU
		 */
		    menuResource.add({
		    	text: 'Book New / Modify Existing',
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/add.png',
		    	handler: function(){
			    	var tab = mainPanel.getComponent('docs-Edit Meeting');
		    		if(tab){
		    			mainPanel.setActiveTab(tab);
		    		}
		    		else{
		    			Ext.resource.editMeetingRoom(mainPanel);
		    		}
		    	}
		    });
		    menuResource.addSeparator();
		    menuResource.add({
		    	text: 'Manage...',
		    	menu: {
		    		items: [
		    		{
		    			text: 'Resource List',
		    			icon: Ext.urlScriptDir + Ext.urlIconDir + '/page_go.png',
		    			handler: function(){
					    	var tab = mainPanel.getComponent('docs-Edit Resources');
				    		if(tab){
				    			mainPanel.setActiveTab(tab);
				    		}
				    		else{
				    			Ext.resource.editGlobalResources(mainPanel);
				    		}
		    			}
		    		},
		    		{
		    			text: 'Resource Availability',
		    			icon: Ext.urlScriptDir + Ext.urlIconDir + '/door_in.png',
		    			handler: function(){
			    			var tab = mainPanel.getComponent('docs-Resource Availability');
				    		if(tab){
				    			mainPanel.setActiveTab(tab);
				    		}
				    		else{
				    			Ext.resource.editResourceAvail(mainPanel);
				    		}
		    			}
		    		}
		    	]}
		    });
		    menuResource.add({
		    	text: 'View...',
		    	menu: {
		    		items: [
		    		{
		    	    	text: 'Meeting Room Calendar',
		    	    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/mtg_calendar.png',
		    	    	handler: function(){
		    				Ext.MeetingRoomCalendar(mainPanel);
		    	    	}
		    	    }
		    	]}
		    });

		    
		/**
		 * TOUR MENU
		 */
		    menuTour.add({
		    	text: 'Book New / Modify Existing',
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/add.png',
				handler: function(){
					var tab = mainPanel.getComponent('docs-Edit Tours');
		    		if(tab){
		    			mainPanel.setActiveTab(tab);
		    		}
		    		else{
		    			Ext.resource.editTour(mainPanel);
		    		}
				}
		    });
		    menuTour.addSeparator();
		    menuTour.add({
		    	text: 'Manage...',
		    	menu: {
		    		items: [
		    	    {
		    	       	text: 'Customer Booking Restrictions',
		    	       	icon: Ext.urlScriptDir + Ext.urlIconDir + '/exclamation.png',
		    	       	handler: function(){
					    	var tab = mainPanel.getComponent('docs-Booking Restrictions');
				    		if(tab){
				    			mainPanel.setActiveTab(tab);
				    		}
				    		else{
				    			Ext.booking.customerRestriction(mainPanel);
				    		}
		    	    	}
		    	    }
		    	]}
		    });
		    menuTour.add({
		    	text: 'View...',
		    	menu: {
		    		items: [
		    	    {
		    	       	text: 'Tour Calendar',
		    	       	icon: Ext.urlScriptDir + Ext.urlIconDir + '/tour_calendar.png',
		    	       	handler: function(){
			    	    	Ext.TourCalendar(mainPanel);
		    	    	}
		    	    }
		    	]}
		    });

		    
		/**
		 * DEMO MENU
		 */
		    menuDemo.add({
		    	text: 'Create New / Modify Existing',
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/add.png',
		    	handler: function(){
			    	var tab = mainPanel.getComponent('docs-Edit Demos');
		    		if(tab){
		    			mainPanel.setActiveTab(tab);
		    		}
		    		else{
		    			Ext.resource.editDemo(mainPanel);
		    		}
		    	}
		    });
		    menuDemo.addSeparator();
		    menuDemo.add({
		    	text: 'Manage...',
		    	menu: {
		    		items: [
		    		{
		    			text: 'Demo Staff Assignment',
		    			icon: Ext.urlScriptDir + Ext.urlIconDir + '/server_go.png',
		    			handler: function(){
			    			var tab = mainPanel.getComponent('docs-Demo Staff Assignment');
				    		if(tab){
				    			mainPanel.setActiveTab(tab);
				    		}
				    		else{
				    			Ext.resource.demoAssign(mainPanel);
				    		}
		    			}
		    		}
		    	]}
		    });
		    menuDemo.add({
		    	text: 'View...',
		    	menu: {
		    		items: [
		    		{
		    	    	text: 'Demo Staff Calendar',
		    	    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/demo_calendar.png',
		    	    	handler: function(){
		    				Ext.DemoCalendar(mainPanel);
		    	    	}
		    	    }
		    	]}
		    });

		    
		/**
		 * USER MENU
		 */
		    menuUser.add({
		    	text: 'User Details',
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/user_gray.png',
		    	handler: function(){
		    		Ext.editUsers(mainPanel);
		    	}
		    });
		    menuUser.add({
		    	text: 'Manage...',
		    	menu: {
		    		items: [
		    		{
		    			text: 'User Groups',
		    			icon: Ext.urlScriptDir + Ext.urlIconDir + '/group_edit.png',
		    			handler: function(){
					    	var tab = mainPanel.getComponent('docs-Edit User Groups');
				    		if(tab){
				    			mainPanel.setActiveTab(tab);
				    		}
				    		else{
				    			Ext.user.editUserGroups(mainPanel);
				    		}
		    	    	}
		    		},
		    		{
		    			text: 'Customer List',
		    			icon: Ext.urlScriptDir + Ext.urlIconDir + '/group_link.png',
		    			handler: function(){
					    	var tab = mainPanel.getComponent('docs-Edit Customers');
				    		if(tab){
				    			mainPanel.setActiveTab(tab);
				    		}
				    		else{
				    			Ext.customer.editGlobalCustomers(mainPanel);
				    		}
		    			}
		    		},
		    		{
		    			text: 'Show Access',
		    			icon: Ext.urlScriptDir + Ext.urlIconDir + '/shield.png',
		    			handler: function(){
			    			var tab = mainPanel.getComponent('docs-User Show Access');
				    		if(tab){
				    			mainPanel.setActiveTab(tab);
				    		}
				    		else{
				    			Ext.user.editUserAccess(mainPanel);
				    		}
		    			}
		    		}
		    	]}
		    });

		    
		/**
		 * CALENDAR MENU
		 */
		    menuCalendar.add({
		    	text: 'Full Show Calendar',
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/date_magnify.png',
		    	handler: function(){
		    		Ext.Calendar(mainPanel);
		    	}
		    });
		    menuCalendar.add({
		    	text: 'Tour Calendar',
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/tour_calendar.png',
		    	handler: function(){
		    		Ext.TourCalendar(mainPanel);
		    	}
		    });
		    menuCalendar.add({
		    	text: 'Meeting Room Calendar',
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/mtg_calendar.png',
		    	handler: function(){
		    		Ext.MeetingRoomCalendar(mainPanel);
		    	}
		    });
		    menuCalendar.add({
		    	text: 'Demo Staff Calendar',
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/demo_calendar.png',
		    	handler: function(){
					Ext.DemoCalendar(mainPanel);
		    	}
		    });
		    menuCalendar.addSeparator();
		    menuCalendar.add({
		    	text: 'Individual\'s Calendar',
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/calendar.png',
		    	handler: function(){
		    		Ext.indyCalendar(mainPanel);
		    	}
		    });
		    
		
	    /**
	     * REPORTS MENU
	     */
		    menuReports.add({ 
		    	text: 'All Reports', 
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/report_user.png',
		    	handler: function(){
		    		Ext.Report(mainPanel);
		    	}
		    });
		    
		/**
		 * ADMIN MENU
		 */
		    
		    menuAdmin.add({ 
		    	text: 'Uploads', 
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/page_excel.png',
		    	handler: function(){
		    		Ext.EditUpload(mainPanel);
		    	}
		    });
		    
		    menuAdmin.add({ 
		    	text: 'Email Templates', 
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/email_edit.png',
		    	handler: function(){
		    		Ext.Email(mainPanel);
		    	}
		    });
		    
	    /**
	     * HELP ITEMS
	     */
		    menuHelp.add({ 
		    	text: 'Contact Information', 
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/vcard.png',
		    	handler: function(){
		    		Ext.Help(mainPanel);
		    	}
		    });			    
		   
		/**
		 * Generate the admin (user level 2) menu
		 */
			var menuToolbar = new Ext.Toolbar({
	            cls:'top-toolbar',
	            items:[
	            // Add the menu items created above
	            	{text: 'Show', iconCls: 'icon-fav', menu: menuShow},
	                ' ',
	                ' ',
	            	{text: 'Resource', iconCls: 'icon-door-open', menu: menuResource},
	            	' ',
	            	' ',
	            	{text: 'Tour', iconCls: 'icon-time', menu: menuTour},
	            	' ',
	            	' ',
	            	{text: 'Demo', iconCls: 'icon-server', menu: menuDemo},
	            	' ',
	            	' ',
	            	{text: 'User', iconCls: 'icon-group', menu: menuUser},
	            	' ',
	            	' ',
	                {text: 'Calendar', iconCls: 'icon-calendar', menu: menuCalendar},
	                ' ',
	                ' ',
	                {text: 'Reports', iconCls: 'icon-report', menu: menuReports},
	                ' ',
	                ' ',
	                {text: 'Admin', iconCls: 'icon-key', menu: menuAdmin},
	                ' ',
	                ' ',
	                {text: 'Help', iconCls: 'icon-help', menu: menuHelp},
	                ' ',
	                ' ',
	            	'->', // Using -> will fill the menu bar with a 100% width div, pushing all future elements to the far right
	                {text: 'Home', iconCls:'icon-house', handler: onHomeClick, tooltip: "Shortcut: Ctrl + Home"},
	                ' ',
	                ' ',
	                ' ',
	                ' ',
	                {text: 'Logout ' + Ext.userName, id: 'logout', handler: onLogoutClick}
	            ]
	        });
	    }
	    else if (Ext.user_level == 1) {
		
		/**
		 * USER MENU
		 */
		    menuUser.add({
		    	text: 'Your Profile',
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/user_gray.png',
		    	handler: function(){
		    		Ext.editUsers(mainPanel);
		    	}
		    });
		    menuUser.add({ 
		    	text: 'Your Availability', 
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/user_comment.png',
		    	handler: function(){
		    		Ext.editAvailability(mainPanel);
		    	}
		    });		    
		    
		    
		/**
		 * CALENDAR MENU
		 */
		    menuCalendar.add({
		    	text: 'Your Calendar',
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/calendar.png',
				handler: function(){
		    		Ext.yourCalendar(mainPanel);
				}
		    });
		    
		    menuCalendar.add({
		    	text: 'Full Show Calendar',
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/date_magnify.png',
		    	handler: function(){
	    			Ext.Calendar(mainPanel);
		    	}
		    });
		   
	    /**
	     * REPORTS MENU
	     */
		    menuReports.add({ 
		    	text: 'All Reports', 
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/report_user.png',
		    	handler: function(){
		    		Ext.Report(mainPanel);
		    	}
		    });		    
		    
		    
	    /**
	     * HELP ITEMS
	     */
		    menuHelp.add({ 
		    	text: 'Contact Information', 
		    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/vcard.png',
		    	handler: function(){
		    		Ext.Help(mainPanel);
		    	}
		    });			    
		    
		    
		/**
		 * Generate the tour/demo (user level 1) menu
		 */
	    
	        var menuToolbar = new Ext.Toolbar({
	            cls:'top-toolbar',
	            items:[
	            // Add the menu items created above
	            	{text: 'User', iconCls: 'icon-group', menu: menuUser},
	            	' ',
	            	' ',
	                {text: 'Calendar', iconCls: 'icon-calendar', menu: menuCalendar},
	                ' ',
	                ' ',
	                {text: 'Reports', iconCls: 'icon-report', menu: menuReports},
	                ' ',
	                ' ',
	                {text: 'Help', iconCls: 'icon-help', menu: menuHelp},
	                ' ',
	                ' ',
	            	'->', // Using -> will fill the menu bar with a 100% width div, pushing all future elements to the far right
                    {text: 'Home', iconCls:'icon-house', handler: onHomeClick, tooltip: "Shortcut: Ctrl + Home"},
                    ' ',
                    ' ',
                    ' ',
                    ' ',
                    {text: 'Logout ' + Ext.userName, id: 'logout', handler: onLogoutClick}
	            ]
	        });
		}
		else if (Ext.user_level == 0) {
			
			/**
			 * USER MENU
			 */
			    menuUser.add({
			    	text: 'Your Profile',
			    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/user_gray.png',
			    	handler: function(){
			    		Ext.editUsers(mainPanel);
			    	}
			    });

			    
		    /**
		     * REPORTS MENU
		     */
			    menuReports.add({ 
			    	text: 'All Reports', 
			    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/report_user.png',
			    	handler: function(){
			    		Ext.Report(mainPanel);
			    	}
			    });		
			    
			    
			 /**
			  * CALENDAR MEETING
			  */
			    menuCalendar.add({
			    	text: 'Full Show Calendar',
			    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/date_magnify.png',
			    	handler: function(){
		    			Ext.Calendar(mainPanel);
			    	}
			    });
			   
			    
		    /**
		     * HELP ITEMS
		     */
			    menuHelp.add({ 
			    	text: 'Contact Information', 
			    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/vcard.png',
			    	handler: function(){
			    		Ext.Help(mainPanel);
			    	}
			    });			    
			    
			    
			/**
			 * Generate the tour/demo (user level 1) menu
			 */
		    
		        var menuToolbar = new Ext.Toolbar({
		            cls:'top-toolbar',
		            items:[
		            // Add the menu items created above
		            	{text: 'User', iconCls: 'icon-group', menu: menuUser},
		            	' ',
		            	' ',
		                {text: 'Calendar', iconCls: 'icon-calendar', menu: menuCalendar},
		                ' ',
		                ' ',
		                {text: 'Reports', iconCls: 'icon-report', menu: menuReports},
		                ' ',
		                ' ',
		                {text: 'Help', iconCls: 'icon-help', menu: menuHelp},
		                ' ',
		                ' ',
		            	'->', // Using -> will fill the menu bar with a 100% width div, pushing all future elements to the far right
	                    {text: 'Home', iconCls:'icon-house', handler: onHomeClick, tooltip: "Shortcut: Ctrl + Home"},
	                    ' ',
	                    ' ',
	                    ' ',
	                    ' ',
	                    {text: 'Logout ' + Ext.userName, id: 'logout', handler: onLogoutClick}
		            ]
		        });
		}
		else {
			
			/**
			 * USER MENU
			 */
			    menuUser.add({
			    	text: 'Your Profile',
			    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/user_gray.png',
			    	handler: function(){
			    		Ext.editUsers(mainPanel);
			    	}
			    });

			    
		    /**
		     * HELP ITEMS
		     */
			    menuHelp.add({ 
			    	text: 'Contact Information', 
			    	icon: Ext.urlScriptDir + Ext.urlIconDir + '/vcard.png',
			    	handler: function(){
			    		Ext.Help(mainPanel);
			    	}
			    });			    
			    
			    
			/**
			 * Generate the tour/demo (user level 1) menu
			 */
		    
		        var menuToolbar = new Ext.Toolbar({
		            cls:'top-toolbar',
		            items:[
		            // Add the menu items created above
		            	{text: 'User', iconCls: 'icon-group', menu: menuUser},
		            	' ',
		            	' ',
		                {text: 'Help', iconCls: 'icon-help', menu: menuHelp},
		                ' ',
		                ' ',
		            	'->', // Using -> will fill the menu bar with a 100% width div, pushing all future elements to the far right
	                    {text: 'Home', iconCls:'icon-house', handler: onHomeClick, tooltip: "Shortcut: Ctrl + Home"},
	                    ' ',
	                    ' ',
	                    ' ',
	                    ' ',
	                    {text: 'Logout ' + Ext.userName, id: 'logout', handler: onLogoutClick}
		            ]
		        });					        
		} // end if statement

	/* build menu bar in North region */
	    var menuBar = new Ext.Panel({
	        border: false,
	        layout: 'anchor',
	        region:'north',
	        cls: 'docs-header',
	        height:60,
	        items: [
		        {
		        	// To use this header box, increase the height of the menu-bar to 60
		            xtype:'box',
		            el:'header',
		            border:false,
		            anchor: 'none -32'
		        },
		        menuToolbar
	        ]
	    });
		
	/* create the viewport which will maximize itself to fill the entire screen */
	    var viewport = new Ext.Viewport({
	        layout:'border',
	        id:'mainViewport',
	        items:[ menuBar, mainPanel ]
	    });

	/* render everything we've created */
	    viewport.doLayout();

}; // end MainScreen