//$Id$

MainLayout = function() {
	return {
		login:function() {
			var A = new NGMP.dialog.LoginDialog();
			NGMP.loginDialog.addCallback(function() {
					MainLayout.init();
					this.setAdminMenu();
			},this);
			NGMP.loginDialog.show();
			this.removeLoadMask();
		},
		init:function() {
			this.tabPanel = new Ext.TabPanel({
				region:'center',
				titlebar: false,
				border:false,
				activeTab:0,
				tabPosition:'top',
			    items: NGMP.moduleManager.getAllPanels(),
				layoutOnTabChange:true
			});


			var topPanel = new Ext.Panel({
				region:'north',
				cls:'ngmp-top-panel',
				id:'topPanel',
				height:28,
				titlebar:false,
				border:false,
				html:"<div class='ngmp-top-panel'><div id='top-panel-logged'>"+LANG_logged_as + ' ' + NGMP.preferences.settings.account + "</div><div id='top-panel-logout'><font color='white'>" + LANG_logout+"</font></div><div id='top-panel-admin'><a id='adminMenuLink' href='#'><font color='white'>"+LANG_admin+"</font></a></div></div>"
			});


			var viewport = new Ext.Viewport({
				layout:'border',
				border:false,
				items:[topPanel, this.tabPanel]
			});
		},
		removeLoadMask:function() {
			var loading = Ext.get('loading');
			var mask = Ext.get('loading-mask');
			mask.setOpacity(0.8);
			mask.shift({
				xy:loading.getXY(),
				width:loading.getWidth(),
				height:loading.getHeight(),
				remove:true,
				duration:0.5,
				opacity:0.3,
				easing:'bounceOut',
				callback: function() {
					loading.fadeOut({
						duration:0.2,
						remove:true
					});
				}
			});
		},
		setAdminMenu:function() {
			var adminMenuLink = Ext.get('adminMenuLink');
			if (adminMenuLink) {
				adminMenuLink.on('click', function() {
					//check if admin
					Ext.Ajax.request({
						url:'ngmp.php?func=adminCheckAdmin',
						method:'POST',
						success:function(A,B) {
							var J = Ext.decode(A.responseText);
							if (J.success) {
								if (!NGMP.admin.adminWindow)
									NGMP.admin.adminWindow = new NGMP.admin.AdminWindow();
								NGMP.admin.adminWindow.show();
							} else {
								Ext.Msg.alert(LANG_error, LANG_not_admin);
							}
						},
						failture:function(A,B) {
						},
						scope:this
					});
				
				},this);
			}
			var logoutMenuLink = Ext.get('top-panel-logout');
			if (logoutMenuLink) {
				logoutMenuLink.on('click', function() {
					Ext.Ajax.request({
						url:'ngmp.php',
						params:{
							func:'logout'
						},
						success:function(A,B) {
							var J = Ext.decode(A.responseText);
							if (J.success) {
								window.location.reload();	
							}
						},
						scope:this
					});
				},this);
			}
		}
	};
}();
